From c93ca8cc1bba792879ac59f42d52e03b1bccbaaa Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 6 Mar 2024 11:45:30 -0500 Subject: [PATCH] feat: add venv_backend property Signed-off-by: Henry Schreiner --- nox/sessions.py | 5 +++++ nox/virtualenv.py | 21 ++++++++++++++++++++- tests/test_sessions.py | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/nox/sessions.py b/nox/sessions.py index 2728b1fa..c1217022 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -193,6 +193,11 @@ def virtualenv(self) -> ProcessEnv: raise ValueError("A virtualenv has not been created for this session") return venv + @property + def venv_backend(self) -> str: + """The venv_backend selected.""" + return self._runner.venv.venv_backend + @property def python(self) -> str | Sequence[str] | bool | None: """The python version passed into ``@nox.session``.""" diff --git a/nox/virtualenv.py b/nox/virtualenv.py index 6f4b6099..2258a04b 100644 --- a/nox/virtualenv.py +++ b/nox/virtualenv.py @@ -93,6 +93,13 @@ def create(self) -> bool: Returns True if the environment is new, and False if it was reused. """ + @property + @abc.abstractmethod + def venv_backend(self) -> str: + """ + Returns the string used to select this environment. + """ + def locate_via_py(version: str) -> str | None: """Find the Python executable using the Windows Launcher. @@ -180,6 +187,10 @@ def create(self) -> bool: False since it's always reused.""" return False + @property + def venv_backend(self) -> str: + return "none" + class CondaEnv(ProcessEnv): """Conda environment management class. @@ -303,6 +314,10 @@ def is_offline() -> bool: except BaseException: # pragma: no cover return True + @property + def venv_backend(self) -> str: + return self.conda_cmd + class VirtualEnv(ProcessEnv): """Virtualenv management class. @@ -341,7 +356,7 @@ def __init__( self.interpreter = interpreter self._resolved: None | str | InterpreterNotFound = None self.reuse_existing = reuse_existing - self.venv_backend = venv_backend + self._venv_backend = venv_backend self.venv_params = venv_params or [] if venv_backend not in {"virtualenv", "venv", "uv"}: msg = f"venv_backend {venv_backend} not recognized" @@ -544,6 +559,10 @@ def create(self) -> bool: return True + @property + def venv_backend(self) -> str: + return self._venv_backend + ALL_VENVS: dict[str, Callable[..., ProcessEnv]] = { "conda": functools.partial(CondaEnv, conda_cmd="conda"), diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 98478a60..aa71226c 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -641,6 +641,8 @@ class SessionNoSlots(nox.sessions.Session): session = SessionNoSlots(runner=runner) + assert session.venv_backend == "venv" + with mock.patch.object(session, "_run", autospec=True) as run: session.install("requests", "urllib3") run.assert_called_once_with(