1 2import os, filecmp, shutil, tempfile 3import unittest 4from test import test_support 5 6class 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.assertTrue(filecmp.cmp(self.name, self.name_same), 29 "Comparing file to itself fails") 30 self.assertTrue(filecmp.cmp(self.name, self.name_same, shallow=False), 31 "Comparing file to itself fails") 32 self.assertTrue(filecmp.cmp(self.name, self.name, shallow=False), 33 "Comparing file to identical file fails") 34 self.assertTrue(filecmp.cmp(self.name, self.name), 35 "Comparing file to identical file fails") 36 37 def test_different(self): 38 self.assertFalse(filecmp.cmp(self.name, self.name_diff), 39 "Mismatched files compare as equal") 40 self.assertFalse(filecmp.cmp(self.name, self.dir), 41 "File and directory compare as equal") 42 43class 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 self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a') 50 data = 'Contents of file go here.\n' 51 for dir in [self.dir, self.dir_same, self.dir_diff]: 52 shutil.rmtree(dir, True) 53 os.mkdir(dir) 54 if self.caseinsensitive and dir is self.dir_same: 55 fn = 'FiLe' # Verify case-insensitive comparison 56 else: 57 fn = 'file' 58 output = open(os.path.join(dir, fn), 'w') 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): 72 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) == 73 (['file'], [], []), 74 "Comparing directory to itself fails") 75 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) == 76 (['file'], [], []), 77 "Comparing directory to same fails") 78 79 # Try it with shallow=False 80 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'], 81 shallow=False) == 82 (['file'], [], []), 83 "Comparing directory to itself fails") 84 self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'], 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 93 self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same, 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) 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']]) 106 self.assertTrue(d.common == ['file']) 107 self.assertTrue(d.left_only == d.right_only == []) 108 self.assertTrue(d.same_files == ['file']) 109 self.assertTrue(d.diff_files == []) 110 111 # Check attributes for comparison of two different directories 112 d = filecmp.dircmp(self.dir, self.dir_diff) 113 self.assertTrue(d.left_list == ['file']) 114 self.assertTrue(d.right_list == ['file', 'file2']) 115 self.assertTrue(d.common == ['file']) 116 self.assertTrue(d.left_only == []) 117 self.assertTrue(d.right_only == ['file2']) 118 self.assertTrue(d.same_files == ['file']) 119 self.assertTrue(d.diff_files == []) 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) 126 self.assertTrue(d.same_files == ['file']) 127 self.assertTrue(d.diff_files == ['file2']) 128 129 130def test_main(): 131 test_support.run_unittest(FileCompareTestCase, DirCompareTestCase) 132 133if __name__ == "__main__": 134 test_main() 135