From 7b6da50c93fe1090c14bd49f31f1de1f966833a7 Mon Sep 17 00:00:00 2001 From: Zhengshou Lai Date: Sat, 18 Jul 2026 11:47:08 +0800 Subject: [PATCH] test(upgrade): make pip-flag tests hermetic, cover uv branch uv run injects VIRTUAL_ENV, which silently selects the uv pip branch and broke test_uses_break_system_packages_on_externally_managed. Both tests now delete VIRTUAL_ENV to pin the plain-pip branch; new test pins the uv branch via mocks. --- tests/test_upgrade.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_upgrade.py b/tests/test_upgrade.py index cb79967..21c4889 100644 --- a/tests/test_upgrade.py +++ b/tests/test_upgrade.py @@ -238,6 +238,9 @@ class TestPipInstallFlags: ) -> None: import sysconfig + # Force the plain-pip branch regardless of how pytest was launched + # (e.g. `uv run` sets VIRTUAL_ENV, which would select the uv branch). + monkeypatch.delenv("VIRTUAL_ENV", raising=False) em = tmp_path / "EXTERNALLY-MANAGED" em.write_text("[externally-managed]\n") monkeypatch.setattr( @@ -257,6 +260,7 @@ class TestPipInstallFlags: ) -> None: import sysconfig + monkeypatch.delenv("VIRTUAL_ENV", raising=False) monkeypatch.setattr( sysconfig, "get_path", lambda name: str(tmp_path) if name == "stdlib" else "" ) @@ -268,3 +272,18 @@ class TestPipInstallFlags: ) up_mod._pip_install(tmp_path / "pkg") assert "--break-system-packages" not in seen[0] + + def test_prefers_uv_pip_inside_virtualenv( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + monkeypatch.setenv("VIRTUAL_ENV", str(tmp_path / "venv")) + monkeypatch.setattr(up_mod.shutil, "which", lambda name: f"/usr/bin/{name}") + seen: list[list[str]] = [] + monkeypatch.setattr( + "subprocess.run", + lambda cmd, **kw: seen.append(cmd) + or type("R", (), {"returncode": 0, "stderr": ""})(), + ) + up_mod._pip_install(tmp_path / "pkg") + assert seen[0][:3] == ["uv", "pip", "install"] + assert "--break-system-packages" not in seen[0]