blob: 09599794ad9d178f33ad44f8fd87f514cba4aefe [file] [log] [blame]
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +00001
Georg Brandl1b37e872010-03-14 10:45:50 +00002import os, filecmp, shutil, tempfile
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +00003import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +00005
6class FileCompareTestCase(unittest.TestCase):
7 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008 self.name = support.TESTFN
9 self.name_same = support.TESTFN + '-same'
10 self.name_diff = support.TESTFN + '-diff'
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000011 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()
16
17 output = open(self.name_diff, 'a+')
18 output.write('An extra line.\n')
19 output.close()
20 self.dir = tempfile.gettempdir()
21
22 def tearDown(self):
23 os.unlink(self.name)
24 os.unlink(self.name_same)
25 os.unlink(self.name_diff)
26
27 def test_matching(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000028 self.assertTrue(filecmp.cmp(self.name, self.name_same),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000029 "Comparing file to itself fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000030 self.assertTrue(filecmp.cmp(self.name, self.name_same, shallow=False),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000031 "Comparing file to itself fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000032 self.assertTrue(filecmp.cmp(self.name, self.name, shallow=False),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000033 "Comparing file to identical file fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000034 self.assertTrue(filecmp.cmp(self.name, self.name),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000035 "Comparing file to identical file fails")
36
37 def test_different(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000038 self.assertFalse(filecmp.cmp(self.name, self.name_diff),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000039 "Mismatched files compare as equal")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000040 self.assertFalse(filecmp.cmp(self.name, self.dir),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000041 "File and directory compare as equal")
42
43class 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')
Raymond Hettingerf70e0762003-09-02 06:59:21 +000049 self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000050 data = 'Contents of file go here.\n'
51 for dir in [self.dir, self.dir_same, self.dir_diff]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 shutil.rmtree(dir, True)
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000053 os.mkdir(dir)
Raymond Hettingerf70e0762003-09-02 06:59:21 +000054 if self.caseinsensitive and dir is self.dir_same:
Raymond Hettingereeca37e2003-09-02 05:42:02 +000055 fn = 'FiLe' # Verify case-insensitive comparison
56 else:
57 fn = 'file'
58 output = open(os.path.join(dir, fn), 'w')
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000059 output.write(data)
60 output.close()
61
62 output = open(os.path.join(self.dir_diff, 'file2'), 'w')
63 output.write('An extra file.\n')
64 output.close()
65
66 def tearDown(self):
67 shutil.rmtree(self.dir)
68 shutil.rmtree(self.dir_same)
69 shutil.rmtree(self.dir_diff)
70
71 def test_cmpfiles(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000072 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) ==
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000073 (['file'], [], []),
74 "Comparing directory to itself fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000075 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) ==
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000076 (['file'], [], []),
77 "Comparing directory to same fails")
78
79 # Try it with shallow=False
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000080 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'],
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000081 shallow=False) ==
82 (['file'], [], []),
83 "Comparing directory to itself fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000084 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'],
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000085 shallow=False),
86 "Comparing directory to same fails")
87
88 # Add different file2
89 output = open(os.path.join(self.dir, 'file2'), 'w')
90 output.write('Different contents.\n')
91 output.close()
92
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000093 self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same,
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000094 ['file', 'file2']) ==
95 (['file'], ['file2'], []),
96 "Comparing mismatched directories fails")
97
98
99 def test_dircmp(self):
100 # Check attributes for comparison of two identical directories
R David Murray2b209cd2012-08-14 21:40:13 -0400101 left_dir, right_dir = self.dir, self.dir_same
102 d = filecmp.dircmp(left_dir, right_dir)
103 self.assertEqual(d.left, left_dir)
104 self.assertEqual(d.right, right_dir)
Raymond Hettingerf70e0762003-09-02 06:59:21 +0000105 if self.caseinsensitive:
106 self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']])
107 else:
108 self.assertEqual([d.left_list, d.right_list],[['file'], ['file']])
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000109 self.assertEqual(d.common, ['file'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000110 self.assertTrue(d.left_only == d.right_only == [])
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000111 self.assertEqual(d.same_files, ['file'])
112 self.assertEqual(d.diff_files, [])
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000113
114 # Check attributes for comparison of two different directories
R David Murray2b209cd2012-08-14 21:40:13 -0400115 left_dir, right_dir = self.dir, self.dir_diff
116 d = filecmp.dircmp(left_dir, right_dir)
117 self.assertEqual(d.left, left_dir)
118 self.assertEqual(d.right, right_dir)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000119 self.assertEqual(d.left_list, ['file'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000120 self.assertTrue(d.right_list == ['file', 'file2'])
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000121 self.assertEqual(d.common, ['file'])
122 self.assertEqual(d.left_only, [])
123 self.assertEqual(d.right_only, ['file2'])
124 self.assertEqual(d.same_files, ['file'])
125 self.assertEqual(d.diff_files, [])
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000126
127 # Add different file2
128 output = open(os.path.join(self.dir, 'file2'), 'w')
129 output.write('Different contents.\n')
130 output.close()
131 d = filecmp.dircmp(self.dir, self.dir_diff)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000132 self.assertEqual(d.same_files, ['file'])
133 self.assertEqual(d.diff_files, ['file2'])
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000134
135
136def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000137 support.run_unittest(FileCompareTestCase, DirCompareTestCase)
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000138
139if __name__ == "__main__":
140 test_main()