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