Debian Codegraph @

Lib/test/test_filecmp.py

raw
1 import filecmp
2 import os
3 import re
4 import shutil
5 import tempfile
6 import unittest
7
8 from test import support
9 from test.support import os_helper
10
11
12 def _create_file_shallow_equal(template_path, new_path):
13 """create a file with the same size and mtime but different content."""
14 shutil.copy2(template_path, new_path)
15 with open(new_path, 'r+b') as f:
16 next_char = bytearray(f.read(1))
17 next_char[0] = (next_char[0] + 1) % 256
18 f.seek(0)
19 f.write(next_char)
20 shutil.copystat(template_path, new_path)
21 assert os.stat(new_path).st_size == os.stat(template_path).st_size
22 assert os.stat(new_path).st_mtime == os.stat(template_path).st_mtime
23
24 class FileCompareTestCase(unittest.TestCase):
25 def setUp(self):
26 self.name = os_helper.TESTFN
27 self.name_same = os_helper.TESTFN + '-same'
28 self.name_diff = os_helper.TESTFN + '-diff'
29 self.name_same_shallow = os_helper.TESTFN + '-same-shallow'
30 data = 'Contents of file go here.\n'
31 for name in [self.name, self.name_same, self.name_diff]:
32 with open(name, 'w', encoding="utf-8") as output:
33 output.write(data)
34
35 with open(self.name_diff, 'a+', encoding="utf-8") as output:
36 output.write('An extra line.\n')
37
38 for name in [self.name_same, self.name_diff]:
39 shutil.copystat(self.name, name)
40
41 _create_file_shallow_equal(self.name, self.name_same_shallow)
42
43 self.dir = tempfile.gettempdir()
44
45 def tearDown(self):
46 os.unlink(self.name)
47 os.unlink(self.name_same)
48 os.unlink(self.name_diff)
49 os.unlink(self.name_same_shallow)
50
51 def test_matching(self):
52 self.assertTrue(filecmp.cmp(self.name, self.name),
53 "Comparing file to itself fails")
54 self.assertTrue(filecmp.cmp(self.name, self.name, shallow=False),
55 "Comparing file to itself fails")
56 self.assertTrue(filecmp.cmp(self.name, self.name_same),
57 "Comparing file to identical file fails")
58 self.assertTrue(filecmp.cmp(self.name, self.name_same, shallow=False),
59 "Comparing file to identical file fails")
60 self.assertTrue(filecmp.cmp(self.name, self.name_same_shallow),
61 "Shallow identical files should be considered equal")
62
63 def test_different(self):
64 self.assertFalse(filecmp.cmp(self.name, self.name_diff),
65 "Mismatched files compare as equal")
66 self.assertFalse(filecmp.cmp(self.name, self.dir),
67 "File and directory compare as equal")
68 self.assertFalse(filecmp.cmp(self.name, self.name_same_shallow,
69 shallow=False),
70 "Mismatched file to shallow identical file compares as equal")
71
72 def test_cache_clear(self):
73 first_compare = filecmp.cmp(self.name, self.name_same, shallow=False)
74 second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False)
75 filecmp.clear_cache()
76 self.assertTrue(len(filecmp._cache) == 0,
77 "Cache not cleared after calling clear_cache")
78
79 class DirCompareTestCase(unittest.TestCase):
80 def setUp(self):
81 tmpdir = tempfile.gettempdir()
82 self.dir = os.path.join(tmpdir, 'dir')
83 self.dir_same = os.path.join(tmpdir, 'dir-same')
84 self.dir_diff = os.path.join(tmpdir, 'dir-diff')
85 self.dir_diff_file = os.path.join(tmpdir, 'dir-diff-file')
86 self.dir_same_shallow = os.path.join(tmpdir, 'dir-same-shallow')
87
88 # Another dir is created under dir_same, but it has a name from the
89 # ignored list so it should not affect testing results.
90 self.dir_ignored = os.path.join(self.dir_same, '.hg')
91
92 self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
93 data = 'Contents of file go here.\n'
94
95 shutil.rmtree(self.dir, True)
96 os.mkdir(self.dir)
97 subdir_path = os.path.join(self.dir, 'subdir')
98 os.mkdir(subdir_path)
99 dir_file_path = os.path.join(self.dir, "file")
100 with open(dir_file_path, 'w', encoding="utf-8") as output:
101 output.write(data)
102
103 for dir in (self.dir_same, self.dir_same_shallow,
104 self.dir_diff, self.dir_diff_file):
105 shutil.rmtree(dir, True)
106 os.mkdir(dir)
107 subdir_path = os.path.join(dir, 'subdir')
108 os.mkdir(subdir_path)
109 if self.caseinsensitive and dir is self.dir_same:
110 fn = 'FiLe' # Verify case-insensitive comparison
111 else:
112 fn = 'file'
113
114 file_path = os.path.join(dir, fn)
115
116 if dir is self.dir_same_shallow:
117 _create_file_shallow_equal(dir_file_path, file_path)
118 else:
119 shutil.copy2(dir_file_path, file_path)
120
121 with open(os.path.join(self.dir_diff, 'file2'), 'w', encoding="utf-8") as output:
122 output.write('An extra file.\n')
123
124 # Add different file2 with respect to dir_diff
125 with open(os.path.join(self.dir_diff_file, 'file2'), 'w', encoding="utf-8") as output:
126 output.write('Different contents.\n')
127
128
129 def tearDown(self):
130 for dir in (self.dir, self.dir_same, self.dir_diff,
131 self.dir_same_shallow, self.dir_diff_file):
132 shutil.rmtree(dir)
133
134 def test_default_ignores(self):
135 self.assertIn('.hg', filecmp.DEFAULT_IGNORES)
136
137 def test_cmpfiles(self):
138 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) ==
139 (['file'], [], []),
140 "Comparing directory to itself fails")
141 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) ==
142 (['file'], [], []),
143 "Comparing directory to same fails")
144
145 # Try it with shallow=False
146 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'],
147 shallow=False) ==
148 (['file'], [], []),
149 "Comparing directory to itself fails")
150 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'],
151 shallow=False),
152 "Comparing directory to same fails")
153
154 self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_diff_file,
155 ['file', 'file2']) ==
156 (['file'], ['file2'], []),
157 "Comparing mismatched directories fails")
158
159 def test_cmpfiles_invalid_names(self):
160 # See https://github.com/python/cpython/issues/122400.
161 for file, desc in [
162 ('\x00', 'NUL bytes filename'),
163 (__file__ + '\x00', 'filename with embedded NUL bytes'),
164 ("\uD834\uDD1E.py", 'surrogate codes (MUSICAL SYMBOL G CLEF)'),
165 ('a' * 1_000_000, 'very long filename'),
166 ]:
167 for other_dir in [self.dir, self.dir_same, self.dir_diff]:
168 with self.subTest(f'cmpfiles: {desc}', other_dir=other_dir):
169 res = filecmp.cmpfiles(self.dir, other_dir, [file])
170 self.assertTupleEqual(res, ([], [], [file]))
171
172 def test_dircmp_invalid_names(self):
173 for bad_dir, desc in [
174 ('\x00', 'NUL bytes dirname'),
175 (f'Top{os.sep}Mid\x00', 'dirname with embedded NUL bytes'),
176 ("\uD834\uDD1E", 'surrogate codes (MUSICAL SYMBOL G CLEF)'),
177 ('a' * 1_000_000, 'very long dirname'),
178 ]:
179 d1 = filecmp.dircmp(self.dir, bad_dir)
180 d2 = filecmp.dircmp(bad_dir, self.dir)
181 for target in [
182 # attributes where os.listdir() raises OSError or ValueError
183 'left_list', 'right_list',
184 'left_only', 'right_only', 'common',
185 ]:
186 with self.subTest(f'dircmp(ok, bad): {desc}', target=target):
187 with self.assertRaises((OSError, ValueError)):
188 getattr(d1, target)
189 with self.subTest(f'dircmp(bad, ok): {desc}', target=target):
190 with self.assertRaises((OSError, ValueError)):
191 getattr(d2, target)
192
193 def _assert_lists(self, actual, expected):
194 """Assert that two lists are equal, up to ordering."""
195 self.assertEqual(sorted(actual), sorted(expected))
196
197 def test_dircmp_identical_directories(self):
198 self._assert_dircmp_identical_directories()
199 self._assert_dircmp_identical_directories(shallow=False)
200
201 def test_dircmp_different_file(self):
202 self._assert_dircmp_different_file()
203 self._assert_dircmp_different_file(shallow=False)
204
205 def test_dircmp_different_directories(self):
206 self._assert_dircmp_different_directories()
207 self._assert_dircmp_different_directories(shallow=False)
208
209 def _assert_dircmp_identical_directories(self, **options):
210 # Check attributes for comparison of two identical directories
211 left_dir, right_dir = self.dir, self.dir_same
212 d = filecmp.dircmp(left_dir, right_dir, **options)
213 self.assertEqual(d.left, left_dir)
214 self.assertEqual(d.right, right_dir)
215 if self.caseinsensitive:
216 self._assert_lists(d.left_list, ['file', 'subdir'])
217 self._assert_lists(d.right_list, ['FiLe', 'subdir'])
218 else:
219 self._assert_lists(d.left_list, ['file', 'subdir'])
220 self._assert_lists(d.right_list, ['file', 'subdir'])
221 self._assert_lists(d.common, ['file', 'subdir'])
222 self._assert_lists(d.common_dirs, ['subdir'])
223 self.assertEqual(d.left_only, [])
224 self.assertEqual(d.right_only, [])
225 self.assertEqual(d.same_files, ['file'])
226 self.assertEqual(d.diff_files, [])
227 expected_report = [
228 "diff {} {}".format(self.dir, self.dir_same),
229 "Identical files : ['file']",
230 "Common subdirectories : ['subdir']",
231 ]
232 self._assert_report(d.report, expected_report)
233
234 def _assert_dircmp_different_directories(self, **options):
235 # Check attributes for comparison of two different directories (right)
236 left_dir, right_dir = self.dir, self.dir_diff
237 d = filecmp.dircmp(left_dir, right_dir, **options)
238 self.assertEqual(d.left, left_dir)
239 self.assertEqual(d.right, right_dir)
240 self._assert_lists(d.left_list, ['file', 'subdir'])
241 self._assert_lists(d.right_list, ['file', 'file2', 'subdir'])
242 self._assert_lists(d.common, ['file', 'subdir'])
243 self._assert_lists(d.common_dirs, ['subdir'])
244 self.assertEqual(d.left_only, [])
245 self.assertEqual(d.right_only, ['file2'])
246 self.assertEqual(d.same_files, ['file'])
247 self.assertEqual(d.diff_files, [])
248 expected_report = [
249 "diff {} {}".format(self.dir, self.dir_diff),
250 "Only in {} : ['file2']".format(self.dir_diff),
251 "Identical files : ['file']",
252 "Common subdirectories : ['subdir']",
253 ]
254 self._assert_report(d.report, expected_report)
255
256 # Check attributes for comparison of two different directories (left)
257 left_dir, right_dir = self.dir_diff, self.dir
258 d = filecmp.dircmp(left_dir, right_dir, **options)
259 self.assertEqual(d.left, left_dir)
260 self.assertEqual(d.right, right_dir)
261 self._assert_lists(d.left_list, ['file', 'file2', 'subdir'])
262 self._assert_lists(d.right_list, ['file', 'subdir'])
263 self._assert_lists(d.common, ['file', 'subdir'])
264 self.assertEqual(d.left_only, ['file2'])
265 self.assertEqual(d.right_only, [])
266 self.assertEqual(d.same_files, ['file'])
267 self.assertEqual(d.diff_files, [])
268 expected_report = [
269 "diff {} {}".format(self.dir_diff, self.dir),
270 "Only in {} : ['file2']".format(self.dir_diff),
271 "Identical files : ['file']",
272 "Common subdirectories : ['subdir']",
273 ]
274 self._assert_report(d.report, expected_report)
275
276
277 def _assert_dircmp_different_file(self, **options):
278 # A different file2
279 d = filecmp.dircmp(self.dir_diff, self.dir_diff_file, **options)
280 self.assertEqual(d.same_files, ['file'])
281 self.assertEqual(d.diff_files, ['file2'])
282 expected_report = [
283 "diff {} {}".format(self.dir_diff, self.dir_diff_file),
284 "Identical files : ['file']",
285 "Differing files : ['file2']",
286 "Common subdirectories : ['subdir']",
287 ]
288 self._assert_report(d.report, expected_report)
289
290 def test_dircmp_no_shallow_different_file(self):
291 # A non shallow different file2
292 d = filecmp.dircmp(self.dir, self.dir_same_shallow, shallow=False)
293 self.assertEqual(d.same_files, [])
294 self.assertEqual(d.diff_files, ['file'])
295 expected_report = [
296 "diff {} {}".format(self.dir, self.dir_same_shallow),
297 "Differing files : ['file']",
298 "Common subdirectories : ['subdir']",
299 ]
300 self._assert_report(d.report, expected_report)
301
302 def test_dircmp_shallow_same_file(self):
303 # A non shallow different file2
304 d = filecmp.dircmp(self.dir, self.dir_same_shallow)
305 self.assertEqual(d.same_files, ['file'])
306 self.assertEqual(d.diff_files, [])
307 expected_report = [
308 "diff {} {}".format(self.dir, self.dir_same_shallow),
309 "Identical files : ['file']",
310 "Common subdirectories : ['subdir']",
311 ]
312 self._assert_report(d.report, expected_report)
313
314 def test_dircmp_shallow_is_keyword_only(self):
315 with self.assertRaisesRegex(
316 TypeError,
317 re.escape("dircmp.__init__() takes from 3 to 5 positional arguments but 6 were given"),
318 ):
319 filecmp.dircmp(self.dir, self.dir_same, None, None, True)
320 self.assertIsInstance(
321 filecmp.dircmp(self.dir, self.dir_same, None, None, shallow=True),
322 filecmp.dircmp,
323 )
324
325 def test_dircmp_subdirs_type(self):
326 """Check that dircmp.subdirs respects subclassing."""
327 class MyDirCmp(filecmp.dircmp):
328 pass
329 d = MyDirCmp(self.dir, self.dir_diff)
330 sub_dirs = d.subdirs
331 self.assertEqual(list(sub_dirs.keys()), ['subdir'])
332 sub_dcmp = sub_dirs['subdir']
333 self.assertEqual(type(sub_dcmp), MyDirCmp)
334
335 def test_report_partial_closure(self):
336 left_dir, right_dir = self.dir, self.dir_same
337 d = filecmp.dircmp(left_dir, right_dir)
338 left_subdir = os.path.join(left_dir, 'subdir')
339 right_subdir = os.path.join(right_dir, 'subdir')
340 expected_report = [
341 "diff {} {}".format(self.dir, self.dir_same),
342 "Identical files : ['file']",
343 "Common subdirectories : ['subdir']",
344 '',
345 "diff {} {}".format(left_subdir, right_subdir),
346 ]
347 self._assert_report(d.report_partial_closure, expected_report)
348
349 def test_report_full_closure(self):
350 left_dir, right_dir = self.dir, self.dir_same
351 d = filecmp.dircmp(left_dir, right_dir)
352 left_subdir = os.path.join(left_dir, 'subdir')
353 right_subdir = os.path.join(right_dir, 'subdir')
354 expected_report = [
355 "diff {} {}".format(self.dir, self.dir_same),
356 "Identical files : ['file']",
357 "Common subdirectories : ['subdir']",
358 '',
359 "diff {} {}".format(left_subdir, right_subdir),
360 ]
361 self._assert_report(d.report_full_closure, expected_report)
362
363 def _assert_report(self, dircmp_report, expected_report_lines):
364 with support.captured_stdout() as stdout:
365 dircmp_report()
366 report_lines = stdout.getvalue().strip().split('\n')
367 self.assertEqual(report_lines, expected_report_lines)
368
369
370 if __name__ == "__main__":
371 unittest.main()