Skip to content

Commit 7bcfce7

Browse files
committed
tests: use monkeypatch instead of snakeoil's os_environ
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent bd2eba0 commit 7bcfce7

3 files changed

Lines changed: 73 additions & 60 deletions

File tree

‎tests/scripts/test_pkgdev_commit.py‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import pytest
1313
from pkgcore.ebuild.atom import atom as atom_cls
14-
from snakeoil.contexts import os_environ
1514

1615
from pkgdev.mangle import copyright_regex, keywords_regex
1716
from pkgdev.scripts import pkgdev_commit as commit
@@ -258,7 +257,7 @@ def test_path_matching_ref_name(self, repo, make_git_repo):
258257
assert excinfo.value.code == 0
259258
assert git_repo.log(["--pretty=tformat:%s", "-1"]) == ["main: add main"]
260259

261-
def test_git_message_opts(self, repo, make_git_repo, tmp_path):
260+
def test_git_message_opts(self, repo, make_git_repo, tmp_path, monkeypatch):
262261
"""Verify message-related options are passed through to `git commit`."""
263262
git_repo = make_git_repo(repo.location, commit=True)
264263
repo.create_ebuild("cat/pkg-0")
@@ -279,8 +278,8 @@ def test_git_message_opts(self, repo, make_git_repo, tmp_path):
279278

