diff --git a/nox/sessions.py b/nox/sessions.py index 432db0ca..57cc7bd0 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -565,7 +565,7 @@ def conda_install( prefix_args: tuple[str, ...] = () if isinstance(venv, CondaEnv): prefix_args = ("--prefix", venv.location) - elif not isinstance(venv, PassthroughEnv): # pragma: no cover + elif not isinstance(venv, PassthroughEnv): raise ValueError( "A session without a conda environment can not install dependencies" " from conda." @@ -574,7 +574,9 @@ def conda_install( if not args: raise ValueError("At least one argument required to install().") - if self._runner.global_config.no_install and venv._reused: + if self._runner.global_config.no_install and ( + isinstance(venv, PassthroughEnv) or venv._reused + ): return # Escape args that should be (conda-specific; pip install does not need this) @@ -648,6 +650,8 @@ def install(self, *args: str, **kwargs: Any) -> None: "A session without a virtualenv can not install dependencies." ) if isinstance(venv, PassthroughEnv): + if self._runner.global_config.no_install: + return raise ValueError( f"Session {self.name} does not have a virtual environment, so use of" " session.install() is no longer allowed since it would modify the" diff --git a/tests/test_sessions.py b/tests/test_sessions.py index e6006eaf..e720c61b 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -374,6 +374,20 @@ def test_run_install_bad_args(self): exc_args = exc_info.value.args assert exc_args == ("At least one argument required to run_install().",) + def test_run_no_install_passthrough(self): + session, runner = self.make_session_and_runner() + runner.venv = nox.virtualenv.PassthroughEnv() + runner.global_config.no_install = True + + session.install("numpy") + session.conda_install("numpy") + + def test_run_no_conda_install(self): + session, runner = self.make_session_and_runner() + + with pytest.raises(ValueError, match="A session without a conda"): + session.conda_install("numpy") + def test_run_install_success(self): session, _ = self.make_session_and_runner()