From 36daaed9cdf710ab06f76d94bd9500f2fd251b15 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 12 Apr 2024 17:48:47 -0400 Subject: [PATCH] tests: monkeypatch lower down for new test Signed-off-by: Henry Schreiner --- nox/sessions.py | 8 ++++++-- tests/test_sessions.py | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/nox/sessions.py b/nox/sessions.py index aa7f400f..47277b0b 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -500,8 +500,12 @@ def _run( return self._run_func(args[0], args[1:], kwargs) # type: ignore[unreachable] # Using `"uv"` when `uv` is the backend is guaranteed to work, even if it was co-installed with nox. - if self.virtualenv.venv_backend == "uv" and args[0] == "uv" and nox.virtualenv.UV != "uv": - args = (nox.virualenv.UV, *args[1:]) + if ( + self.virtualenv.venv_backend == "uv" + and args[0] == "uv" + and nox.virtualenv.UV != "uv" + ): + args = (nox.virtualenv.UV, *args[1:]) # Combine the env argument with our virtualenv's env vars. if include_outer_env: diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 3696816e..0f486458 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -823,8 +823,7 @@ def test_session_venv_reused_with_no_install(self, no_install, reused, run_calle assert run.called is run_called - @pytest.mark.parametrize("uv", [None, "/some/uv"]) - def test_install_uv(self, uv, monkeypatch): + def test_install_uv(self): runner = nox.sessions.SessionRunner( name="test", signatures=["test"], @@ -841,13 +840,10 @@ class SessionNoSlots(nox.sessions.Session): session = SessionNoSlots(runner=runner) - if uv is not None: - monkeypatch.setattr(nox.virtualenv, "UV", uv) - with mock.patch.object(session, "_run", autospec=True) as run: session.install("requests", "urllib3", silent=False) run.assert_called_once_with( - nox.virtualenv.UV, + "uv", "pip", "install", "requests", @@ -856,6 +852,39 @@ class SessionNoSlots(nox.sessions.Session): external="error", ) + def test_install_uv_command(self, monkeypatch): + runner = nox.sessions.SessionRunner( + name="test", + signatures=["test"], + func=mock.sentinel.func, + global_config=_options.options.namespace(posargs=[]), + manifest=mock.create_autospec(nox.manifest.Manifest), + ) + runner.venv = mock.create_autospec(nox.virtualenv.VirtualEnv) + runner.venv.env = {} + runner.venv.venv_backend = "uv" + + class SessionNoSlots(nox.sessions.Session): + pass + + session = SessionNoSlots(runner=runner) + + monkeypatch.setattr(nox.virtualenv, "UV", "/some/uv") + monkeypatch.setattr(shutil, "which", lambda x: None) + + with mock.patch.object(nox.command, "run", autospec=True) as run: + session.install("requests", "urllib3", silent=False) + run.assert_called_once() + + ((call_args,), _) = run.call_args + assert call_args == ( + "/some/uv", + "pip", + "install", + "requests", + "urllib3", + ) + def test___slots__(self): session, _ = self.make_session_and_runner() with pytest.raises(AttributeError):