280279
repo.create_ebuild("cat/pkg-1")
281280
git_repo.add_all("cat/pkg-1", commit=False)
281+
monkeypatch.setenv("GIT_EDITOR", "sed -i '1s/1/2/'")
282282
with (
283-
os_environ(GIT_EDITOR="sed -i '1s/1/2/'"),
284283
patch("sys.argv", self.args + ["-u", "-t", path]),
285284
pytest.raises(SystemExit) as excinfo,
286285
chdir(git_repo.path),
@@ -404,15 +403,15 @@ def test_custom_prefixed_message(self, capsys, repo, make_git_repo):
404403
commit_msg = git_repo.log(["-1", "--pretty=tformat:%B", "HEAD"])
405404
assert commit_msg == ["prefix: msg"]
406405

407-
def test_edited_commit_message(self, capsys, repo, make_git_repo):
406+
def test_edited_commit_message(self, capsys, repo, make_git_repo, monkeypatch):
408407
git_repo = make_git_repo(repo.location)
409408
ebuild_path = repo.create_ebuild("cat/pkg-0")
410409
git_repo.add_all("cat/pkg-0")
411410
with open(ebuild_path, "a+") as f:
412411
f.write("# comment\n")
413412

413+
monkeypatch.setenv("GIT_EDITOR", "sed -i '1s/$/commit/'")
414414
with (
415-
os_environ(GIT_EDITOR="sed -i '1s/$/commit/'"),
416415
patch("sys.argv", self.args + ["-u"]),
417416
pytest.raises(SystemExit) as excinfo,
418417
chdir(git_repo.path),
@@ -511,13 +510,14 @@ def commit():
511510
assert commit() == "msg"
512511

513512
def _auto_commit(self, capsys, git_repo):
514-
with (
515-
os_environ(GIT_EDITOR="sed -i '1s/$/summary/'"),
516-
patch("sys.argv", self.args + ["-a"]),
517-
pytest.raises(SystemExit) as excinfo,
518-
chdir(git_repo.path),
519-
):
520-
self.script()
513+
with pytest.MonkeyPatch.context() as mp:
514+
mp.setenv("GIT_EDITOR", "sed -i '1s/$/summary/'")
515+
with (
516+
patch("sys.argv", self.args + ["-a"]),
517+
pytest.raises(SystemExit) as excinfo,
518+
chdir(git_repo.path),
519+
):
520+
self.script()
521521
assert excinfo.value.code == 0
522522
out, err = capsys.readouterr()
523523
assert err == out == ""
@@ -949,13 +949,14 @@ def test_no_summary(self, capsys, repo, make_git_repo):
949949
git_repo.add_all("cat/pkg-0")
950950

951951
def commit(args):
952-
with (
953-
os_environ(GIT_EDITOR="sed -i '1s/$/summary/'"),
954-
patch("sys.argv", self.args + args),
955-
pytest.raises(SystemExit) as excinfo,
956-
chdir(git_repo.path),
957-
):
958-
self.script()
952+
with pytest.MonkeyPatch.context() as mp:
953+
mp.setenv("GIT_EDITOR", "sed -i '1s/$/summary/'")
954+
with (
955+
patch("sys.argv", self.args + args),
956+
pytest.raises(SystemExit) as excinfo,
957+
chdir(git_repo.path),
958+
):
959+
self.script()
959960
assert excinfo.value.code == 0
960961
out, err = capsys.readouterr()
961962
assert err == out == ""

‎tests/scripts/test_pkgdev_mask.py‎

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import pytest
1212
from pkgcore.ebuild.atom import atom as atom_cls
13-
from snakeoil.contexts import os_environ
1413

1514
from pkgdev.scripts import run
1615

@@ -122,9 +121,10 @@ def profile(self):
122121
def test_empty_repo(self):
123122
assert self.profile.masks == frozenset()
124123

125-
def test_nonexistent_editor(self, capsys):
124+
def test_nonexistent_editor(self, capsys, monkeypatch):
125+
monkeypatch.delenv("VISUAL", raising=False)
126+
monkeypatch.setenv("EDITOR", "12345")
126127
with (
127-
os_environ("VISUAL", EDITOR="12345"),
128128
patch("sys.argv", self.args + ["cat/pkg"]),
129129
pytest.raises(SystemExit),
130130
chdir(pjoin(self.repo.path)),
@@ -133,9 +133,10 @@ def test_nonexistent_editor(self, capsys):
133133
_out, err = capsys.readouterr()
134134
assert err.strip() == "pkgdev mask: error: nonexistent editor: '12345'"
135135

136-
def test_nonexistent_visual(self, capsys):
136+
def test_nonexistent_visual(self, capsys, monkeypatch):
137+
monkeypatch.delenv("EDITOR", raising=False)
138+
monkeypatch.setenv("VISUAL", "12345")
137139
with (
138-
os_environ("EDITOR", VISUAL="12345"),
139140
patch("sys.argv", self.args + ["cat/pkg"]),
140141
pytest.raises(SystemExit),
141142
chdir(pjoin(self.repo.path)),
@@ -144,9 +145,10 @@ def test_nonexistent_visual(self, capsys):
144145
_out, err = capsys.readouterr()
145146
assert err.strip() == "pkgdev mask: error: nonexistent editor: '12345'"
146147

147-
def test_failed_editor(self, capsys):
148+
def test_failed_editor(self, capsys, monkeypatch):
149+
monkeypatch.delenv("VISUAL", raising=False)
150+
monkeypatch.setenv("EDITOR", "sed -i 's///'")
148151
with (
149-
os_environ("VISUAL", EDITOR="sed -i 's///'"),
150152
patch("sys.argv", self.args + ["cat/pkg"]),
151153
pytest.raises(SystemExit),
152154
chdir(pjoin(self.repo.path)),
@@ -155,9 +157,10 @@ def test_failed_editor(self, capsys):
155157
_out, err = capsys.readouterr()
156158
assert err.strip() == "pkgdev mask: error: failed writing mask comment"
157159

158-
def test_empty_mask_comment(self, capsys):
160+
def test_empty_mask_comment(self, capsys, monkeypatch):
161+
monkeypatch.delenv("VISUAL", raising=False)
162+
monkeypatch.setenv("EDITOR", "sed -i 's/#/#/'")
159163
with (
160-
os_environ("VISUAL", EDITOR="sed -i 's/#/#/'"),
161164
patch("sys.argv", self.args + ["cat/pkg"]),
162165
pytest.raises(SystemExit),
163166
chdir(pjoin(self.repo.path)),
@@ -166,37 +169,40 @@ def test_empty_mask_comment(self, capsys):
166169
_out, err = capsys.readouterr()
167170
assert err.strip() == "pkgdev mask: error: empty mask comment"
168171

169-
def test_mask_cwd(self):
172+
def test_mask_cwd(self, monkeypatch):
173+
monkeypatch.delenv("VISUAL", raising=False)
174+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
170175
with (
171-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
172176
patch("sys.argv", self.args),
173177
pytest.raises(SystemExit),
174178
chdir(pjoin(self.repo.path, "cat/pkg")),
175179
):
176180
self.script()
177181
assert self.profile.masks == frozenset([atom_cls("cat/pkg")])
178182

179-
def test_mask_target(self):
183+
def test_mask_target(self, monkeypatch):
184+
monkeypatch.delenv("VISUAL", raising=False)
185+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
180186
with (
181-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
182187
patch("sys.argv", self.args + ["cat/pkg"]),
183188
pytest.raises(SystemExit),
184189
chdir(pjoin(self.repo.path)),
185190
):
186191
self.script()
187192
assert self.profile.masks == frozenset([atom_cls("cat/pkg")])
188193

189-
def test_mask_ebuild_path(self):
194+
def test_mask_ebuild_path(self, monkeypatch):
195+
monkeypatch.delenv("VISUAL", raising=False)
196+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
190197
with (
191-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
192198
patch("sys.argv", self.args + ["cat/pkg/pkg-0.ebuild"]),
193199
pytest.raises(SystemExit),
194200
chdir(pjoin(self.repo.path)),
195201
):
196202
self.script()
197203
assert self.profile.masks == frozenset([atom_cls("=cat/pkg-0")])
198204

199-
def test_existing_masks(self):
205+
def test_existing_masks(self, monkeypatch):
200206
self.masks_path.write_text(
201207
textwrap.dedent(
202208
"""\
@@ -207,16 +213,17 @@ def test_existing_masks(self):
207213
)
208214
)
209215

216+
monkeypatch.delenv("VISUAL", raising=False)
217+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
210218
with (
211-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
212219
patch("sys.argv", self.args + ["=cat/pkg-0"]),
213220
pytest.raises(SystemExit),
214221
chdir(pjoin(self.repo.path)),
215222
):
216223
self.script()
217224
assert self.profile.masks == frozenset([atom_cls("cat/masked"), atom_cls("=cat/pkg-0")])
218225

219-
def test_invalid_header(self, capsys):
226+
def test_invalid_header(self, capsys, monkeypatch):
220227
self.masks_path.write_text(
221228
textwrap.dedent(
222229
"""\
@@ -235,8 +242,9 @@ def test_invalid_header(self, capsys):
235242
)
236243
)
237244

245+
monkeypatch.delenv("VISUAL", raising=False)
246+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
238247
with (
239-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
240248
patch("sys.argv", self.args + ["=cat/pkg-0"]),
241249
pytest.raises(SystemExit),
242250
chdir(pjoin(self.repo.path)),
@@ -245,7 +253,9 @@ def test_invalid_header(self, capsys):
245253
_, err = capsys.readouterr()
246254
assert "invalid mask entry header, lineno 9" in err
247255

248-
def test_invalid_author(self, capsys):
256+
def test_invalid_author(self, capsys, monkeypatch):
257+
monkeypatch.delenv("VISUAL", raising=False)
258+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
249259
for line in (
250260
"# Random Dev <random.dev@email.com>",
251261
"# Random Dev <random.dev@email.com) (2021-03-24)",
@@ -268,7 +278,6 @@ def test_invalid_author(self, capsys):
268278
)
269279

270280
with (
271-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
272281
patch("sys.argv", self.args + ["=cat/pkg-0"]),
273282
pytest.raises(SystemExit),
274283
chdir(pjoin(self.repo.path)),
@@ -277,11 +286,12 @@ def test_invalid_author(self, capsys):
277286
_, err = capsys.readouterr()
278287
assert "pkgdev mask: error: invalid author, lineno 5" in err
279288

280-
def test_last_rites(self):
289+
def test_last_rites(self, monkeypatch):
290+
monkeypatch.delenv("VISUAL", raising=False)
291+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
281292
for rflag in ("-r", "--rites"):
282293
for args in ([rflag], [rflag, "14"]):
283294
with (
284-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
285295
patch("sys.argv", self.args + ["cat/pkg"] + args),
286296
pytest.raises(SystemExit),
287297
chdir(pjoin(self.repo.path)),
@@ -303,13 +313,13 @@ def test_last_rites(self):
303313
self.masks_path.write_text("") # Reset the contents of package.mask
304314

305315
@pytest.mark.skipif(sys.platform == "darwin", reason="no xdg-email on mac os")
306-
def test_last_rites_with_email(self, tmp_path):
316+
def test_last_rites_with_email(self, tmp_path, monkeypatch):
307317
output_file = tmp_path / "mail.txt"
318+
monkeypatch.delenv("VISUAL", raising=False)
319+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
320+
monkeypatch.setenv("MAILER", f"> {output_file} echo")
308321
for rflag in ("-r", "--rites"):
309322
with (
310-
os_environ(
311-
"VISUAL", EDITOR="sed -i '1s/$/mask comment/'", MAILER=f"> {output_file} echo"
312-
),
313323
patch("sys.argv", self.args + ["cat/pkg", rflag, "--email"]),
314324
pytest.raises(SystemExit),
315325
chdir(pjoin(self.repo.path)),
@@ -321,10 +331,12 @@ def test_last_rites_with_email(self, tmp_path):
321331
self.masks_path.write_text("") # Reset the contents of package.mask
322332

323333
@pytest.mark.skipif(sys.platform == "darwin", reason="no xdg-email on mac os")
324-
def test_last_email_bad_mailer(self, capsys):
334+
def test_last_email_bad_mailer(self, capsys, monkeypatch):
335+
monkeypatch.delenv("VISUAL", raising=False)
336+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
337+
monkeypatch.setenv("MAILER", "false")
325338
for rflag in ("-r", "--rites"):
326339
with (
327-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'", MAILER="false"),
328340
patch("sys.argv", self.args + ["cat/pkg", rflag, "--email"]),
329341
pytest.raises(SystemExit),
330342
chdir(pjoin(self.repo.path)),
@@ -333,8 +345,10 @@ def test_last_email_bad_mailer(self, capsys):
333345
_, err = capsys.readouterr()
334346
assert err.strip() == "pkgdev mask: error: failed opening email composer"
335347

336-
def test_mask_bugs(self):
348+
def test_mask_bugs(self, monkeypatch):
337349
today = self.today.strftime("%Y-%m-%d")
350+
monkeypatch.delenv("VISUAL", raising=False)
351+
monkeypatch.setenv("EDITOR", "sed -i '1s/$/mask comment/'")
338352
for bflag in ("-b", "--bug"):
339353
for bug_nums, expected in [
340354
(["42"], "Bug #42."),
@@ -345,7 +359,6 @@ def test_mask_bugs(self):
345359
for bug_num in bug_nums:
346360
args += [bflag, bug_num]
347361
with (
348-
os_environ("VISUAL", EDITOR="sed -i '1s/$/mask comment/'"),
349362
patch("sys.argv", self.args + ["cat/pkg"] + args),
350363
pytest.raises(SystemExit),
351364
chdir(pjoin(self.repo.path)),

‎tests/scripts/test_pkgdev_showkw.py‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from unittest.mock import patch
66

77
import pytest
8-
from snakeoil.contexts import os_environ
98

109
from pkgdev.scripts import run
1110

@@ -41,7 +40,7 @@ def test_unknown_arches(self, capsys, tool, make_repo):
4140
"pkgdev showkw: error: unknown arch: 'unknown' (choices: amd64)"
4241
)
4342

44-
def test_no_color(self, tool, make_repo, tmp_path):
43+
def test_no_color(self, tool, make_repo, tmp_path, monkeypatch):
4544
repo = make_repo(arches=["amd64"])
4645
repo.create_ebuild("foo/bar-0", keywords=("x86"))
4746

@@ -60,14 +59,14 @@ def parse(*args):
6059
)
6160
return options
6261

63-
with os_environ("NOCOLOR"):
64-
assert parse().color is True
65-
with os_environ(NOCOLOR="1"):
66-
# NOCOLOR overrides config file
67-
assert parse().color is False
68-
# cmd line option overrides NOCOLOR
69-
assert parse("--color", "n").color is False
70-
assert parse("--color", "y").color is True
62+
monkeypatch.delenv("NOCOLOR", raising=False)
63+
assert parse().color is True
64+
monkeypatch.setenv("NOCOLOR", "1")
65+
# NOCOLOR overrides config file
66+
assert parse().color is False
67+
# cmd line option overrides NOCOLOR
68+
assert parse("--color", "n").color is False
69+
assert parse("--color", "y").color is True
7170

7271

7372
class TestPkgdevShowkw:

0 commit comments

Comments
 (0)