blob: c7cb3fcf3677cc71a14b42ed51e4740f389c6986 [file] [log] [blame]
Georg Brandl1b37e872010-03-14 10:45:50 +00001import os, filecmp, shutil, tempfile
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +00002import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +00004
5class FileCompareTestCase(unittest.TestCase):
6 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007 self.name = support.TESTFN
8 self.name_same = support.TESTFN + '-same'
9 self.name_diff = support.TESTFN + '-diff'
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000010 data = 'Contents of file go here.\n'
11 for name in [self.name, self.name_same, self.name_diff]:
12 output = open(name, 'w')
13 output.write(data)
14 output.close()
15
16 output = open(self.name_diff, 'a+')
17 output.write('An extra line.\n')
18 output.close()
19 self.dir = tempfile.gettempdir()
20
21 def tearDown(self):
22 os.unlink(self.name)
23 os.unlink(self.name_same)
24 os.unlink(self.name_diff)
25
26 def test_matching(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000027 self.assertTrue(filecmp.cmp(self.name, self.name_same),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000028 "Comparing file to itself fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000029 self.assertTrue(filecmp.cmp(self.name, self.name_same, shallow=False),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000030 "Comparing file to itself fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000031 self.assertTrue(filecmp.cmp(self.name, self.name, shallow=False),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000032 "Comparing file to identical file fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000033 self.assertTrue(filecmp.cmp(self.name, self.name),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000034 "Comparing file to identical file fails")
35
36 def test_different(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000037 self.assertFalse(filecmp.cmp(self.name, self.name_diff),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000038 "Mismatched files compare as equal")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000039 self.assertFalse(filecmp.cmp(self.name, self.dir),
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000040 "File and directory compare as equal")
41
Ned Deily7bff3cb2013-06-14 15:19:11 -070042 def test_cache_clear(self):
43 first_compare = filecmp.cmp(self.name, self.name_same, shallow=False)
44 second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False)
45 filecmp.clear_cache()
46 self.assertTrue(len(filecmp._cache) == 0,
47 "Cache not cleared after calling clear_cache")
48
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000049class DirCompareTestCase(unittest.TestCase):
50 def setUp(self):
51 tmpdir = tempfile.gettempdir()
52 self.dir = os.path.join(tmpdir, 'dir')
53 self.dir_same = os.path.join(tmpdir, 'dir-same')
54 self.dir_diff = os.path.join(tmpdir, 'dir-diff')
Eli Benderskyeb2884a2013-01-12 06:13:32 -080055
56 # Another dir is created under dir_same, but it has a name from the
57 # ignored list so it should not affect testing results.
58 self.dir_ignored = os.path.join(self.dir_same, '.hg')
59
Raymond Hettingerf70e0762003-09-02 06:59:21 +000060 self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000061 data = 'Contents of file go here.\n'
Eli Benderskyeb2884a2013-01-12 06:13:32 -080062 for dir in (self.dir, self.dir_same, self.dir_diff, self.dir_ignored):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063 shutil.rmtree(dir, True)
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000064 os.mkdir(dir)
Raymond Hettingerf70e0762003-09-02 06:59:21 +000065 if self.caseinsensitive and dir is self.dir_same:
Raymond Hettingereeca37e2003-09-02 05:42:02 +000066 fn = 'FiLe' # Verify case-insensitive comparison
67 else:
68 fn = 'file'
69 output = open(os.path.join(dir, fn), 'w')
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000070 output.write(data)
71 output.close()
72
73 output = open(os.path.join(self.dir_diff, 'file2'), 'w')
74 output.write('An extra file.\n')
75 output.close()
76
77 def tearDown(self):
Eli Benderskyeb2884a2013-01-12 06:13:32 -080078 for dir in (self.dir, self.dir_same, self.dir_diff):
79 shutil.rmtree(dir)
80
81 def test_default_ignores(self):
82 self.assertIn('.hg', filecmp.DEFAULT_IGNORES)
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000083
84 def test_cmpfiles(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000085 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) ==
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000086 (['file'], [], []),
87 "Comparing directory to itself fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000088 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) ==
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000089 (['file'], [], []),
90 "Comparing directory to same fails")
91
92 # Try it with shallow=False
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000093 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'],
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000094 shallow=False) ==
95 (['file'], [], []),
96 "Comparing directory to itself fails")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000097 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'],
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +000098 shallow=False),
99 "Comparing directory to same fails")
100
101 # Add different file2
102 output = open(os.path.join(self.dir, 'file2'), 'w')
103 output.write('Different contents.\n')
104 output.close()
105
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000106 self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same,
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000107 ['file', 'file2']) ==
108 (['file'], ['file2'], []),
109 "Comparing mismatched directories fails")
110
111
112 def test_dircmp(self):
113 # Check attributes for comparison of two identical directories
R David Murray2b209cd2012-08-14 21:40:13 -0400114 left_dir, right_dir = self.dir, self.dir_same
115 d = filecmp.dircmp(left_dir, right_dir)
116 self.assertEqual(d.left, left_dir)
117 self.assertEqual(d.right, right_dir)
Raymond Hettingerf70e0762003-09-02 06:59:21 +0000118 if self.caseinsensitive:
119 self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']])
120 else:
121 self.assertEqual([d.left_list, d.right_list],[['file'], ['file']])
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000122 self.assertEqual(d.common, ['file'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000123 self.assertTrue(d.left_only == d.right_only == [])
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000124 self.assertEqual(d.same_files, ['file'])
125 self.assertEqual(d.diff_files, [])
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000126
127 # Check attributes for comparison of two different directories
R David Murray2b209cd2012-08-14 21:40:13 -0400128 left_dir, right_dir = self.dir, self.dir_diff
129 d = filecmp.dircmp(left_dir, right_dir)
130 self.assertEqual(d.left, left_dir)
131 self.assertEqual(d.right, right_dir)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000132 self.assertEqual(d.left_list, ['file'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000133 self.assertTrue(d.right_list == ['file', 'file2'])
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000134 self.assertEqual(d.common, ['file'])
135 self.assertEqual(d.left_only, [])
136 self.assertEqual(d.right_only, ['file2'])
137 self.assertEqual(d.same_files, ['file'])
138 self.assertEqual(d.diff_files, [])
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000139
140 # Add different file2
141 output = open(os.path.join(self.dir, 'file2'), 'w')
142 output.write('Different contents.\n')
143 output.close()
144 d = filecmp.dircmp(self.dir, self.dir_diff)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000145 self.assertEqual(d.same_files, ['file'])
146 self.assertEqual(d.diff_files, ['file2'])
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000147
148
149def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000150 support.run_unittest(FileCompareTestCase, DirCompareTestCase)
Andrew M. Kuchlinge63846d2003-02-06 17:42:45 +0000151
152if __name__ == "__main__":
153 test_main()