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 | |
| 42 | class DirCompareTestCase(unittest.TestCase): |
| 43 | def setUp(self): |
| 44 | tmpdir = tempfile.gettempdir() |
| 45 | self.dir = os.path.join(tmpdir, 'dir') |
| 46 | self.dir_same = os.path.join(tmpdir, 'dir-same') |
| 47 | self.dir_diff = os.path.join(tmpdir, 'dir-diff') |
Eli Bendersky | eb2884a | 2013-01-12 06:13:32 -0800 | [diff] [blame^] | 48 | |
| 49 | # Another dir is created under dir_same, but it has a name from the |
| 50 | # ignored list so it should not affect testing results. |
| 51 | self.dir_ignored = os.path.join(self.dir_same, '.hg') |
| 52 | |
Raymond Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 53 | self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a') |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 54 | data = 'Contents of file go here.\n' |
Eli Bendersky | eb2884a | 2013-01-12 06:13:32 -0800 | [diff] [blame^] | 55 | 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] | 56 | shutil.rmtree(dir, True) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 57 | os.mkdir(dir) |
Raymond Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 58 | if self.caseinsensitive and dir is self.dir_same: |
Raymond Hettinger | eeca37e | 2003-09-02 05:42:02 +0000 | [diff] [blame] | 59 | fn = 'FiLe' # Verify case-insensitive comparison |
| 60 | else: |
| 61 | fn = 'file' |
| 62 | output = open(os.path.join(dir, fn), 'w') |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 63 | output.write(data) |
| 64 | output.close() |
| 65 | |
| 66 | output = open(os.path.join(self.dir_diff, 'file2'), 'w') |
| 67 | output.write('An extra file.\n') |
| 68 | output.close() |
| 69 | |
| 70 | def tearDown(self): |
Eli Bendersky | eb2884a | 2013-01-12 06:13:32 -0800 | [diff] [blame^] | 71 | for dir in (self.dir, self.dir_same, self.dir_diff): |
| 72 | shutil.rmtree(dir) |
| 73 | |
| 74 | def test_default_ignores(self): |
| 75 | self.assertIn('.hg', filecmp.DEFAULT_IGNORES) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 76 | |
| 77 | def test_cmpfiles(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 78 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) == |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 79 | (['file'], [], []), |
| 80 | "Comparing directory to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 81 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) == |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 82 | (['file'], [], []), |
| 83 | "Comparing directory to same fails") |
| 84 | |
| 85 | # Try it with shallow=False |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 86 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'], |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 87 | shallow=False) == |
| 88 | (['file'], [], []), |
| 89 | "Comparing directory to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 90 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'], |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 91 | shallow=False), |
| 92 | "Comparing directory to same fails") |
| 93 | |
| 94 | # Add different file2 |
| 95 | output = open(os.path.join(self.dir, 'file2'), 'w') |
| 96 | output.write('Different contents.\n') |
| 97 | output.close() |
| 98 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 99 | self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same, |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 100 | ['file', 'file2']) == |
| 101 | (['file'], ['file2'], []), |
| 102 | "Comparing mismatched directories fails") |
| 103 | |
| 104 | |
| 105 | def test_dircmp(self): |
| 106 | # Check attributes for comparison of two identical directories |
R David Murray | 2b209cd | 2012-08-14 21:40:13 -0400 | [diff] [blame] | 107 | left_dir, right_dir = self.dir, self.dir_same |
| 108 | d = filecmp.dircmp(left_dir, right_dir) |
| 109 | self.assertEqual(d.left, left_dir) |
| 110 | self.assertEqual(d.right, right_dir) |
Raymond Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 111 | if self.caseinsensitive: |
| 112 | self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']]) |
| 113 | else: |
| 114 | self.assertEqual([d.left_list, d.right_list],[['file'], ['file']]) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 115 | self.assertEqual(d.common, ['file']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 116 | self.assertTrue(d.left_only == d.right_only == []) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 117 | self.assertEqual(d.same_files, ['file']) |
| 118 | self.assertEqual(d.diff_files, []) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 119 | |
| 120 | # Check attributes for comparison of two different directories |
R David Murray | 2b209cd | 2012-08-14 21:40:13 -0400 | [diff] [blame] | 121 | left_dir, right_dir = self.dir, self.dir_diff |
| 122 | d = filecmp.dircmp(left_dir, right_dir) |
| 123 | self.assertEqual(d.left, left_dir) |
| 124 | self.assertEqual(d.right, right_dir) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 125 | self.assertEqual(d.left_list, ['file']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 126 | self.assertTrue(d.right_list == ['file', 'file2']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 127 | self.assertEqual(d.common, ['file']) |
| 128 | self.assertEqual(d.left_only, []) |
| 129 | self.assertEqual(d.right_only, ['file2']) |
| 130 | self.assertEqual(d.same_files, ['file']) |
| 131 | self.assertEqual(d.diff_files, []) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 132 | |
| 133 | # Add different file2 |
| 134 | output = open(os.path.join(self.dir, 'file2'), 'w') |
| 135 | output.write('Different contents.\n') |
| 136 | output.close() |
| 137 | d = filecmp.dircmp(self.dir, self.dir_diff) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 138 | self.assertEqual(d.same_files, ['file']) |
| 139 | self.assertEqual(d.diff_files, ['file2']) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 143 | support.run_unittest(FileCompareTestCase, DirCompareTestCase) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 144 | |
| 145 | if __name__ == "__main__": |
| 146 | test_main() |