aboutsummaryrefslogtreecommitdiff
blob: 2e181e575c86776ff218893472011c17189d371a (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
from datetime import datetime, timedelta

import pytest
from pkgcheck.checks import SkipCheck
from pkgcheck.checks.stablereq import StableRequest, StableRequestCheck
from pkgcore.ebuild.cpv import VersionedCPV
from snakeoil.osutils import pjoin

from ..misc import ReportTestCase, init_check


class TestStableRequestCheck(ReportTestCase):

    check_kls = StableRequestCheck

    @pytest.fixture(autouse=True)
    def _setup(self, tmp_path, tool, make_repo, make_git_repo):
        self._tool = tool
        self.cache_dir = str(tmp_path)

        # initialize parent repo
        self.parent_git_repo = make_git_repo()
        self.parent_repo = make_repo(self.parent_git_repo.path, repo_id="gentoo")
        self.parent_git_repo.add_all("initial commit")
        # create a stub pkg and commit it
        self.parent_repo.create_ebuild("cat/pkg-0")
        self.parent_git_repo.add_all("cat/pkg-0")

        # initialize child repo
        self.child_git_repo = make_git_repo()
        self.child_git_repo.run(["git", "remote", "add", "origin", self.parent_git_repo.path])
        self.child_git_repo.run(["git", "pull", "origin", "main"])
        self.child_git_repo.run(["git", "remote", "set-head", "origin", "main"])
        self.child_repo = make_repo(self.child_git_repo.path)

    def init_check(self, options=None, future=0, stable_time=None):
        self.options = options if options is not None else self._options(stable_time=stable_time)
        self.check, required_addons, self.source = init_check(self.check_kls, self.options)
        for k, v in required_addons.items():
            setattr(self, k, v)
        if future:
            self.check.today = datetime.today() + timedelta(days=+future)

    def _options(self, stable_time=None, **kwargs):
        args = [
            "scan",
            "-q",
            "--cache-dir",
            self.cache_dir,
            "--repo",
            self.child_repo.location,
        ]
        if stable_time is not None:
            args.extend(["--stabletime", str(stable_time)])
        options, _ = self._tool.parse_args(args)
        return options

    def test_no_git_support(self):
        options = self._options()
        options.cache["git"] = False
        with pytest.raises(SkipCheck, match="git cache support required"):
            self.init_check(options)

    def test_no_stable_keywords(self):
        self.parent_repo.create_ebuild("cat/pkg-1", keywords=["~amd64"])
        self.parent_git_repo.add_all("cat/pkg-1")
        self.parent_repo.create_ebuild("cat/pkg-2", keywords=["~amd64"])
        self.parent_git_repo.add_all("cat/pkg-2")
        self.child_git_repo.run(["git", "pull", "origin", "main"])
        self.init_check()
        self.assertNoReport(self.check, self.source)

    def test_uncommitted_local_ebuild(self):
        self.parent_repo.create_ebuild("cat/pkg-1", keywords=["amd64"])
        self.parent_git_repo.add_all("cat/pkg-1")
        self.child_git_repo.run(["git", "pull", "origin", "main"])
        self.child_repo.create_ebuild("cat/pkg-2", keywords=["~amd64"])
        self.init_check(future=30)
        self.assertNoReport(self.check, self.source)

    @pytest.mark.parametrize(
        ("stable_time", "less_days", "more_days"),
        (
            pytest.param(None, (0, 1, 10, 20, 29), (30, 31), id="stable_time=unset"),
            pytest.param(1, (0,), (1, 10), id="stable_time=1"),
            pytest.param(14, (0, 1, 10, 13), (14, 15, 30), id="stable_time=14"),
            pytest.param(30, (0, 1, 10, 20, 29), (30, 31), id="stable_time=30"),
            pytest.param(100, (98, 99), (100, 101), id="stable_time=100"),
        ),
    )
    def test_existing_stable_keywords(self, stable_time, less_days, more_days):
        self.parent_repo.create_ebuild("cat/pkg-1", keywords=["amd64"])
        self.parent_git_repo.add_all("cat/pkg-1")
        self.parent_repo.create_ebuild("cat/pkg-2", keywords=["~amd64"])
        self.parent_git_repo.add_all("cat/pkg-2")
        self.child_git_repo.run(["git", "pull", "origin", "main"])

        # packages are not old enough to trigger any results
        for future in less_days:
            self.init_check(future=future, stable_time=stable_time)
            self.assertNoReport(self.check, self.source, msg=f"Got report for future={future}")

        # packages are now >= stable_time days old
        for future in more_days:
            self.init_check(future=future, stable_time=stable_time)
            r = self.assertReport(self.check, self.source)
            expected = StableRequest("0", ["~amd64"], future, pkg=VersionedCPV("cat/pkg-2"))
            assert r == expected

    def test_multislot_with_unstable_slot(self):
        self.parent_repo.create_ebuild("cat/pkg-1", keywords=["amd64"])
        self.parent_git_repo.add_all("cat/pkg-1")
        self.parent_repo.create_ebuild("cat/pkg-2", keywords=["~amd64"], slot="1")
        self.parent_git_repo.add_all("cat/pkg-2")
        self.child_git_repo.run(["git", "pull", "origin", "main"])
        self.init_check(future=30)
        r = self.assertReport(self.check, self.source)
        expected = StableRequest("1", ["~amd64"], 30, pkg=VersionedCPV("cat/pkg-2"))
        assert r == expected

    def test_moved_category(self):
        self.parent_repo.create_ebuild("cat/pkg-1", keywords=["amd64"])
        self.parent_git_repo.add_all("cat/pkg-1")
        self.parent_repo.create_ebuild("cat/pkg-2", keywords=["~amd64"])
        self.parent_git_repo.add_all("cat/pkg-2")
        self.parent_git_repo.move("cat", "newcat")
        self.child_git_repo.run(["git", "pull", "origin", "main"])
        self.init_check(future=30)
        r = self.assertReport(self.check, self.source)
        expected = StableRequest("0", ["~amd64"], 30, pkg=VersionedCPV("newcat/pkg-2"))
        assert r == expected

    def test_moved_package(self):
        self.parent_repo.create_ebuild("cat/pkg-1", keywords=["amd64"])
        self.parent_git_repo.add_all("cat/pkg-1")
        self.parent_repo.create_ebuild("cat/pkg-2", keywords=["~amd64"])
        self.parent_git_repo.add_all("cat/pkg-2")

        # rename pkg and commit results
        path = self.parent_git_repo.path
        new_pkg_dir = pjoin(path, "cat/newpkg")
        os.rename(pjoin(path, "cat/pkg"), new_pkg_dir)
        for i, f in enumerate(sorted(os.listdir(new_pkg_dir))):
            os.rename(pjoin(new_pkg_dir, f), pjoin(new_pkg_dir, f"newpkg-{i}.ebuild"))
        self.parent_git_repo.add_all()
        self.child_git_repo.run(["git", "pull", "origin", "main"])

        self.init_check(future=30)
        r = self.assertReport(self.check, self.source)
        expected = StableRequest("0", ["~amd64"], 30, pkg=VersionedCPV("cat/newpkg-2"))
        assert r == expected

    def test_renamed_ebuild(self):
        self.parent_repo.create_ebuild("cat/pkg-1", keywords=["amd64"])
        self.parent_git_repo.add_all("cat/pkg-1")
        self.parent_repo.create_ebuild("cat/pkg-2_rc1", keywords=["~amd64"])
        self.parent_git_repo.add_all("cat/pkg-2_rc1")
        self.parent_git_repo.move("cat/pkg/pkg-2_rc1.ebuild", "cat/pkg/pkg-2.ebuild")
        self.child_git_repo.run(["git", "pull", "origin", "main"])
        self.init_check(future=30)
        r = self.assertReport(self.check, self.source)
        expected = StableRequest("0", ["~amd64"], 30, pkg=VersionedCPV("cat/pkg-2"))
        assert r == expected

    def test_modified_ebuild(self):
        self.parent_repo.create_ebuild("cat/pkg-1", keywords=["amd64"])
        self.parent_git_repo.add_all("cat/pkg-1")
        self.parent_repo.create_ebuild("cat/pkg-2", keywords=["~amd64"])
        self.parent_git_repo.add_all("cat/pkg-2")
        with open(pjoin(self.parent_git_repo.path, "cat/pkg/pkg-2.ebuild"), "a") as f:
            f.write("# comment\n")
        self.parent_git_repo.add_all("cat/pkg-2: add comment")
        self.child_git_repo.run(["git", "pull", "origin", "main"])
        self.init_check(future=30)
        r = self.assertReport(self.check, self.source)
        expected = StableRequest("0", ["~amd64"], 30, pkg=VersionedCPV("cat/pkg-2"))
        assert r == expected