codekingpro/portable-devtools
114k
1"""Tests for distutils._modified."""2 3import os4import types5from distutils._modified import newer, newer_group, newer_pairwise, newer_pairwise_group6from distutils.errors import DistutilsFileError7from distutils.tests import support8 9import pytest10 11 12class TestDepUtil(support.TempdirManager):13 def test_newer(self):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 with pytest.raises(DistutilsFileError):20 newer(new_file, old_file)21 22 # Return true if 'new_file' exists and is more recently modified than23 # 'old_file', or if 'new_file' exists and 'old_file' doesn't.24 self.write_file(new_file)25 assert newer(new_file, 'I_dont_exist')26 assert newer(new_file, old_file)27 28 # Return false if both exist and 'old_file' is the same age or younger29 # than 'new_file'.30 assert not newer(old_file, new_file)31 32 def _setup_1234(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 file41 four = os.path.join(targets, 'four')42 self.write_file(one)43 self.write_file(two)44 self.write_file(four)45 return one, two, three, four46 47 def test_newer_pairwise(self):48 one, two, three, four = self._setup_1234()49 50 assert newer_pairwise([one, two], [three, four]) == ([one], [three])51 52 def test_newer_pairwise_mismatch(self):53 one, two, three, four = self._setup_1234()54 55 with pytest.raises(ValueError):56 newer_pairwise([one], [three, four])57 58 with pytest.raises(ValueError):59 newer_pairwise([one, two], [three])60 61 def test_newer_pairwise_empty(self):62 assert newer_pairwise([], []) == ([], [])63 64 def test_newer_pairwise_fresh(self):65 one, two, three, four = self._setup_1234()66 67 assert newer_pairwise([one, three], [two, four]) == ([], [])68 69 def test_newer_group(self):70 tmpdir = self.mkdtemp()71 sources = os.path.join(tmpdir, 'sources')72 os.mkdir(sources)73 one = os.path.join(sources, 'one')74 two = os.path.join(sources, 'two')75 three = os.path.join(sources, 'three')76 old_file = os.path.abspath(__file__)77 78 # return true if 'old_file' is out-of-date with respect to any file79 # listed in 'sources'.80 self.write_file(one)81 self.write_file(two)82 self.write_file(three)83 assert newer_group([one, two, three], old_file)84 assert not newer_group([one, two, old_file], three)85 86 # missing handling87 os.remove(one)88 with pytest.raises(OSError):89 newer_group([one, two, old_file], three)90 91 assert not newer_group([one, two, old_file], three, missing='ignore')92 93 assert newer_group([one, two, old_file], three, missing='newer')94 95 96@pytest.fixture97def groups_target(tmp_path):98 """99 Set up some older sources, a target, and newer sources.100 101 Returns a simple namespace with these values.102 """103 filenames = ['older.c', 'older.h', 'target.o', 'newer.c', 'newer.h']104 paths = [tmp_path / name for name in filenames]105 106 for mtime, path in enumerate(paths):107 path.write_text('', encoding='utf-8')108 109 # make sure modification times are sequential110 os.utime(path, (mtime, mtime))111 112 return types.SimpleNamespace(older=paths[:2], target=paths[2], newer=paths[3:])113 114 115def test_newer_pairwise_group(groups_target):116 older = newer_pairwise_group([groups_target.older], [groups_target.target])117 newer = newer_pairwise_group([groups_target.newer], [groups_target.target])118 assert older == ([], [])119 assert newer == ([groups_target.newer], [groups_target.target])120 121 122def test_newer_group_no_sources_no_target(tmp_path):123 """124 Consider no sources and no target "newer".125 """126 assert newer_group([], str(tmp_path / 'does-not-exist'))127 