blob: 3e1c366892d2968928b6653fcb17e8f661ab09ff [file] [log] [blame]
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +00001"""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
Éric Araujo70ec44a2010-11-06 02:44:43 +00009from test.support import run_unittest
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000010
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000011class DepUtilTestCase(support.TempdirManager, unittest.TestCase):
12
13 def test_newer(self):
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000014
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000015 tmpdir = self.mkdtemp()
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000016 new_file = os.path.join(tmpdir, 'new')
17 old_file = os.path.abspath(__file__)
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000018
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000019 # Raise DistutilsFileError if 'new_file' does not exist.
20 self.assertRaises(DistutilsFileError, newer, new_file, old_file)
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000021
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000022 # 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))
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000027
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000028 # 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))
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000031
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')
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000040 three = os.path.abspath(__file__) # I am the old file
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000041 four = os.path.join(targets, 'four')
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000042 self.write_file(one)
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000043 self.write_file(two)
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000044 self.write_file(four)
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000045
Ezio Melottib3aedd42010-11-20 19:04:17 +000046 self.assertEqual(newer_pairwise([one, two], [three, four]),
47 ([one],[three]))
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000048
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')
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000056 old_file = os.path.abspath(__file__)
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000057
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000058 # return true if 'old_file' is out-of-date with respect to any file
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000059 # listed in 'sources'.
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000060 self.write_file(one)
61 self.write_file(two)
62 self.write_file(three)
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000063 self.assertTrue(newer_group([one, two, three], old_file))
64 self.assertFalse(newer_group([one, two, old_file], three))
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000065
66 # missing handling
67 os.remove(one)
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000068 self.assertRaises(OSError, newer_group, [one, two, old_file], three)
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000069
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000070 self.assertFalse(newer_group([one, two, old_file], three,
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000071 missing='ignore'))
72
Tarek Ziadéa90a8a12009-12-10 19:37:05 +000073 self.assertTrue(newer_group([one, two, old_file], three,
Tarek Ziadé7ecb9c62009-12-10 15:35:35 +000074 missing='newer'))
75
76
77def test_suite():
78 return unittest.makeSuite(DepUtilTestCase)
79
80if __name__ == "__main__":
Éric Araujo70ec44a2010-11-06 02:44:43 +000081 run_unittest(test_suite())