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