Skip to content

Commit 60bc27c

Browse files
committed
replace dangerous monkeypatch with mocker.patch
1 parent b5ba32f commit 60bc27c

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

tests/_models/test_package.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import sys
34
from importlib.metadata import PackageNotFoundError
45
from pathlib import Path
56
from typing import TYPE_CHECKING, Any
@@ -26,11 +27,13 @@ def test_guess_version_setuptools(mocker: MockerFixture) -> None:
2627
assert result == "?"
2728

2829

29-
def test_package_as_frozen_repr(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
30+
def test_package_as_frozen_repr(tmp_path: Path, mocker: MockerFixture) -> None:
3031
file_path = tmp_path / "foo.egg-link"
3132
with Path(file_path).open("w") as f:
3233
f.write("/A/B/foo")
33-
monkeypatch.syspath_prepend(str(tmp_path))
34+
mock_path = sys.path.copy()
35+
mock_path.append(str(tmp_path))
36+
mocker.patch("pipdeptree._discovery.sys.path", mock_path)
3437
json_text = '{"dir_info": {"editable": true}}'
3538
foo = Mock(metadata={"Name": "foo"}, version="20.4.1")
3639
foo.read_text = Mock(return_value=json_text)

tests/test_discovery.py

Lines changed: 9 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,11 @@ 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+
sys_path = sys.path.copy()
32+
mock_path = sys_path + venv_site_packages
33+
mocker.patch("pipdeptree._discovery.sys.path", mock_path)
34+
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
3435
main()
3536
out, _ = capfd.readouterr()
3637
found = {i.split("==")[0] for i in out.splitlines()}
@@ -41,16 +42,16 @@ def test_local_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pyte
4142
assert found == expected
4243

4344

44-
def test_user_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]) -> None:
45+
def test_user_only(tmp_path: Path, mocker: MockerFixture, capfd: pytest.CaptureFixture[str]) -> None:
4546
fake_dist = Path(tmp_path) / "foo-1.2.5.dist-info"
4647
fake_dist.mkdir()
4748
fake_metadata = Path(fake_dist) / "METADATA"
4849
with Path(fake_metadata).open("w") as f:
4950
f.write("Metadata-Version: 2.3\n" "Name: foo\n" "Version: 1.2.5\n")
5051

51-
monkeypatch.setattr(site, "getusersitepackages", Mock(return_value=str(tmp_path)))
5252
cmd = [sys.executable, "--user-only"]
53-
monkeypatch.setattr(sys, "argv", cmd)
53+
mocker.patch("pipdeptree._discovery.site.getusersitepackages", Mock(return_value=str(tmp_path)))
54+
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
5455
main()
5556
out, _ = capfd.readouterr()
5657
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)