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