Skip to content

Commit

Permalink
tests: monkeypatch lower down for new test
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Apr 12, 2024
1 parent 8f12d60 commit 7415271
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
8 changes: 6 additions & 2 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,12 @@ def _run(
return self._run_func(args[0], args[1:]) # 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:
Expand Down
41 changes: 35 additions & 6 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,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"],
Expand All @@ -867,20 +866,50 @@ 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",
"urllib3",
**_run_with_defaults(silent=False, 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):
Expand Down

0 comments on commit 7415271

Please sign in to comment.