aboutsummaryrefslogtreecommitdiff
blob: 8dadb5d4a2ea31210c4d2d6a9d958541a27c1f56 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import textwrap
from functools import partial
from typing import List, NamedTuple
from unittest.mock import patch

import pytest
from snakeoil.contexts import chdir, os_environ

from pkgdev.scripts import run


class Profile(NamedTuple):
    """Profile record used to create profiles in a repository."""

    path: str
    arch: str
    status: str = "stable"
    deprecated: bool = False
    defaults: List[str] = None
    eapi: str = "5"


class TestPkgdevShowkwParseArgs:
    args = ("showkw", "--config", "no")

    def test_missing_target(self, capsys, tool):
        with pytest.raises(SystemExit):
            tool.parse_args(self.args)
        captured = capsys.readouterr()
        assert captured.err.strip() == (
            "pkgdev showkw: error: missing target argument and not in a supported repo"
        )

    def test_unknown_arches(self, capsys, tool, make_repo):
        repo = make_repo(arches=["amd64"])
        with pytest.raises(SystemExit):
            tool.parse_args([*self.args, "-a", "unknown", "-r", repo.location])
        captured = capsys.readouterr()
        assert captured.err.strip() == (
            "pkgdev showkw: error: unknown arch: 'unknown' (choices: amd64)"
        )

    def test_no_color(self, tool, make_repo, tmp_path):
        repo = make_repo(arches=["amd64"])
        repo.create_ebuild("foo/bar-0", keywords=("x86"))

        (config_file := tmp_path / "pkgcheck.conf").write_text(
            textwrap.dedent(
                """\
            [DEFAULT]
            showkw.color = true
        """
            )
        )

        def parse(*args):
            options, _ = tool.parse_args(
                ["showkw", "-r", repo.location, "foo/bar", "--config", str(config_file), *args]
            )
            return options

        with os_environ("NOCOLOR"):
            assert parse().color is True
        with os_environ(NOCOLOR="1"):
            # NOCOLOR overrides config file
            assert parse().color is False
            # cmd line option overrides NOCOLOR
            assert parse("--color", "n").color is False
            assert parse("--color", "y").color is True


