Mercurial > treepkg
annotate test/test_util.py @ 488:a0d19e20fb89
sawmill: moved imports before there usage
author | Sascha Teichmann <teichmann@intevation.de> |
---|---|
date | Sun, 26 Sep 2010 07:49:57 +0000 |
parents | 454967511f5c |
children | ca95be9d033a |
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> |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
4 # |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
5 # 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
|
6 # 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
|
7 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
8 """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
|
9 |
169
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
10 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
|
11 import unittest |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
12 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
|
13 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
14 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
|
15 |
439 | 16 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
|
17 remove_trailing_slashes, expand_filename, \ |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
18 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
|
19 |
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 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
|
22 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
29 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
|
30 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
|
31 "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
|
32 "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
|
33 "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
|
34 "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
|
35 "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
|
36 "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
|
37 "" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
38 "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
|
39 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
|
40 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
|
41 |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
42 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
|
43 """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
|
44 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
|
45 "Some filler" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
46 "text that sometimes" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
47 "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
|
48 "1.0-" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
49 "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
|
50 "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
|
51 "" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
52 "and more filler") |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
53 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
|
54 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
|
55 |
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 class TestListDirAbs(unittest.TestCase, FileTestMixin): |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
58 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
59 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
|
60 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
|
61 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
62 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
|
63 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
|
64 ("foo.dsc", ""), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
65 ("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
|
66 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
|
67 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
|
68 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
|
69 "foo.diff.gz"]])) |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
70 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
71 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
|
72 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
|
73 ("foo.dsc", ""), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
74 ("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
|
75 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
|
76 [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
|
77 |
394 | 78 class TestMd5sum(unittest.TestCase, FileTestMixin): |
79 | |
80 content = "this is a test content" | |
81 | |
82 def setUp(self): | |
83 self.testfile = self.create_temp_file("testmd5.txt", self.content) | |
84 | |
85 def test_md5sum(self): | |
86 sum = md5sum(self.testfile) | |
87 self.assertEquals("a12511153555c1f0f0a1eda200733a3f", sum) | |
439 | 88 |
89 class TestRemoveTrailingSlashes(unittest.TestCase): | |
90 | |
91 def test_remove_trailing_slashes(self): | |
92 dir_w_slash = "/tmp/dir/" | |
93 dir_wo_slash = "/tmp/dir" | |
94 | |
95 self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_w_slash)) | |
96 self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_wo_slash)) | |
97 | |
98 class TestExpandFilename(unittest.TestCase): | |
99 | |
100 def test_expand_filenam(self): | |
101 os.environ['MY_TEST_VAR'] = "def" | |
102 path = "/abc/${MY_TEST_VAR}/" | |
103 | |
104 self.assertEquals("/abc/def/", expand_filename(path)) | |
105 | |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
106 class TestCompressAllLogs(unittest.TestCase, FileTestMixin): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
107 |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
108 def test_compress_all_logs(self): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
109 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
|
110 try: |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
111 for i in range(15): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
112 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
|
113 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
|
114 f.close() |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
115 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
|
116 compress_all_logs(ref_log) |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
117 for i in range(15): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
118 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
|
119 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
|
120 finally: |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
439
diff
changeset
|
121 shutil.rmtree(log_dir, ignore_errors=True) |