Mercurial > treepkg
annotate test/test_util.py @ 518:452c5670ec6b
fix git tag detector usage in enterprise packager
working copy paths were missing
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Wed, 10 Nov 2010 13:32:37 +0000 |
parents | ca95be9d033a |
children |
rev | line source |
---|---|
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
1 # Copyright (C) 2007, 2008 by Intevation GmbH |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
2 # Authors: |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
3 # Bernhard Herzog <bh@intevation.de> |
495
ca95be9d033a
add tests for determine debian upstream version
Bjoern Ricks <bricks@intevation.de>
parents:
461
diff
changeset
|
4 # Bjoern Ricks <bjoern.ricks@intevation.de> |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
5 # |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
6 # This program is free software under the GPL (>=v2) |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
7 # Read the file COPYING coming with the software for details. |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
8 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
9 """Tests for the treepkg.util module""" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
10 |
169
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
11 import os |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
12 import unittest |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
13 import shutil |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
14 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
15 from filesupport import FileTestMixin |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
16 |
439 | 17 from treepkg.util import replace_in_file, listdir_abs, md5sum, \ |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
18 remove_trailing_slashes, expand_filename, \ |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
19 compress_all_logs |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
20 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
21 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
22 class TestReplaceInFile(unittest.TestCase, FileTestMixin): |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
23 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
24 def runtest(self, orig_contents, expected_contents, pattern, replacement): |
185
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
25 filename = self.create_temp_file("orig", orig_contents) |
160
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
26 changed = replace_in_file(filename, pattern, replacement) |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
27 self.assertEquals(changed, orig_contents != expected_contents) |
195
e3ab8aca2b08
Make filesupport.py more PEP8 conformant. Rename method
Bernhard Herzog <bh@intevation.de>
parents:
185
diff
changeset
|
28 self.check_file_contents(filename, expected_contents) |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
29 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
30 def test_version_replacement(self): |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
31 template = ("project foo version 1.0-svn%(rev)d" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
32 "Some filler" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
33 "text that sometimes" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
34 "looks similar to the pattern" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
35 "1.0-" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
36 "foo 1.2-svn2" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
37 "echo foo version 1.0-svn%(rev)d" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
38 "" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
39 "and more filler") |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
40 self.runtest(template % dict(rev=0), template % dict(rev=321), |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
41 r"1\.0-svn0", "1.0-svn321") |
160
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
42 |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
43 def test_no_matches(self): |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
44 """Tests replace_in_file when no matches are found""" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
45 template = ("project foo version 1.0-svn%(rev)d" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
46 "Some filler" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
47 "text that sometimes" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
48 "looks similar to the pattern" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
49 "1.0-" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
50 "foo 1.2-svn2" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
51 "echo foo version 1.0-svn%(rev)d" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
52 "" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
53 "and more filler") |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
54 self.runtest(template % dict(rev=0), template % dict(rev=0), |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
55 r"0\.9-svn0", "1.0-svn321") |
169
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
56 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
57 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
58 class TestListDirAbs(unittest.TestCase, FileTestMixin): |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
59 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
60 def setUp(self): |
185
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
61 self.directory = self.create_temp_dir("a_directory") |
169
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
62 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
63 def test_listdir_abs(self): |
185
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
64 directory = self.create_files("dir", [("foo.orig.tgz", ""), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
65 ("foo.dsc", ""), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
66 ("foo.diff.gz", ""),]) |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
67 self.assertEquals(sorted(listdir_abs(directory)), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
68 sorted([os.path.join(directory, d) |
169
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
69 for d in ["foo.orig.tgz", "foo.dsc", |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
70 "foo.diff.gz"]])) |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
71 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
72 def test_listdir_abs_pattern(self): |
185
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
73 directory = self.create_files("dir", [("foo.orig.tgz", ""), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
74 ("foo.dsc", ""), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
75 ("foo.diff.gz", ""),]) |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
76 self.assertEquals(sorted(listdir_abs(directory, '*.dsc')), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
77 [os.path.join(directory, "foo.dsc")]) |
169
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
78 |
394 | 79 class TestMd5sum(unittest.TestCase, FileTestMixin): |
80 | |
81 content = "this is a test content" | |
82 | |
83 def setUp(self): | |
84 self.testfile = self.create_temp_file("testmd5.txt", self.content) | |
85 | |
86 def test_md5sum(self): | |
87 sum = md5sum(self.testfile) | |
88 self.assertEquals("a12511153555c1f0f0a1eda200733a3f", sum) | |
439 | 89 |
90 class TestRemoveTrailingSlashes(unittest.TestCase): | |
91 | |
92 def test_remove_trailing_slashes(self): | |
93 dir_w_slash = "/tmp/dir/" | |
94 dir_wo_slash = "/tmp/dir" | |
95 | |
96 self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_w_slash)) | |
97 self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_wo_slash)) | |
98 | |
99 class TestExpandFilename(unittest.TestCase): | |
100 | |
101 def test_expand_filenam(self): | |
102 os.environ['MY_TEST_VAR'] = "def" | |
103 path = "/abc/${MY_TEST_VAR}/" | |
104 | |
105 self.assertEquals("/abc/def/", expand_filename(path)) | |
106 | |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
107 class TestCompressAllLogs(unittest.TestCase, FileTestMixin): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
108 |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
109 def test_compress_all_logs(self): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
110 log_dir = self.create_test_specific_temp_dir() |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
111 try: |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
112 for i in range(15): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
113 f = open(os.path.join(log_dir, "log_%d.txt" % i), "w") |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
114 print >> f, "World domination is right at hand!" |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
115 f.close() |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
116 ref_log = os.path.join(log_dir, "log_0.txt") |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
117 compress_all_logs(ref_log) |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
118 for i in range(15): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
119 self.assertTrue(os.path.isfile(os.path.join( |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
120 log_dir, "log_%d.txt.gz" % i))) |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
121 finally: |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
122 shutil.rmtree(log_dir, ignore_errors=True) |