1 | n/a | import filecmp |
---|
2 | n/a | import os |
---|
3 | n/a | import shutil |
---|
4 | n/a | import tempfile |
---|
5 | n/a | import unittest |
---|
6 | n/a | |
---|
7 | n/a | from test import support |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | class FileCompareTestCase(unittest.TestCase): |
---|
11 | n/a | def setUp(self): |
---|
12 | n/a | self.name = support.TESTFN |
---|
13 | n/a | self.name_same = support.TESTFN + '-same' |
---|
14 | n/a | self.name_diff = support.TESTFN + '-diff' |
---|
15 | n/a | data = 'Contents of file go here.\n' |
---|
16 | n/a | for name in [self.name, self.name_same, self.name_diff]: |
---|
17 | n/a | with open(name, 'w') as output: |
---|
18 | n/a | output.write(data) |
---|
19 | n/a | |
---|
20 | n/a | with open(self.name_diff, 'a+') as output: |
---|
21 | n/a | output.write('An extra line.\n') |
---|
22 | n/a | self.dir = tempfile.gettempdir() |
---|
23 | n/a | |
---|
24 | n/a | def tearDown(self): |
---|
25 | n/a | os.unlink(self.name) |
---|
26 | n/a | os.unlink(self.name_same) |
---|
27 | n/a | os.unlink(self.name_diff) |
---|
28 | n/a | |
---|
29 | n/a | def test_matching(self): |
---|
30 | n/a | self.assertTrue(filecmp.cmp(self.name, self.name), |
---|
31 | n/a | "Comparing file to itself fails") |
---|
32 | n/a | self.assertTrue(filecmp.cmp(self.name, self.name, shallow=False), |
---|
33 | n/a | "Comparing file to itself fails") |
---|
34 | n/a | self.assertTrue(filecmp.cmp(self.name, self.name_same), |
---|
35 | n/a | "Comparing file to identical file fails") |
---|
36 | n/a | self.assertTrue(filecmp.cmp(self.name, self.name_same, shallow=False), |
---|
37 | n/a | "Comparing file to identical file fails") |
---|
38 | n/a | |
---|
39 | n/a | def test_different(self): |
---|
40 | n/a | self.assertFalse(filecmp.cmp(self.name, self.name_diff), |
---|
41 | n/a | "Mismatched files compare as equal") |
---|
42 | n/a | self.assertFalse(filecmp.cmp(self.name, self.dir), |
---|
43 | n/a | "File and directory compare as equal") |
---|
44 | n/a | |
---|
45 | n/a | def test_cache_clear(self): |
---|
46 | n/a | first_compare = filecmp.cmp(self.name, self.name_same, shallow=False) |
---|
47 | n/a | second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False) |
---|
48 | n/a | filecmp.clear_cache() |
---|
49 | n/a | self.assertTrue(len(filecmp._cache) == 0, |
---|
50 | n/a | "Cache not cleared after calling clear_cache") |
---|
51 | n/a | |
---|
52 | n/a | class DirCompareTestCase(unittest.TestCase): |
---|
53 | n/a | def setUp(self): |
---|
54 | n/a | tmpdir = tempfile.gettempdir() |
---|
55 | n/a | self.dir = os.path.join(tmpdir, 'dir') |
---|
56 | n/a | self.dir_same = os.path.join(tmpdir, 'dir-same') |
---|
57 | n/a | self.dir_diff = os.path.join(tmpdir, 'dir-diff') |
---|
58 | n/a | |
---|
59 | n/a | # Another dir is created under dir_same, but it has a name from the |
---|
60 | n/a | # ignored list so it should not affect testing results. |
---|
61 | n/a | self.dir_ignored = os.path.join(self.dir_same, '.hg') |
---|
62 | n/a | |
---|
63 | n/a | self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a') |
---|
64 | n/a | data = 'Contents of file go here.\n' |
---|
65 | n/a | for dir in (self.dir, self.dir_same, self.dir_diff, self.dir_ignored): |
---|
66 | n/a | shutil.rmtree(dir, True) |
---|
67 | n/a | os.mkdir(dir) |
---|
68 | n/a | if self.caseinsensitive and dir is self.dir_same: |
---|
69 | n/a | fn = 'FiLe' # Verify case-insensitive comparison |
---|
70 | n/a | else: |
---|
71 | n/a | fn = 'file' |
---|
72 | n/a | with open(os.path.join(dir, fn), 'w') as output: |
---|
73 | n/a | output.write(data) |
---|
74 | n/a | |
---|
75 | n/a | with open(os.path.join(self.dir_diff, 'file2'), 'w') as output: |
---|
76 | n/a | output.write('An extra file.\n') |
---|
77 | n/a | |
---|
78 | n/a | def tearDown(self): |
---|
79 | n/a | for dir in (self.dir, self.dir_same, self.dir_diff): |
---|
80 | n/a | shutil.rmtree(dir) |
---|
81 | n/a | |
---|
82 | n/a | def test_default_ignores(self): |
---|
83 | n/a | self.assertIn('.hg', filecmp.DEFAULT_IGNORES) |
---|
84 | n/a | |
---|
85 | n/a | def test_cmpfiles(self): |
---|
86 | n/a | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) == |
---|
87 | n/a | (['file'], [], []), |
---|
88 | n/a | "Comparing directory to itself fails") |
---|
89 | n/a | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) == |
---|
90 | n/a | (['file'], [], []), |
---|
91 | n/a | "Comparing directory to same fails") |
---|
92 | n/a | |
---|
93 | n/a | # Try it with shallow=False |
---|
94 | n/a | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'], |
---|
95 | n/a | shallow=False) == |
---|
96 | n/a | (['file'], [], []), |
---|
97 | n/a | "Comparing directory to itself fails") |
---|
98 | n/a | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'], |
---|
99 | n/a | shallow=False), |
---|
100 | n/a | "Comparing directory to same fails") |
---|
101 | n/a | |
---|
102 | n/a | # Add different file2 |
---|
103 | n/a | with open(os.path.join(self.dir, 'file2'), 'w') as output: |
---|
104 | n/a | output.write('Different contents.\n') |
---|
105 | n/a | |
---|
106 | n/a | self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same, |
---|
107 | n/a | ['file', 'file2']) == |
---|
108 | n/a | (['file'], ['file2'], []), |
---|
109 | n/a | "Comparing mismatched directories fails") |
---|
110 | n/a | |
---|
111 | n/a | |
---|
112 | n/a | def test_dircmp(self): |
---|
113 | n/a | # Check attributes for comparison of two identical directories |
---|
114 | n/a | left_dir, right_dir = self.dir, self.dir_same |
---|
115 | n/a | d = filecmp.dircmp(left_dir, right_dir) |
---|
116 | n/a | self.assertEqual(d.left, left_dir) |
---|
117 | n/a | self.assertEqual(d.right, right_dir) |
---|
118 | n/a | if self.caseinsensitive: |
---|
119 | n/a | self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']]) |
---|
120 | n/a | else: |
---|
121 | n/a | self.assertEqual([d.left_list, d.right_list],[['file'], ['file']]) |
---|
122 | n/a | self.assertEqual(d.common, ['file']) |
---|
123 | n/a | self.assertEqual(d.left_only, []) |
---|
124 | n/a | self.assertEqual(d.right_only, []) |
---|
125 | n/a | self.assertEqual(d.same_files, ['file']) |
---|
126 | n/a | self.assertEqual(d.diff_files, []) |
---|
127 | n/a | expected_report = [ |
---|
128 | n/a | "diff {} {}".format(self.dir, self.dir_same), |
---|
129 | n/a | "Identical files : ['file']", |
---|
130 | n/a | ] |
---|
131 | n/a | self._assert_report(d.report, expected_report) |
---|
132 | n/a | |
---|
133 | n/a | # Check attributes for comparison of two different directories (right) |
---|
134 | n/a | left_dir, right_dir = self.dir, self.dir_diff |
---|
135 | n/a | d = filecmp.dircmp(left_dir, right_dir) |
---|
136 | n/a | self.assertEqual(d.left, left_dir) |
---|
137 | n/a | self.assertEqual(d.right, right_dir) |
---|
138 | n/a | self.assertEqual(d.left_list, ['file']) |
---|
139 | n/a | self.assertEqual(d.right_list, ['file', 'file2']) |
---|
140 | n/a | self.assertEqual(d.common, ['file']) |
---|
141 | n/a | self.assertEqual(d.left_only, []) |
---|
142 | n/a | self.assertEqual(d.right_only, ['file2']) |
---|
143 | n/a | self.assertEqual(d.same_files, ['file']) |
---|
144 | n/a | self.assertEqual(d.diff_files, []) |
---|
145 | n/a | expected_report = [ |
---|
146 | n/a | "diff {} {}".format(self.dir, self.dir_diff), |
---|
147 | n/a | "Only in {} : ['file2']".format(self.dir_diff), |
---|
148 | n/a | "Identical files : ['file']", |
---|
149 | n/a | ] |
---|
150 | n/a | self._assert_report(d.report, expected_report) |
---|
151 | n/a | |
---|
152 | n/a | # Check attributes for comparison of two different directories (left) |
---|
153 | n/a | left_dir, right_dir = self.dir, self.dir_diff |
---|
154 | n/a | shutil.move( |
---|
155 | n/a | os.path.join(self.dir_diff, 'file2'), |
---|
156 | n/a | os.path.join(self.dir, 'file2') |
---|
157 | n/a | ) |
---|
158 | n/a | d = filecmp.dircmp(left_dir, right_dir) |
---|
159 | n/a | self.assertEqual(d.left, left_dir) |
---|
160 | n/a | self.assertEqual(d.right, right_dir) |
---|
161 | n/a | self.assertEqual(d.left_list, ['file', 'file2']) |
---|
162 | n/a | self.assertEqual(d.right_list, ['file']) |
---|
163 | n/a | self.assertEqual(d.common, ['file']) |
---|
164 | n/a | self.assertEqual(d.left_only, ['file2']) |
---|
165 | n/a | self.assertEqual(d.right_only, []) |
---|
166 | n/a | self.assertEqual(d.same_files, ['file']) |
---|
167 | n/a | self.assertEqual(d.diff_files, []) |
---|
168 | n/a | expected_report = [ |
---|
169 | n/a | "diff {} {}".format(self.dir, self.dir_diff), |
---|
170 | n/a | "Only in {} : ['file2']".format(self.dir), |
---|
171 | n/a | "Identical files : ['file']", |
---|
172 | n/a | ] |
---|
173 | n/a | self._assert_report(d.report, expected_report) |
---|
174 | n/a | |
---|
175 | n/a | # Add different file2 |
---|
176 | n/a | with open(os.path.join(self.dir_diff, 'file2'), 'w') as output: |
---|
177 | n/a | output.write('Different contents.\n') |
---|
178 | n/a | d = filecmp.dircmp(self.dir, self.dir_diff) |
---|
179 | n/a | self.assertEqual(d.same_files, ['file']) |
---|
180 | n/a | self.assertEqual(d.diff_files, ['file2']) |
---|
181 | n/a | expected_report = [ |
---|
182 | n/a | "diff {} {}".format(self.dir, self.dir_diff), |
---|
183 | n/a | "Identical files : ['file']", |
---|
184 | n/a | "Differing files : ['file2']", |
---|
185 | n/a | ] |
---|
186 | n/a | self._assert_report(d.report, expected_report) |
---|
187 | n/a | |
---|
188 | n/a | def test_report_partial_closure(self): |
---|
189 | n/a | left_dir, right_dir = self.dir, self.dir_same |
---|
190 | n/a | d = filecmp.dircmp(left_dir, right_dir) |
---|
191 | n/a | expected_report = [ |
---|
192 | n/a | "diff {} {}".format(self.dir, self.dir_same), |
---|
193 | n/a | "Identical files : ['file']", |
---|
194 | n/a | ] |
---|
195 | n/a | self._assert_report(d.report_partial_closure, expected_report) |
---|
196 | n/a | |
---|
197 | n/a | def test_report_full_closure(self): |
---|
198 | n/a | left_dir, right_dir = self.dir, self.dir_same |
---|
199 | n/a | d = filecmp.dircmp(left_dir, right_dir) |
---|
200 | n/a | expected_report = [ |
---|
201 | n/a | "diff {} {}".format(self.dir, self.dir_same), |
---|
202 | n/a | "Identical files : ['file']", |
---|
203 | n/a | ] |
---|
204 | n/a | self._assert_report(d.report_full_closure, expected_report) |
---|
205 | n/a | |
---|
206 | n/a | def _assert_report(self, dircmp_report, expected_report_lines): |
---|
207 | n/a | with support.captured_stdout() as stdout: |
---|
208 | n/a | dircmp_report() |
---|
209 | n/a | report_lines = stdout.getvalue().strip().split('\n') |
---|
210 | n/a | self.assertEqual(report_lines, expected_report_lines) |
---|
211 | n/a | |
---|
212 | n/a | |
---|
213 | n/a | def test_main(): |
---|
214 | n/a | support.run_unittest(FileCompareTestCase, DirCompareTestCase) |
---|
215 | n/a | |
---|
216 | n/a | if __name__ == "__main__": |
---|
217 | n/a | test_main() |
---|