Skip to content

Commit 62c12fe

Browse files
committed
replace dangerous monkeypatch with mocker.patch
1 parent b5ba32f commit 62c12fe

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

tests/test_discovery.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pytest_mock import MockerFixture
1717

1818

19-
def test_local_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]) -> None:
19+
def test_local_only(tmp_path: Path, mocker: MockerFixture, capfd: pytest.CaptureFixture[str]) -> None:
2020
venv_path = str(tmp_path / "venv")
2121
result = virtualenv.cli_run([venv_path, "--activators", ""])
2222
venv_site_packages = site.getsitepackages([venv_path])
@@ -27,10 +27,9 @@ def test_local_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pyte
2727
f.write("Metadata-Version: 2.3\n" "Name: foo\n" "Version: 1.2.5\n")
2828

2929
cmd = [str(result.creator.exe.parent / "python3"), "--local-only"]
30-
monkeypatch.setattr(sys, "prefix", venv_path)
31-
for s in venv_site_packages:
32-
monkeypatch.syspath_prepend(s)
33-
monkeypatch.setattr(sys, "argv", cmd)
30+
mocker.patch("pipdeptree._discovery.sys.prefix", venv_path)
31+
mocker.patch("pipdeptree._discovery.sys.path", venv_site_packages)
32+
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
3433
main()
3534
out, _ = capfd.readouterr()
3635
found = {i.split("==")[0] for i in out.splitlines()}
@@ -41,16 +40,16 @@ def test_local_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pyte
4140
assert found == expected
4241

4342

44-
def test_user_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]) -> None:
43+
def test_user_only(tmp_path: Path, mocker: MockerFixture, capfd: pytest.CaptureFixture[str]) -> None:
4544
fake_dist = Path(tmp_path) / "foo-1.2.5.dist-info"
4645
fake_dist.mkdir()
4746
fake_metadata = Path(fake_dist) / "METADATA"
4847
with Path(fake_metadata).open("w") as f:
4948
f.write("Metadata-Version: 2.3\n" "Name: foo\n" "Version: 1.2.5\n")
5049

51-
monkeypatch.setattr(site, "getusersitepackages", Mock(return_value=str(tmp_path)))
5250
cmd = [sys.executable, "--user-only"]
53-
monkeypatch.setattr(sys, "argv", cmd)
51+
mocker.patch("pipdeptree._discovery.site.getusersitepackages", Mock(return_value=str(tmp_path)))
52+
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
5453
main()
5554
out, _ = capfd.readouterr()
5655
found = {i.split("==")[0] for i in out.splitlines()}

tests/test_non_host.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
if TYPE_CHECKING:
1313
from pathlib import Path
1414

15+
from pytest_mock import MockerFixture
16+
1517

1618
@pytest.mark.parametrize("args_joined", [True, False])
1719
def test_custom_interpreter(
1820
tmp_path: Path,
21+
mocker: MockerFixture,
1922
monkeypatch: pytest.MonkeyPatch,
2023
capfd: pytest.CaptureFixture[str],
2124
args_joined: bool,
@@ -25,7 +28,7 @@ def test_custom_interpreter(
2528
monkeypatch.chdir(tmp_path)
2629
py = str(result.creator.exe.relative_to(tmp_path))
2730
cmd += [f"--python={result.creator.exe}"] if args_joined else ["--python", py]
28-
monkeypatch.setattr(sys, "argv", cmd)
31+
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
2932
main()
3033
out, _ = capfd.readouterr()
3134
found = {i.split("==")[0] for i in out.splitlines()}
@@ -43,16 +46,16 @@ def test_custom_interpreter(
4346

4447
def test_custom_interpreter_with_local_only(
4548
tmp_path: Path,
46-
monkeypatch: pytest.MonkeyPatch,
49+
mocker: MockerFixture,
4750
capfd: pytest.CaptureFixture[str],
4851
) -> None:
4952
venv_path = str(tmp_path / "venv")
5053

5154
result = virtualenv.cli_run([venv_path, "--system-site-packages", "--activators", ""])
5255

5356
cmd = ["", f"--python={result.creator.exe}", "--local-only"]
54-
monkeypatch.setattr(sys, "prefix", venv_path)
55-
monkeypatch.setattr(sys, "argv", cmd)
57+
mocker.patch("pipdeptree._discovery.sys.prefix", venv_path)
58+
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
5659
main()
5760
out, _ = capfd.readouterr()
5861
found = {i.split("==")[0] for i in out.splitlines()}

0 commit comments

Comments
 (0)