Skip to content

Commit

Permalink
feat: add venv_backend property
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Mar 6, 2024
1 parent 545a621 commit c93ca8c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``."""
Expand Down
21 changes: 20 additions & 1 deletion nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"),
Expand Down
2 changes: 2 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c93ca8c

Please sign in to comment.