Mercurial > treepkg
annotate test/test_util.py @ 439:8e0c81870e5e treepkg-status
cleanup modules
fix test_info testcases
added testcases for remove_trailingslashes and expand_filename
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Fri, 06 Aug 2010 11:06:08 +0000 |
parents | bfd1c6a155fa |
children | 454967511f5c |
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 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
12 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
13 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
|
14 |
439 | 15 from treepkg.util import replace_in_file, listdir_abs, md5sum, \ |
16 remove_trailing_slashes, expand_filename | |
17 | |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
18 |
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 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
|
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 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
28 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
|
29 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
|
30 "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
|
31 "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
|
32 "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
|
33 "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
|
34 "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
|
35 "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
|
36 "" |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
diff
changeset
|
37 "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
|
38 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
|
39 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
|
40 |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
41 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
|
42 """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
|
43 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
|
44 "Some filler" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
45 "text that sometimes" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
46 "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
|
47 "1.0-" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
48 "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
|
49 "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
|
50 "" |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
51 "and more filler") |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
52 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
|
53 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
|
54 |
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 class TestListDirAbs(unittest.TestCase, FileTestMixin): |
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 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
|
59 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
|
60 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
61 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
|
62 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
|
63 ("foo.dsc", ""), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
64 ("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
|
65 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
|
66 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
|
67 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
|
68 "foo.diff.gz"]])) |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
69 |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
162
diff
changeset
|
70 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
|
71 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
|
72 ("foo.dsc", ""), |
e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
73 ("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
|
74 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
|
75 [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
|
76 |
394 | 77 class TestMd5sum(unittest.TestCase, FileTestMixin): |
78 | |
79 content = "this is a test content" | |
80 | |
81 def setUp(self): | |
82 self.testfile = self.create_temp_file("testmd5.txt", self.content) | |
83 | |
84 def test_md5sum(self): | |
85 sum = md5sum(self.testfile) | |
86 self.assertEquals("a12511153555c1f0f0a1eda200733a3f", sum) | |
439 | 87 |
88 class TestRemoveTrailingSlashes(unittest.TestCase): | |
89 | |
90 def test_remove_trailing_slashes(self): | |
91 dir_w_slash = "/tmp/dir/" | |
92 dir_wo_slash = "/tmp/dir" | |
93 | |
94 self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_w_slash)) | |
95 self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_wo_slash)) | |
96 | |
97 class TestExpandFilename(unittest.TestCase): | |
98 | |
99 def test_expand_filenam(self): | |
100 os.environ['MY_TEST_VAR'] = "def" | |
101 path = "/abc/${MY_TEST_VAR}/" | |
102 | |
103 self.assertEquals("/abc/def/", expand_filename(path)) | |
104 |