8
8
from test .support import os_helper
9
9
10
10
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
+
11
23
class FileCompareTestCase (unittest .TestCase ):
12
24
def setUp (self ):
13
25
self .name = os_helper .TESTFN
@@ -21,9 +33,12 @@ def setUp(self):
21
33
22
34
with open (self .name_diff , 'a+' , encoding = "utf-8" ) as output :
23
35
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
+
27
42
self .dir = tempfile .gettempdir ()
28
43
29
44
def tearDown (self ):
@@ -75,8 +90,17 @@ def setUp(self):
75
90
76
91
self .caseinsensitive = os .path .normcase ('A' ) == os .path .normcase ('a' )
77
92
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 ):
80
104
shutil .rmtree (dir , True )
81
105
os .mkdir (dir )
82
106
subdir_path = os .path .join (dir , 'subdir' )
@@ -85,21 +109,22 @@ def setUp(self):
85
109
fn = 'FiLe' # Verify case-insensitive comparison
86
110
else :
87
111
fn = 'file'
112
+
113
+ file_path = os .path .join (dir , fn )
114
+
88
115
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 )
92
117
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 )
95
119
96
120
with open (os .path .join (self .dir_diff , 'file2' ), 'w' , encoding = "utf-8" ) as output :
97
121
output .write ('An extra file.\n ' )
98
122
99
- # Add different file2
123
+ # Add different file2 with respect to dir_diff
100
124
with open (os .path .join (self .dir_diff_file , 'file2' ), 'w' , encoding = "utf-8" ) as output :
101
125
output .write ('Different contents.\n ' )
102
126
127
+
103
128
def tearDown (self ):
104
129
for dir in (self .dir , self .dir_same , self .dir_diff ,
105
130
self .dir_same_shallow , self .dir_diff_file ):
@@ -296,6 +321,5 @@ def _assert_report(self, dircmp_report, expected_report_lines):
296
321
report_lines = stdout .getvalue ().strip ().split ('\n ' )
297
322
self .assertEqual (report_lines , expected_report_lines )
298
323
299
-
300
324
if __name__ == "__main__" :
301
325
unittest .main ()
0 commit comments