class TestPkgdevShowkw:
    script = staticmethod(partial(run, "pkgdev"))
    base_args = ("pkgdev", "showkw", "--config", "n", "--color", "n")

    def _create_repo(self, make_repo):
        repo = make_repo(arches=["amd64", "ia64", "mips", "x86"])
        repo.create_profiles(
            [
                Profile("default/linux/amd64", "amd64"),
                Profile("default/linux/x86", "x86"),
                Profile("default/linux/ia64", "ia64", "dev"),
                Profile("default/linux/mips", "mips", "exp"),
            ]
        )
        return repo

    def _run_and_parse(self, capsys, *args):
        with (
            patch("sys.argv", [*self.base_args, "--format", "presto", *args]),
            pytest.raises(SystemExit) as excinfo,
        ):
            self.script()
        assert excinfo.value.code is None
        out, err = capsys.readouterr()
        assert not err
        lines = out.split("\n")
        table_columns = [s.strip() for s in lines[1].split("|")][1:]
        return {
            ver: dict(zip(table_columns, values))
            for ver, *values in map(lambda s: map(str.strip, s.split("|")), lines[3:-1])
        }

    def test_match(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0")
        with (
            patch("sys.argv", [*self.base_args, "-r", repo.location, "foo/bar"]),
            pytest.raises(SystemExit) as excinfo,
        ):
            self.script()
        assert excinfo.value.code is None
        out, err = capsys.readouterr()
        assert not err
        assert out.split("\n")[0] == "keywords for foo/bar:"

    def test_match_short_name(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0")
        with (
            patch("sys.argv", [*self.base_args, "-r", repo.location, "bar"]),
            pytest.raises(SystemExit) as excinfo,
        ):
            self.script()
        assert excinfo.value.code is None
        out, err = capsys.readouterr()
        assert not err
        assert out.split("\n")[0] == "keywords for foo/bar:"

    def test_match_cwd_repo(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0")
        with (
            patch("sys.argv", [*self.base_args, "foo/bar"]),
            pytest.raises(SystemExit) as excinfo,
            chdir(repo.location),
        ):
            self.script()
        assert excinfo.value.code is None
        out, err = capsys.readouterr()
        assert not err
        assert out.split("\n")[0] == "keywords for foo/bar:"

    def test_match_cwd_pkg(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0")
        with (
            patch("sys.argv", self.base_args),
            pytest.raises(SystemExit) as excinfo,
            chdir(repo.location + "/foo/bar"),
        ):
            self.script()
        assert excinfo.value.code is None
        _, err = capsys.readouterr()
        assert not err

    def test_no_matches(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        with (
            patch("sys.argv", [*self.base_args, "-r", repo.location, "foo/bar"]),
            pytest.raises(SystemExit) as excinfo,
        ):
            self.script()
        assert excinfo.value.code == 1
        out, err = capsys.readouterr()
        assert not out
        assert err.strip() == "pkgdev showkw: no matches for 'foo/bar'"

    def test_match_stable(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0", keywords=("~amd64", "~ia64", "~mips", "x86"))
        res = self._run_and_parse(capsys, "-r", repo.location, "foo/bar", "--stable")
        assert set(res.keys()) == {"0"}
        assert {"amd64", "ia64", "mips", "x86"} & res["0"].keys() == {"amd64", "x86"}

    def test_match_unstable(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0", keywords=("~amd64", "~ia64", "~mips", "x86"))
        res = self._run_and_parse(capsys, "-r", repo.location, "foo/bar", "--unstable")
        assert set(res.keys()) == {"0"}
        assert {"amd64", "ia64", "mips", "x86"} <= res["0"].keys()

    def test_match_specific_arch(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0", keywords=("~amd64", "~ia64", "~mips", "x86"))
        res = self._run_and_parse(capsys, "-r", repo.location, "foo/bar", "--arch", "amd64")
        assert set(res.keys()) == {"0"}
        assert {"amd64", "ia64", "mips", "x86"} & res["0"].keys() == {"amd64"}

    def test_match_specific_multiple_arch(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0", keywords=("~amd64", "~ia64", "~mips", "x86"))
        res = self._run_and_parse(capsys, "-r", repo.location, "foo/bar", "--arch", "amd64,mips")
        assert set(res.keys()) == {"0"}
        assert {"amd64", "ia64", "mips", "x86"} & res["0"].keys() == {"amd64", "mips"}

    def test_correct_keywords_status(self, capsys, make_repo):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0", keywords=("amd64", "~ia64", "~mips", "x86"))
        repo.create_ebuild("foo/bar-1", keywords=("~amd64", "-mips", "~x86"))
        repo.create_ebuild("foo/bar-2", keywords=("-*", "amd64", "-x86"), eapi=8, slot=2)
        res = self._run_and_parse(capsys, "-r", repo.location, "foo/bar")
        assert set(res.keys()) == {"0", "1", "2"}
        assert dict(amd64="+", ia64="~", mips="~", x86="+", slot="0").items() <= res["0"].items()
        assert dict(amd64="~", ia64="o", mips="-", x86="~", slot="0").items() <= res["1"].items()
        assert (
            dict(amd64="+", ia64="*", mips="*", x86="-", slot="2", eapi="8").items()
            <= res["2"].items()
        )

    @pytest.mark.parametrize(
        ("arg", "expected"),
        (
            pytest.param("--stable", {"amd64", "x86"}, id="stable"),
            pytest.param("--unstable", {"amd64", "ia64", "mips", "x86"}, id="unstable"),
            pytest.param("--only-unstable", {"ia64", "mips"}, id="only-unstable"),
        ),
    )
    def test_collapse(self, capsys, make_repo, arg, expected):
        repo = self._create_repo(make_repo)
        repo.create_ebuild("foo/bar-0", keywords=("amd64", "~ia64", "~mips", "~x86"))
        repo.create_ebuild("foo/bar-1", keywords=("~amd64", "~ia64", "~mips", "x86"))
        with (
            patch("sys.argv", [*self.base_args, "-r", repo.location, "foo/bar", "--collapse", arg]),
            pytest.raises(SystemExit) as excinfo,
        ):
            self.script()
        out, err = capsys.readouterr()
        assert excinfo.value.code is None
        assert not err
        arches = set(out.split("\n")[0].split())
        assert arches == expected