2 import os
, filecmp
, shutil
, tempfile
4 from test
import test_support
6 class FileCompareTestCase(unittest
.TestCase
):
8 self
.name
= test_support
.TESTFN
9 self
.name_same
= test_support
.TESTFN
+ '-same'
10 self
.name_diff
= test_support
.TESTFN
+ '-diff'
11 data
= 'Contents of file go here.\n'
12 for name
in [self
.name
, self
.name_same
, self
.name_diff
]:
13 output
= open(name
, 'w')
17 output
= open(self
.name_diff
, 'a+')
18 output
.write('An extra line.\n')
20 self
.dir = tempfile
.gettempdir()
24 os
.unlink(self
.name_same
)
25 os
.unlink(self
.name_diff
)
27 def test_matching(self
):
28 self
.assertTrue(filecmp
.cmp(self
.name
, self
.name_same
),
29 "Comparing file to itself fails")
30 self
.assertTrue(filecmp
.cmp(self
.name
, self
.name_same
, shallow
=False),
31 "Comparing file to itself fails")
32 self
.assertTrue(filecmp
.cmp(self
.name
, self
.name
, shallow
=False),
33 "Comparing file to identical file fails")
34 self
.assertTrue(filecmp
.cmp(self
.name
, self
.name
),
35 "Comparing file to identical file fails")
37 def test_different(self
):
38 self
.assertFalse(filecmp
.cmp(self
.name
, self
.name_diff
),
39 "Mismatched files compare as equal")
40 self
.assertFalse(filecmp
.cmp(self
.name
, self
.dir),
41 "File and directory compare as equal")
43 class DirCompareTestCase(unittest
.TestCase
):
45 tmpdir
= tempfile
.gettempdir()
46 self
.dir = os
.path
.join(tmpdir
, 'dir')
47 self
.dir_same
= os
.path
.join(tmpdir
, 'dir-same')
48 self
.dir_diff
= os
.path
.join(tmpdir
, 'dir-diff')
49 self
.caseinsensitive
= os
.path
.normcase('A') == os
.path
.normcase('a')
50 data
= 'Contents of file go here.\n'
51 for dir in [self
.dir, self
.dir_same
, self
.dir_diff
]:
52 shutil
.rmtree(dir, True)
54 if self
.caseinsensitive
and dir is self
.dir_same
:
55 fn
= 'FiLe' # Verify case-insensitive comparison
58 output
= open(os
.path
.join(dir, fn
), 'w')
62 output
= open(os
.path
.join(self
.dir_diff
, 'file2'), 'w')
63 output
.write('An extra file.\n')
67 shutil
.rmtree(self
.dir)
68 shutil
.rmtree(self
.dir_same
)
69 shutil
.rmtree(self
.dir_diff
)
71 def test_cmpfiles(self
):
72 self
.assertTrue(filecmp
.cmpfiles(self
.dir, self
.dir, ['file']) ==
74 "Comparing directory to itself fails")
75 self
.assertTrue(filecmp
.cmpfiles(self
.dir, self
.dir_same
, ['file']) ==
77 "Comparing directory to same fails")
79 # Try it with shallow=False
80 self
.assertTrue(filecmp
.cmpfiles(self
.dir, self
.dir, ['file'],
83 "Comparing directory to itself fails")
84 self
.assertTrue(filecmp
.cmpfiles(self
.dir, self
.dir_same
, ['file'],
86 "Comparing directory to same fails")
89 output
= open(os
.path
.join(self
.dir, 'file2'), 'w')
90 output
.write('Different contents.\n')
93 self
.assertFalse(filecmp
.cmpfiles(self
.dir, self
.dir_same
,
95 (['file'], ['file2'], []),
96 "Comparing mismatched directories fails")
99 def test_dircmp(self
):
100 # Check attributes for comparison of two identical directories
101 d
= filecmp
.dircmp(self
.dir, self
.dir_same
)
102 if self
.caseinsensitive
:
103 self
.assertEqual([d
.left_list
, d
.right_list
],[['file'], ['FiLe']])
105 self
.assertEqual([d
.left_list
, d
.right_list
],[['file'], ['file']])
106 self
.assertTrue(d
.common
== ['file'])
107 self
.assertTrue(d
.left_only
== d
.right_only
== [])
108 self
.assertTrue(d
.same_files
== ['file'])
109 self
.assertTrue(d
.diff_files
== [])
111 # Check attributes for comparison of two different directories
112 d
= filecmp
.dircmp(self
.dir, self
.dir_diff
)
113 self
.assertTrue(d
.left_list
== ['file'])
114 self
.assertTrue(d
.right_list
== ['file', 'file2'])
115 self
.assertTrue(d
.common
== ['file'])
116 self
.assertTrue(d
.left_only
== [])
117 self
.assertTrue(d
.right_only
== ['file2'])
118 self
.assertTrue(d
.same_files
== ['file'])
119 self
.assertTrue(d
.diff_files
== [])
121 # Add different file2
122 output
= open(os
.path
.join(self
.dir, 'file2'), 'w')
123 output
.write('Different contents.\n')
125 d
= filecmp
.dircmp(self
.dir, self
.dir_diff
)
126 self
.assertTrue(d
.same_files
== ['file'])
127 self
.assertTrue(d
.diff_files
== ['file2'])
131 test_support
.run_unittest(FileCompareTestCase
, DirCompareTestCase
)
133 if __name__
== "__main__":