aboutsummaryrefslogtreecommitdiff
blob: d723535054a42c178cd11e515c97d3d165987831 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os
import sys
import textwrap
from datetime import datetime, timedelta, timezone
from functools import partial
from pathlib import Path
from unittest.mock import patch

import pytest
from pkgcore.ebuild.atom import atom as atom_cls
from pkgdev.scripts import run
from snakeoil.contexts import chdir, os_environ
from snakeoil.osutils import pjoin


class TestPkgdevMaskParseArgs:
    def test_non_repo_cwd(self, capsys, tool):
        with pytest.raises(SystemExit):
            tool.parse_args(["mask"])
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: not in ebuild repo"

    def test_non_git_repo_cwd(self, repo, capsys, tool):
        with pytest.raises(SystemExit), chdir(repo.location):
            tool.parse_args(["mask"])
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: not in git repo"

    def test_non_ebuild_git_repo_cwd(self, make_repo, git_repo, capsys, tool):
        os.mkdir(pjoin(git_repo.path, "repo"))
        repo = make_repo(pjoin(git_repo.path, "repo"))
        with pytest.raises(SystemExit), chdir(repo.location):
            tool.parse_args(["mask"])
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: not in ebuild git repo"

    def test_cwd_target(self, repo, make_git_repo, capsys, tool):
        git_repo = make_git_repo(repo.location)
        # empty repo
        with pytest.raises(SystemExit), chdir(repo.location):
            tool.parse_args(["mask"])
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: not in a package directory"

        # not in package dir
        repo.create_ebuild("cat/pkg-0")
        git_repo.add_all("cat/pkg-0")
        with pytest.raises(SystemExit), chdir(repo.location):
            tool.parse_args(["mask"])
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: not in a package directory"

        # masking CWD package
        with chdir(pjoin(repo.location, "cat/pkg")):
            options, _ = tool.parse_args(["mask"])
        assert options.atoms == [atom_cls("cat/pkg")]

    def test_targets(self, repo, make_git_repo, capsys, tool):
        git_repo = make_git_repo(repo.location)

        # invalid atom
        with pytest.raises(SystemExit), chdir(repo.location):
            tool.parse_args(["mask", "pkg"])
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: invalid atom: 'pkg'"

        # nonexistent pkg
        with pytest.raises(SystemExit), chdir(repo.location):
            tool.parse_args(["mask", "cat/nonexistent"])
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: no repo matches: 'cat/nonexistent'"

        # masked pkg
        repo.create_ebuild("cat/pkg-0")
        git_repo.add_all("cat/pkg-0")
        with chdir(repo.location):
            options, _ = tool.parse_args(["mask", "cat/pkg"])
        assert options.atoms == [atom_cls("cat/pkg")]

    def test_email_not_rites(self, repo, make_git_repo, capsys, tool):
        git_repo = make_git_repo(repo.location)

        # masked pkg
        repo.create_ebuild("cat/pkg-0")
        git_repo.add_all("cat/pkg-0")
        with pytest.raises(SystemExit), chdir(repo.location):
            tool.parse_args(["mask", "--email", "cat/pkg"])
        _, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: last rites required for email support"


