diff --git a/nox/sessions.py b/nox/sessions.py index 3a51d751..5eb2b93a 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -1053,6 +1053,12 @@ def reuse_existing_venv(self) -> bool: Returns: bool: True if the existing virtual environment should be reused, False otherwise. """ + if self.global_config.reuse_venv not in {"always", "never", "no", "yes", None}: + msg = ( + "nox.options.reuse_venv must be set to 'always', 'never', 'no', or 'yes'," + f" got {self.global_config.reuse_venv!r}!" + ) + raise AttributeError(msg) return any( ( diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 6f55988d..b55d3bc0 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -18,6 +18,7 @@ import logging import operator import os +import re import shutil import subprocess import sys @@ -1145,6 +1146,13 @@ def test__reuse_venv_outcome(self, reuse_venv, reuse_venv_func, should_reuse): runner.global_config.reuse_venv = reuse_venv assert runner.reuse_existing_venv() == should_reuse + def test__reuse_venv_invalid(self): + runner = self.make_runner() + runner.global_config.reuse_venv = True + msg = "nox.options.reuse_venv must be set to 'always', 'never', 'no', or 'yes', got True!" + with pytest.raises(AttributeError, match=re.escape(msg)): + runner.reuse_existing_venv() + def make_runner_with_mock_venv(self): runner = self.make_runner() runner._create_venv = mock.Mock()