PyXR

c:\python24\lib \ test \ test_filecmp.py



0001 
0002 import os, filecmp, shutil, tempfile
0003 import unittest
0004 from test import test_support
0005 
0006 class FileCompareTestCase(unittest.TestCase):
0007     def setUp(self):
0008         self.name = test_support.TESTFN
0009         self.name_same = test_support.TESTFN + '-same'
0010         self.name_diff = test_support.TESTFN + '-diff'
0011         data = 'Contents of file go here.\n'
0012         for name in [self.name, self.name_same, self.name_diff]:
0013             output = open(name, 'w')
0014             output.write(data)
0015             output.close()
0016 
0017         output = open(self.name_diff, 'a+')
0018         output.write('An extra line.\n')
0019         output.close()
0020         self.dir = tempfile.gettempdir()
0021 
0022     def tearDown(self):
0023         os.unlink(self.name)
0024         os.unlink(self.name_same)
0025         os.unlink(self.name_diff)
0026 
0027     def test_matching(self):
0028         self.failUnless(filecmp.cmp(self.name, self.name_same),
0029                         "Comparing file to itself fails")
0030         self.failUnless(filecmp.cmp(self.name, self.name_same, shallow=False),
0031                         "Comparing file to itself fails")
0032         self.failUnless(filecmp.cmp(self.name, self.name, shallow=False),
0033                         "Comparing file to identical file fails")
0034         self.failUnless(filecmp.cmp(self.name, self.name),
0035                         "Comparing file to identical file fails")
0036 
0037     def test_different(self):
0038         self.failIf(filecmp.cmp(self.name, self.name_diff),
0039                     "Mismatched files compare as equal")
0040         self.failIf(filecmp.cmp(self.name, self.dir),
0041                     "File and directory compare as equal")
0042 
0043 class DirCompareTestCase(unittest.TestCase):
0044     def setUp(self):
0045         tmpdir = tempfile.gettempdir()
0046         self.dir = os.path.join(tmpdir, 'dir')
0047         self.dir_same = os.path.join(tmpdir, 'dir-same')
0048         self.dir_diff = os.path.join(tmpdir, 'dir-diff')
0049         self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
0050         data = 'Contents of file go here.\n'
0051         for dir in [self.dir, self.dir_same, self.dir_diff]:
0052             os.mkdir(dir)
0053             if self.caseinsensitive and dir is self.dir_same:
0054                 fn = 'FiLe'     # Verify case-insensitive comparison
0055             else:
0056                 fn = 'file'
0057             output = open(os.path.join(dir, fn), 'w')
0058             output.write(data)
0059             output.close()
0060 
0061         output = open(os.path.join(self.dir_diff, 'file2'), 'w')
0062         output.write('An extra file.\n')
0063         output.close()
0064 
0065     def tearDown(self):
0066         shutil.rmtree(self.dir)
0067         shutil.rmtree(self.dir_same)
0068         shutil.rmtree(self.dir_diff)
0069 
0070     def test_cmpfiles(self):
0071         self.failUnless(filecmp.cmpfiles(self.dir, self.dir, ['file']) ==
0072                         (['file'], [], []),
0073                         "Comparing directory to itself fails")
0074         self.failUnless(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) ==
0075                         (['file'], [], []),
0076                         "Comparing directory to same fails")
0077 
0078         # Try it with shallow=False
0079         self.failUnless(filecmp.cmpfiles(self.dir, self.dir, ['file'],
0080                                          shallow=False) ==
0081                         (['file'], [], []),
0082                         "Comparing directory to itself fails")
0083         self.failUnless(filecmp.cmpfiles(self.dir, self.dir_same, ['file'],
0084                                          shallow=False),
0085                         "Comparing directory to same fails")
0086 
0087         # Add different file2
0088         output = open(os.path.join(self.dir, 'file2'), 'w')
0089         output.write('Different contents.\n')
0090         output.close()
0091 
0092         self.failIf(filecmp.cmpfiles(self.dir, self.dir_same,
0093                                      ['file', 'file2']) ==
0094                     (['file'], ['file2'], []),
0095                     "Comparing mismatched directories fails")
0096 
0097 
0098     def test_dircmp(self):
0099         # Check attributes for comparison of two identical directories
0100         d = filecmp.dircmp(self.dir, self.dir_same)
0101         if self.caseinsensitive:
0102             self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']])
0103         else:
0104             self.assertEqual([d.left_list, d.right_list],[['file'], ['file']])
0105         self.failUnless(d.common == ['file'])
0106         self.failUnless(d.left_only == d.right_only == [])
0107         self.failUnless(d.same_files == ['file'])
0108         self.failUnless(d.diff_files == [])
0109 
0110         # Check attributes for comparison of two different directories
0111         d = filecmp.dircmp(self.dir, self.dir_diff)
0112         self.failUnless(d.left_list == ['file'])
0113         self.failUnless(d.right_list == ['file', 'file2'])
0114         self.failUnless(d.common == ['file'])
0115         self.failUnless(d.left_only == [])
0116         self.failUnless(d.right_only == ['file2'])
0117         self.failUnless(d.same_files == ['file'])
0118         self.failUnless(d.diff_files == [])
0119 
0120         # Add different file2
0121         output = open(os.path.join(self.dir, 'file2'), 'w')
0122         output.write('Different contents.\n')
0123         output.close()
0124         d = filecmp.dircmp(self.dir, self.dir_diff)
0125         self.failUnless(d.same_files == ['file'])
0126         self.failUnless(d.diff_files == ['file2'])
0127 
0128 
0129 def test_main():
0130     test_support.run_unittest(FileCompareTestCase, DirCompareTestCase)
0131 
0132 if __name__ == "__main__":
0133     test_main()
0134 

Generated by PyXR 0.9.4
SourceForge.net Logo