class TestPkgdevMask:
    script = staticmethod(partial(run, "pkgdev"))

    @pytest.fixture(autouse=True)
    def _setup(self, make_repo, make_git_repo):
        # args for running pkgdev like a script
        self.args = ["pkgdev", "mask"]
        self.repo = make_repo(arches=["amd64"])
        self.git_repo = make_git_repo(self.repo.location)
        self.today = datetime.now(timezone.utc)

        # add stub pkg
        self.repo.create_ebuild("cat/pkg-0")
        self.git_repo.add_all("cat/pkg-0")

        # create profile
        self.profile_path = pjoin(self.repo.location, "profiles/arch/amd64")
        os.makedirs(self.profile_path)
        with open(pjoin(self.repo.location, "profiles/profiles.desc"), "w") as f:
            f.write("amd64 arch/amd64 stable\n")

        self.masks_path = Path(pjoin(self.repo.location, "profiles/package.mask"))

    @property
    def profile(self):
        profile = list(self.repo.config.profiles)[0]
        return self.repo.config.profiles.create_profile(profile)

    def test_empty_repo(self):
        assert self.profile.masks == frozenset()

    def test_nonexistent_editor(self, capsys):
        with (
            os_environ("VISUAL", EDITOR="12345"),
            patch("sys.argv", self.args + ["cat/pkg"]),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path)),
        ):
            self.script()
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: nonexistent editor: '12345'"

    def test_nonexistent_visual(self, capsys):
        with (
            os_environ("EDITOR", VISUAL="12345"),
            patch("sys.argv", self.args + ["cat/pkg"]),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path)),
        ):
            self.script()
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: nonexistent editor: '12345'"

    def test_failed_editor(self, capsys):
        with (
            os_environ("VISUAL", EDITOR="sed -i 's///'"),
            patch("sys.argv", self.args + ["cat/pkg"]),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path)),
        ):
            self.script()
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: failed writing mask comment"

    def test_empty_mask_comment(self, capsys):
        with (
            os_environ("VISUAL", EDITOR="sed -i 's/#/#/'"),
            patch("sys.argv", self.args + ["cat/pkg"]),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path)),
        ):
            self.script()
        out, err = capsys.readouterr()
        assert err.strip() == "pkgdev mask: error: empty mask comment"

    def test_mask_cwd(self):
        with (
            os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
            patch("sys.argv", self.args),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path, "cat/pkg")),
        ):
            self.script()
        assert self.profile.masks == frozenset([atom_cls("cat/pkg")])

    def test_mask_target(self):
        with (
            os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
            patch("sys.argv", self.args + ["cat/pkg"]),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path)),
        ):
            self.script()
        assert self.profile.masks == frozenset([atom_cls("cat/pkg")])

    def test_mask_ebuild_path(self):
        with (
            os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
            patch("sys.argv", self.args + ["cat/pkg/pkg-0.ebuild"]),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path)),
        ):
            self.script()
        assert self.profile.masks == frozenset([atom_cls("=cat/pkg-0")])

    def test_existing_masks(self):
        self.masks_path.write_text(
            textwrap.dedent(
                """\
                    # Random Dev <random.dev@email.com> (2021-03-24)
                    # masked
                    cat/masked
                """
            )
        )

        with (
            os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
            patch("sys.argv", self.args + ["=cat/pkg-0"]),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path)),
        ):
            self.script()
        assert self.profile.masks == frozenset([atom_cls("cat/masked"), atom_cls("=cat/pkg-0")])

    def test_invalid_header(self, capsys):
        self.masks_path.write_text(
            textwrap.dedent(
                """\
                    # Random Dev <random.dev@email.com> (2022-09-09)
                    #
                    # Larry the Cow was here
                    #
                    # masked
                    cat/masked

                    # Larry the Cow <larry@gentoo.org> (2022-09-09)
                    #test
                    # Larry the Cow wasn't here
                    cat/masked2
                """
            )
        )

        with (
            os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
            patch("sys.argv", self.args + ["=cat/pkg-0"]),
            pytest.raises(SystemExit),
            chdir(pjoin(self.repo.path)),
        ):
            self.script()
        _, err = capsys.readouterr()
        assert "invalid mask entry header, lineno 9" in err

    def test_invalid_author(self, capsys):
        for line in (
            "# Random Dev <random.dev@email.com>",
            "# Random Dev <random.dev@email.com) (2021-03-24)",
            "# Random Dev (2021-03-24)",
            "# Random Dev <random.dev@email.com> 2021-03-24",
            "# Random Dev <random.dev@email.com> (24-03-2021)",
        ):
            self.masks_path.write_text(
                textwrap.dedent(
                    f"""\
                        # Random Dev <random.dev@email.com> (2021-03-24)
                        # masked
                        cat/masked

                        {line}
                        # masked
                        cat/masked2
                    """
                )
            )

            with (
                os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
                patch("sys.argv", self.args + ["=cat/pkg-0"]),
                pytest.raises(SystemExit),
                chdir(pjoin(self.repo.path)),
            ):
                self.script()
            _, err = capsys.readouterr()
            assert "pkgdev mask: error: invalid author, lineno 5" in err

    def test_last_rites(self):
        for rflag in ("-r", "--rites"):
            for args in ([rflag], [rflag, "14"]):
                with (
                    os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
                    patch("sys.argv", self.args + ["cat/pkg"] + args),
                    pytest.raises(SystemExit),
                    chdir(pjoin(self.repo.path)),
                ):
                    self.script()

                days = 30 if len(args) == 1 else int(args[1])
                removal_date = self.today + timedelta(days=days)
                today = self.today.strftime("%Y-%m-%d")
                removal = removal_date.strftime("%Y-%m-%d")
                assert self.masks_path.read_text() == textwrap.dedent(
                    f"""\
                        # First Last <first.last@email.com> ({today})
                        # mask comment
                        # Removal: {removal}.
                        cat/pkg
                    """
                )
                self.masks_path.write_text("")  # Reset the contents of package.mask

    @pytest.mark.skipif(sys.platform == "darwin", reason="no xdg-email on mac os")
    def test_last_rites_with_email(self, tmp_path):
        output_file = tmp_path / "mail.txt"
        for rflag in ("-r", "--rites"):
            with (
                os_environ(
                    "VISUAL", EDITOR="sed -i '1s/$/mask comment/'", MAILER=f"> {output_file} echo"
                ),
                patch("sys.argv", self.args + ["cat/pkg", rflag, "--email"]),
                pytest.raises(SystemExit),
                chdir(pjoin(self.repo.path)),
            ):
                self.script()
            out = output_file.read_text()
            assert "mailto:gentoo-dev-announce@lists.gentoo.org" in out

            self.masks_path.write_text("")  # Reset the contents of package.mask

    @pytest.mark.skipif(sys.platform == "darwin", reason="no xdg-email on mac os")
    def test_last_email_bad_mailer(self, capsys):
        for rflag in ("-r", "--rites"):
            with (
                os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'", MAILER="false"),
                patch("sys.argv", self.args + ["cat/pkg", rflag, "--email"]),
                pytest.raises(SystemExit),
                chdir(pjoin(self.repo.path)),
            ):
                self.script()
            _, err = capsys.readouterr()
            assert err.strip() == "pkgdev mask: error: failed opening email composer"

    def test_mask_bugs(self):
        today = self.today.strftime("%Y-%m-%d")
        for bflag in ("-b", "--bug"):
            for bug_nums, expected in [
                (["42"], "Bug #42."),
                (["42", "43"], "Bugs #42, #43."),
            ]:
                args = []
                for bug_num in bug_nums:
                    args += [bflag, bug_num]
                with (
                    os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
                    patch("sys.argv", self.args + ["cat/pkg"] + args),
                    pytest.raises(SystemExit),
                    chdir(pjoin(self.repo.path)),
                ):
                    self.script()

                assert self.masks_path.read_text() == textwrap.dedent(
                    f"""\
                        # First Last <first.last@email.com> ({today})
                        # mask comment
                        # {expected}
                        cat/pkg
                    """
                )
                self.masks_path.write_text("")  # Reset the contents of package.mask

    def test_mask_bug_bad(self, capsys, tool):
        for arg, expected in [("-1", "must be >= 1"), ("foo", "invalid integer value")]:
            with pytest.raises(SystemExit):
                tool.parse_args(["mask", "--bug", arg])
            out, err = capsys.readouterr()
            assert err.strip() == f"pkgdev mask: error: argument -b/--bug: {expected}"