File size: 4,221 Bytes
838f737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Tests for distutils._modified."""

import os
import types
from distutils._modified import newer, newer_group, newer_pairwise, newer_pairwise_group
from distutils.errors import DistutilsFileError
from distutils.tests import support

import pytest


class TestDepUtil(support.TempdirManager):
    def test_newer(self):
        tmpdir = self.mkdtemp()
        new_file = os.path.join(tmpdir, 'new')
        old_file = os.path.abspath(__file__)

        # Raise DistutilsFileError if 'new_file' does not exist.
        with pytest.raises(DistutilsFileError):
            newer(new_file, old_file)

        # Return true if 'new_file' exists and is more recently modified than
        # 'old_file', or if 'new_file' exists and 'old_file' doesn't.
        self.write_file(new_file)
        assert newer(new_file, 'I_dont_exist')
        assert newer(new_file, old_file)

        # Return false if both exist and 'old_file' is the same age or younger
        # than 'new_file'.
        assert not newer(old_file, new_file)

    def _setup_1234(self):
        tmpdir = self.mkdtemp()
        sources = os.path.join(tmpdir, 'sources')
        targets = os.path.join(tmpdir, 'targets')
        os.mkdir(sources)
        os.mkdir(targets)
        one = os.path.join(sources, 'one')
        two = os.path.join(sources, 'two')
        three = os.path.abspath(__file__)  # I am the old file
        four = os.path.join(targets, 'four')
        self.write_file(one)
        self.write_file(two)
        self.write_file(four)
        return one, two, three, four

    def test_newer_pairwise(self):
        one, two, three, four = self._setup_1234()

        assert newer_pairwise([one, two], [three, four]) == ([one], [three])

    def test_newer_pairwise_mismatch(self):
        one, two, three, four = self._setup_1234()

        with pytest.raises(ValueError):
            newer_pairwise([one], [three, four])

        with pytest.raises(ValueError):
            newer_pairwise([one, two], [three])

    def test_newer_pairwise_empty(self):
        assert newer_pairwise([], []) == ([], [])

    def test_newer_pairwise_fresh(self):
        one, two, three, four = self._setup_1234()

        assert newer_pairwise([one, three], [two, four]) == ([], [])

    def test_newer_group(self):
        tmpdir = self.mkdtemp()
        sources = os.path.join(tmpdir, 'sources')
        os.mkdir(sources)
        one = os.path.join(sources, 'one')
        two = os.path.join(sources, 'two')
        three = os.path.join(sources, 'three')
        old_file = os.path.abspath(__file__)

        # return true if 'old_file' is out-of-date with respect to any file
        # listed in 'sources'.
        self.write_file(one)
        self.write_file(two)
        self.write_file(three)
        assert newer_group([one, two, three], old_file)
        assert not newer_group([one, two, old_file], three)

        # missing handling
        os.remove(one)
        with pytest.raises(OSError):
            newer_group([one, two, old_file], three)

        assert not newer_group([one, two, old_file], three, missing='ignore')

        assert newer_group([one, two, old_file], three, missing='newer')


@pytest.fixture
def groups_target(tmp_path):
    """
    Set up some older sources, a target, and newer sources.

    Returns a simple namespace with these values.
    """
    filenames = ['older.c', 'older.h', 'target.o', 'newer.c', 'newer.h']
    paths = [tmp_path / name for name in filenames]

    for mtime, path in enumerate(paths):
        path.write_text('', encoding='utf-8')

        # make sure modification times are sequential
        os.utime(path, (mtime, mtime))

    return types.SimpleNamespace(older=paths[:2], target=paths[2], newer=paths[3:])


def test_newer_pairwise_group(groups_target):
    older = newer_pairwise_group([groups_target.older], [groups_target.target])
    newer = newer_pairwise_group([groups_target.newer], [groups_target.target])
    assert older == ([], [])
    assert newer == ([groups_target.newer], [groups_target.target])


def test_newer_group_no_sources_no_target(tmp_path):
    """
    Consider no sources and no target "newer".
    """
    assert newer_group([], str(tmp_path / 'does-not-exist'))