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