From 6f3a459a2a0eff93f2b57db8fd631633ec09a4f1 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 31 Oct 2024 11:20:09 -0400 Subject: [PATCH] fix: error if invalid reuse_venv set (#872) Signed-off-by: Henry Schreiner --- nox/sessions.py | 6 ++++++ tests/test_sessions.py | 8 ++++++++ 2 files changed, 14 insertions(+) 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()