Georg Brandl | 1b37e87 | 2010-03-14 10:45:50 +0000 | [diff] [blame] | 1 | import os, filecmp, shutil, tempfile |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 2 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 4 | |
| 5 | class FileCompareTestCase(unittest.TestCase): |
| 6 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | self.name = support.TESTFN |
| 8 | self.name_same = support.TESTFN + '-same' |
| 9 | self.name_diff = support.TESTFN + '-diff' |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 10 | 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 27 | self.assertTrue(filecmp.cmp(self.name, self.name_same), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 28 | "Comparing file to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 29 | self.assertTrue(filecmp.cmp(self.name, self.name_same, shallow=False), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 30 | "Comparing file to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 31 | self.assertTrue(filecmp.cmp(self.name, self.name, shallow=False), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 32 | "Comparing file to identical file fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 33 | self.assertTrue(filecmp.cmp(self.name, self.name), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 34 | "Comparing file to identical file fails") |
| 35 | |
| 36 | def test_different(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 37 | self.assertFalse(filecmp.cmp(self.name, self.name_diff), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 38 | "Mismatched files compare as equal") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 39 | self.assertFalse(filecmp.cmp(self.name, self.dir), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 40 | "File and directory compare as equal") |
| 41 | |
Ned Deily | 7bff3cb | 2013-06-14 15:19:11 -0700 | [diff] [blame^] | 42 | 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. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 49 | class 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 Bendersky | eb2884a | 2013-01-12 06:13:32 -0800 | [diff] [blame] | 55 | |
| 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 Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 60 | self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a') |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 61 | data = 'Contents of file go here.\n' |
Eli Bendersky | eb2884a | 2013-01-12 06:13:32 -0800 | [diff] [blame] | 62 | for dir in (self.dir, self.dir_same, self.dir_diff, self.dir_ignored): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | shutil.rmtree(dir, True) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 64 | os.mkdir(dir) |
Raymond Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 65 | if self.caseinsensitive and dir is self.dir_same: |
Raymond Hettinger | eeca37e | 2003-09-02 05:42:02 +0000 | [diff] [blame] | 66 | fn = 'FiLe' # Verify case-insensitive comparison |
| 67 | else: |
| 68 | fn = 'file' |
| 69 | output = open(os.path.join(dir, fn), 'w') |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 70 | 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 Bendersky | eb2884a | 2013-01-12 06:13:32 -0800 | [diff] [blame] | 78 | 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. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 83 | |
| 84 | def test_cmpfiles(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 85 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) == |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 86 | (['file'], [], []), |
| 87 | "Comparing directory to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 88 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) == |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 89 | (['file'], [], []), |
| 90 | "Comparing directory to same fails") |
| 91 | |
| 92 | # Try it with shallow=False |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 93 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'], |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 94 | shallow=False) == |
| 95 | (['file'], [], []), |
| 96 | "Comparing directory to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 97 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'], |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 98 | 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 106 | self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same, |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 107 | ['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 Murray | 2b209cd | 2012-08-14 21:40:13 -0400 | [diff] [blame] | 114 | 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 Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 118 | 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 Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 122 | self.assertEqual(d.common, ['file']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 123 | self.assertTrue(d.left_only == d.right_only == []) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 124 | self.assertEqual(d.same_files, ['file']) |
| 125 | self.assertEqual(d.diff_files, []) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 126 | |
| 127 | # Check attributes for comparison of two different directories |
R David Murray | 2b209cd | 2012-08-14 21:40:13 -0400 | [diff] [blame] | 128 | 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 Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 132 | self.assertEqual(d.left_list, ['file']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 133 | self.assertTrue(d.right_list == ['file', 'file2']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 134 | 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. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 139 | |
| 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 Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 145 | self.assertEqual(d.same_files, ['file']) |
| 146 | self.assertEqual(d.diff_files, ['file2']) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 150 | support.run_unittest(FileCompareTestCase, DirCompareTestCase) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 151 | |
| 152 | if __name__ == "__main__": |
| 153 | test_main() |