Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 1 | |
Georg Brandl | 1b37e87 | 2010-03-14 10:45:50 +0000 | [diff] [blame^] | 2 | import os, filecmp, shutil, tempfile |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 5 | |
| 6 | class FileCompareTestCase(unittest.TestCase): |
| 7 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | self.name = support.TESTFN |
| 9 | self.name_same = support.TESTFN + '-same' |
| 10 | self.name_diff = support.TESTFN + '-diff' |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 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() |
| 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 28 | self.assertTrue(filecmp.cmp(self.name, self.name_same), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 29 | "Comparing file to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 30 | self.assertTrue(filecmp.cmp(self.name, self.name_same, shallow=False), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 31 | "Comparing file to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 32 | self.assertTrue(filecmp.cmp(self.name, self.name, shallow=False), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 33 | "Comparing file to identical file fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 34 | self.assertTrue(filecmp.cmp(self.name, self.name), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 35 | "Comparing file to identical file fails") |
| 36 | |
| 37 | def test_different(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 38 | self.assertFalse(filecmp.cmp(self.name, self.name_diff), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 39 | "Mismatched files compare as equal") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 40 | self.assertFalse(filecmp.cmp(self.name, self.dir), |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 41 | "File and directory compare as equal") |
| 42 | |
| 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') |
Raymond Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 49 | self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a') |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 50 | data = 'Contents of file go here.\n' |
| 51 | for dir in [self.dir, self.dir_same, self.dir_diff]: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | shutil.rmtree(dir, True) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 53 | os.mkdir(dir) |
Raymond Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 54 | if self.caseinsensitive and dir is self.dir_same: |
Raymond Hettinger | eeca37e | 2003-09-02 05:42:02 +0000 | [diff] [blame] | 55 | fn = 'FiLe' # Verify case-insensitive comparison |
| 56 | else: |
| 57 | fn = 'file' |
| 58 | output = open(os.path.join(dir, fn), 'w') |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 59 | 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 72 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) == |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 73 | (['file'], [], []), |
| 74 | "Comparing directory to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 75 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) == |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 76 | (['file'], [], []), |
| 77 | "Comparing directory to same fails") |
| 78 | |
| 79 | # Try it with shallow=False |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 80 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'], |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 81 | shallow=False) == |
| 82 | (['file'], [], []), |
| 83 | "Comparing directory to itself fails") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 84 | self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'], |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 85 | 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 93 | self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same, |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 94 | ['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 |
| 101 | d = filecmp.dircmp(self.dir, self.dir_same) |
Raymond Hettinger | f70e076 | 2003-09-02 06:59:21 +0000 | [diff] [blame] | 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']]) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 106 | self.assertEqual(d.common, ['file']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 107 | self.assertTrue(d.left_only == d.right_only == []) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 108 | self.assertEqual(d.same_files, ['file']) |
| 109 | self.assertEqual(d.diff_files, []) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 110 | |
| 111 | # Check attributes for comparison of two different directories |
| 112 | d = filecmp.dircmp(self.dir, self.dir_diff) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 113 | self.assertEqual(d.left_list, ['file']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 114 | self.assertTrue(d.right_list == ['file', 'file2']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 115 | self.assertEqual(d.common, ['file']) |
| 116 | self.assertEqual(d.left_only, []) |
| 117 | self.assertEqual(d.right_only, ['file2']) |
| 118 | self.assertEqual(d.same_files, ['file']) |
| 119 | self.assertEqual(d.diff_files, []) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 120 | |
| 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) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 126 | self.assertEqual(d.same_files, ['file']) |
| 127 | self.assertEqual(d.diff_files, ['file2']) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 131 | support.run_unittest(FileCompareTestCase, DirCompareTestCase) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 132 | |
| 133 | if __name__ == "__main__": |
| 134 | test_main() |