test_dep_util.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Tests for distutils.dep_util."""
  2. import unittest
  3. import os
  4. from distutils.dep_util import newer, newer_pairwise, newer_group
  5. from distutils.errors import DistutilsFileError
  6. from distutils.tests import support
  7. from test.support import run_unittest
  8. class DepUtilTestCase(support.TempdirManager, unittest.TestCase):
  9. def test_newer(self):
  10. tmpdir = self.mkdtemp()
  11. new_file = os.path.join(tmpdir, 'new')
  12. old_file = os.path.abspath(__file__)
  13. # Raise DistutilsFileError if 'new_file' does not exist.
  14. self.assertRaises(DistutilsFileError, newer, new_file, old_file)
  15. # Return true if 'new_file' exists and is more recently modified than
  16. # 'old_file', or if 'new_file' exists and 'old_file' doesn't.
  17. self.write_file(new_file)
  18. self.assertTrue(newer(new_file, 'I_dont_exist'))
  19. self.assertTrue(newer(new_file, old_file))
  20. # Return false if both exist and 'old_file' is the same age or younger
  21. # than 'new_file'.
  22. self.assertFalse(newer(old_file, new_file))
  23. def test_newer_pairwise(self):
  24. tmpdir = self.mkdtemp()
  25. sources = os.path.join(tmpdir, 'sources')
  26. targets = os.path.join(tmpdir, 'targets')
  27. os.mkdir(sources)
  28. os.mkdir(targets)
  29. one = os.path.join(sources, 'one')
  30. two = os.path.join(sources, 'two')
  31. three = os.path.abspath(__file__) # I am the old file
  32. four = os.path.join(targets, 'four')
  33. self.write_file(one)
  34. self.write_file(two)
  35. self.write_file(four)
  36. self.assertEqual(newer_pairwise([one, two], [three, four]),
  37. ([one],[three]))
  38. def test_newer_group(self):
  39. tmpdir = self.mkdtemp()
  40. sources = os.path.join(tmpdir, 'sources')
  41. os.mkdir(sources)
  42. one = os.path.join(sources, 'one')
  43. two = os.path.join(sources, 'two')
  44. three = os.path.join(sources, 'three')
  45. old_file = os.path.abspath(__file__)
  46. # return true if 'old_file' is out-of-date with respect to any file
  47. # listed in 'sources'.
  48. self.write_file(one)
  49. self.write_file(two)
  50. self.write_file(three)
  51. self.assertTrue(newer_group([one, two, three], old_file))
  52. self.assertFalse(newer_group([one, two, old_file], three))
  53. # missing handling
  54. os.remove(one)
  55. self.assertRaises(OSError, newer_group, [one, two, old_file], three)
  56. self.assertFalse(newer_group([one, two, old_file], three,
  57. missing='ignore'))
  58. self.assertTrue(newer_group([one, two, old_file], three,
  59. missing='newer'))
  60. def test_suite():
  61. return unittest.makeSuite(DepUtilTestCase)
  62. if __name__ == "__main__":
  63. run_unittest(test_suite())