move sections
[python/dscho.git] / Lib / test / test_filecmp.py
blob4739ede4eaea4d2e5d501abc2ead388bbeb56223
2 import os, filecmp, shutil, tempfile
3 import unittest
4 from test import test_support
6 class FileCompareTestCase(unittest.TestCase):
7 def setUp(self):
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')
14 output.write(data)
15 output.close()
17 output = open(self.name_diff, 'a+')
18 output.write('An extra line.\n')
19 output.close()
20 self.dir = tempfile.gettempdir()
22 def tearDown(self):
23 os.unlink(self.name)
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):
44 def setUp(self):
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)
53 os.mkdir(dir)
54 if self.caseinsensitive and dir is self.dir_same:
55 fn = 'FiLe' # Verify case-insensitive comparison
56 else:
57 fn = 'file'
58 output = open(os.path.join(dir, fn), 'w')
59 output.write(data)
60 output.close()
62 output = open(os.path.join(self.dir_diff, 'file2'), 'w')
63 output.write('An extra file.\n')
64 output.close()
66 def tearDown(self):
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']) ==
73 (['file'], [], []),
74 "Comparing directory to itself fails")
75 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) ==
76 (['file'], [], []),
77 "Comparing directory to same fails")
79 # Try it with shallow=False
80 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'],
81 shallow=False) ==
82 (['file'], [], []),
83 "Comparing directory to itself fails")
84 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'],
85 shallow=False),
86 "Comparing directory to same fails")
88 # Add different file2
89 output = open(os.path.join(self.dir, 'file2'), 'w')
90 output.write('Different contents.\n')
91 output.close()
93 self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same,
94 ['file', 'file2']) ==
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']])
104 else:
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')
124 output.close()
125 d = filecmp.dircmp(self.dir, self.dir_diff)
126 self.assertTrue(d.same_files == ['file'])
127 self.assertTrue(d.diff_files == ['file2'])
130 def test_main():
131 test_support.run_unittest(FileCompareTestCase, DirCompareTestCase)
133 if __name__ == "__main__":
134 test_main()