Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 1 | |
| 2 | import os, filecmp, shutil, tempfile |
| 3 | import unittest |
| 4 | from test import test_support |
| 5 | |
| 6 | class FileCompareTestCase(unittest.TestCase): |
| 7 | def setUp(self): |
| 8 | self.name = test_support.TESTFN |
| 9 | self.name_same = test_support.TESTFN + '-same' |
| 10 | self.name_diff = test_support.TESTFN + '-diff' |
| 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): |
| 28 | self.failUnless(filecmp.cmp(self.name, self.name_same), |
| 29 | "Comparing file to itself fails") |
| 30 | self.failUnless(filecmp.cmp(self.name, self.name_same, shallow=False), |
| 31 | "Comparing file to itself fails") |
| 32 | self.failUnless(filecmp.cmp(self.name, self.name, shallow=False), |
| 33 | "Comparing file to identical file fails") |
| 34 | self.failUnless(filecmp.cmp(self.name, self.name), |
| 35 | "Comparing file to identical file fails") |
| 36 | |
| 37 | def test_different(self): |
| 38 | self.failIf(filecmp.cmp(self.name, self.name_diff), |
| 39 | "Mismatched files compare as equal") |
| 40 | self.failIf(filecmp.cmp(self.name, self.dir), |
| 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') |
| 49 | data = 'Contents of file go here.\n' |
| 50 | for dir in [self.dir, self.dir_same, self.dir_diff]: |
| 51 | os.mkdir(dir) |
Raymond Hettinger | eeca37e | 2003-09-02 05:42:02 +0000 | [diff] [blame^] | 52 | if dir is self.dir_same: |
| 53 | fn = 'FiLe' # Verify case-insensitive comparison |
| 54 | else: |
| 55 | fn = 'file' |
| 56 | output = open(os.path.join(dir, fn), 'w') |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 57 | output.write(data) |
| 58 | output.close() |
| 59 | |
| 60 | output = open(os.path.join(self.dir_diff, 'file2'), 'w') |
| 61 | output.write('An extra file.\n') |
| 62 | output.close() |
| 63 | |
| 64 | def tearDown(self): |
| 65 | shutil.rmtree(self.dir) |
| 66 | shutil.rmtree(self.dir_same) |
| 67 | shutil.rmtree(self.dir_diff) |
| 68 | |
| 69 | def test_cmpfiles(self): |
| 70 | self.failUnless(filecmp.cmpfiles(self.dir, self.dir, ['file']) == |
| 71 | (['file'], [], []), |
| 72 | "Comparing directory to itself fails") |
| 73 | self.failUnless(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) == |
| 74 | (['file'], [], []), |
| 75 | "Comparing directory to same fails") |
| 76 | |
| 77 | # Try it with shallow=False |
| 78 | self.failUnless(filecmp.cmpfiles(self.dir, self.dir, ['file'], |
| 79 | shallow=False) == |
| 80 | (['file'], [], []), |
| 81 | "Comparing directory to itself fails") |
| 82 | self.failUnless(filecmp.cmpfiles(self.dir, self.dir_same, ['file'], |
| 83 | shallow=False), |
| 84 | "Comparing directory to same fails") |
| 85 | |
| 86 | # Add different file2 |
| 87 | output = open(os.path.join(self.dir, 'file2'), 'w') |
| 88 | output.write('Different contents.\n') |
| 89 | output.close() |
| 90 | |
| 91 | self.failIf(filecmp.cmpfiles(self.dir, self.dir_same, |
| 92 | ['file', 'file2']) == |
| 93 | (['file'], ['file2'], []), |
| 94 | "Comparing mismatched directories fails") |
| 95 | |
| 96 | |
| 97 | def test_dircmp(self): |
| 98 | # Check attributes for comparison of two identical directories |
| 99 | d = filecmp.dircmp(self.dir, self.dir_same) |
Raymond Hettinger | eeca37e | 2003-09-02 05:42:02 +0000 | [diff] [blame^] | 100 | self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']]) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 101 | self.failUnless(d.common == ['file']) |
| 102 | self.failUnless(d.left_only == d.right_only == []) |
| 103 | self.failUnless(d.same_files == ['file']) |
| 104 | self.failUnless(d.diff_files == []) |
| 105 | |
| 106 | # Check attributes for comparison of two different directories |
| 107 | d = filecmp.dircmp(self.dir, self.dir_diff) |
| 108 | self.failUnless(d.left_list == ['file']) |
| 109 | self.failUnless(d.right_list == ['file', 'file2']) |
| 110 | self.failUnless(d.common == ['file']) |
| 111 | self.failUnless(d.left_only == []) |
| 112 | self.failUnless(d.right_only == ['file2']) |
| 113 | self.failUnless(d.same_files == ['file']) |
| 114 | self.failUnless(d.diff_files == []) |
| 115 | |
| 116 | # Add different file2 |
| 117 | output = open(os.path.join(self.dir, 'file2'), 'w') |
| 118 | output.write('Different contents.\n') |
| 119 | output.close() |
| 120 | d = filecmp.dircmp(self.dir, self.dir_diff) |
| 121 | self.failUnless(d.same_files == ['file']) |
| 122 | self.failUnless(d.diff_files == ['file2']) |
| 123 | |
| 124 | |
| 125 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 126 | test_support.run_unittest(FileCompareTestCase, DirCompareTestCase) |
Andrew M. Kuchling | e63846d | 2003-02-06 17:42:45 +0000 | [diff] [blame] | 127 | |
| 128 | if __name__ == "__main__": |
| 129 | test_main() |