home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_filecmp.py < prev    next >
Text File  |  2003-12-30  |  5KB  |  134 lines

  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.         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.             os.mkdir(dir)
  53.             if self.caseinsensitive and dir is self.dir_same:
  54.                 fn = 'FiLe'     # Verify case-insensitive comparison
  55.             else:
  56.                 fn = 'file'
  57.             output = open(os.path.join(dir, fn), 'w')
  58.             output.write(data)
  59.             output.close()
  60.  
  61.         output = open(os.path.join(self.dir_diff, 'file2'), 'w')
  62.         output.write('An extra file.\n')
  63.         output.close()
  64.  
  65.     def tearDown(self):
  66.         shutil.rmtree(self.dir)
  67.         shutil.rmtree(self.dir_same)
  68.         shutil.rmtree(self.dir_diff)
  69.  
  70.     def test_cmpfiles(self):
  71.         self.failUnless(filecmp.cmpfiles(self.dir, self.dir, ['file']) ==
  72.                         (['file'], [], []),
  73.                         "Comparing directory to itself fails")
  74.         self.failUnless(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) ==
  75.                         (['file'], [], []),
  76.                         "Comparing directory to same fails")
  77.  
  78.         # Try it with shallow=False
  79.         self.failUnless(filecmp.cmpfiles(self.dir, self.dir, ['file'],
  80.                                          shallow=False) ==
  81.                         (['file'], [], []),
  82.                         "Comparing directory to itself fails")
  83.         self.failUnless(filecmp.cmpfiles(self.dir, self.dir_same, ['file'],
  84.                                          shallow=False),
  85.                         "Comparing directory to same fails")
  86.  
  87.         # Add different file2
  88.         output = open(os.path.join(self.dir, 'file2'), 'w')
  89.         output.write('Different contents.\n')
  90.         output.close()
  91.  
  92.         self.failIf(filecmp.cmpfiles(self.dir, self.dir_same,
  93.                                      ['file', 'file2']) ==
  94.                     (['file'], ['file2'], []),
  95.                     "Comparing mismatched directories fails")
  96.  
  97.  
  98.     def test_dircmp(self):
  99.         # Check attributes for comparison of two identical directories
  100.         d = filecmp.dircmp(self.dir, self.dir_same)
  101.         if self.caseinsensitive:
  102.             self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']])
  103.         else:
  104.             self.assertEqual([d.left_list, d.right_list],[['file'], ['file']])
  105.         self.failUnless(d.common == ['file'])
  106.         self.failUnless(d.left_only == d.right_only == [])
  107.         self.failUnless(d.same_files == ['file'])
  108.         self.failUnless(d.diff_files == [])
  109.  
  110.         # Check attributes for comparison of two different directories
  111.         d = filecmp.dircmp(self.dir, self.dir_diff)
  112.         self.failUnless(d.left_list == ['file'])
  113.         self.failUnless(d.right_list == ['file', 'file2'])
  114.         self.failUnless(d.common == ['file'])
  115.         self.failUnless(d.left_only == [])
  116.         self.failUnless(d.right_only == ['file2'])
  117.         self.failUnless(d.same_files == ['file'])
  118.         self.failUnless(d.diff_files == [])
  119.  
  120.         # Add different file2
  121.         output = open(os.path.join(self.dir, 'file2'), 'w')
  122.         output.write('Different contents.\n')
  123.         output.close()
  124.         d = filecmp.dircmp(self.dir, self.dir_diff)
  125.         self.failUnless(d.same_files == ['file'])
  126.         self.failUnless(d.diff_files == ['file2'])
  127.  
  128.  
  129. def test_main():
  130.     test_support.run_unittest(FileCompareTestCase, DirCompareTestCase)
  131.  
  132. if __name__ == "__main__":
  133.     test_main()
  134.