Skip to content

Commit a775613

Browse files
committed
Fix filecmp non shallow unittest.
1 parent 5e30595 commit a775613

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

Lib/test/test_filecmp.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
from test.support import os_helper
99

1010

11+
def _create_file_shallow_equal(template_path, new_path):
12+
"""create a file with the same size and mtime but different content."""
13+
shutil.copy2(template_path, new_path)
14+
with open(new_path, 'r+b') as f:
15+
next_char = bytearray(f.read(1))
16+
next_char[0] = (next_char[0] + 1) % 256
17+
f.seek(0)
18+
f.write(next_char)
19+
shutil.copystat(template_path, new_path)
20+
assert os.stat(new_path).st_size == os.stat(template_path).st_size
21+
assert os.stat(new_path).st_mtime == os.stat(template_path).st_mtime
22+
1123
class FileCompareTestCase(unittest.TestCase):
1224
def setUp(self):
1325
self.name = os_helper.TESTFN
@@ -21,9 +33,12 @@ def setUp(self):
2133

2234
with open(self.name_diff, 'a+', encoding="utf-8") as output:
2335
output.write('An extra line.\n')
24-
with open(self.name_same_shallow , 'wb') as output:
25-
# same file size; but different content (i.e. all zero)
26-
output.write(bytes(len(data.encode())))
36+
37+
for name in [self.name_same, self.name_diff]:
38+
shutil.copystat(self.name, name)
39+
40+
_create_file_shallow_equal(self.name, self.name_same_shallow)
41+
2742
self.dir = tempfile.gettempdir()
2843

2944
def tearDown(self):
@@ -75,8 +90,17 @@ def setUp(self):
7590

7691
self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
7792
data = 'Contents of file go here.\n'
78-
for dir in (self.dir, self.dir_same, self.dir_same_shallow,
79-
self.dir_diff, self.dir_ignored, self.dir_diff_file):
93+
94+
shutil.rmtree(self.dir, True)
95+
os.mkdir(self.dir)
96+
subdir_path = os.path.join(self.dir, 'subdir')
97+
os.mkdir(subdir_path)
98+
dir_file_path = os.path.join(self.dir, "file")
99+
with open(dir_file_path, 'w', encoding="utf-8") as output:
100+
output.write(data)
101+
102+
for dir in (self.dir_same, self.dir_same_shallow,
103+
self.dir_diff, self.dir_diff_file):
80104
shutil.rmtree(dir, True)
81105
os.mkdir(dir)
82106
subdir_path = os.path.join(dir, 'subdir')
@@ -85,21 +109,22 @@ def setUp(self):
85109
fn = 'FiLe' # Verify case-insensitive comparison
86110
else:
87111
fn = 'file'
112+
113+
file_path = os.path.join(dir, fn)
114+
88115
if dir is self.dir_same_shallow:
89-
with open(os.path.join(dir, fn) , 'wb') as output:
90-
# same file size; but different content (i.e. all zero)
91-
output.write(bytes(len(data.encode())))
116+
_create_file_shallow_equal(dir_file_path, file_path)
92117
else:
93-
with open(os.path.join(dir, fn), 'w', encoding="utf-8") as output:
94-
output.write(data)
118+
shutil.copy2(dir_file_path, file_path)
95119

96120
with open(os.path.join(self.dir_diff, 'file2'), 'w', encoding="utf-8") as output:
97121
output.write('An extra file.\n')
98122

99-
# Add different file2
123+
# Add different file2 with respect to dir_diff
100124
with open(os.path.join(self.dir_diff_file, 'file2'), 'w', encoding="utf-8") as output:
101125
output.write('Different contents.\n')
102126

127+
103128
def tearDown(self):
104129
for dir in (self.dir, self.dir_same, self.dir_diff,
105130
self.dir_same_shallow, self.dir_diff_file):
@@ -296,6 +321,5 @@ def _assert_report(self, dircmp_report, expected_report_lines):
296321
report_lines = stdout.getvalue().strip().split('\n')
297322
self.assertEqual(report_lines, expected_report_lines)
298323

299-
300324
if __name__ == "__main__":
301325
unittest.main()

0 commit comments

Comments
 (0)