diff --git a/nox/sessions.py b/nox/sessions.py index 396a09fc..9a92c310 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -167,10 +167,12 @@ def bin_paths(self) -> Optional[List[str]]: return self.virtualenv.bin_paths @property - def bin(self) -> Optional[str]: + def bin(self) -> str: """The first bin directory for the virtualenv.""" paths = self.bin_paths - return paths[0] if paths is not None else None + if paths is None: + raise ValueError("The environment does not have a bin directory.") + return paths[0] def create_tmp(self) -> str: """Create, and return, a temporary directory.""" diff --git a/nox/virtualenv.py b/nox/virtualenv.py index c32efaf2..eba573e7 100644 --- a/nox/virtualenv.py +++ b/nox/virtualenv.py @@ -69,10 +69,12 @@ def bin_paths(self) -> Optional[List[str]]: return self._bin_paths @property - def bin(self) -> Optional[str]: + def bin(self) -> str: """The first bin directory for the virtualenv.""" paths = self.bin_paths - return paths[0] if paths is not None else None + if paths is None: + raise ValueError("The environment does not have a bin directory.") + return paths[0] def create(self) -> bool: raise NotImplementedError("ProcessEnv.create should be overwritten in subclass") diff --git a/tests/test_sessions.py b/tests/test_sessions.py index e1e19eae..f52c1639 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -108,7 +108,10 @@ def test_no_bin_paths(self): session, runner = self.make_session_and_runner() runner.venv.bin_paths = None - assert session.bin is None + with pytest.raises( + ValueError, match=r"^The environment does not have a bin directory\.$" + ): + session.bin assert session.bin_paths is None def test_virtualenv_as_none(self): diff --git a/tests/test_virtualenv.py b/tests/test_virtualenv.py index eb67694a..7200d2d1 100644 --- a/tests/test_virtualenv.py +++ b/tests/test_virtualenv.py @@ -121,10 +121,17 @@ def mock_sysfind(arg): def test_process_env_constructor(): penv = nox.virtualenv.ProcessEnv() assert not penv.bin_paths + with pytest.raises( + ValueError, match=r"^The environment does not have a bin directory\.$" + ): + penv.bin penv = nox.virtualenv.ProcessEnv(env={"SIGIL": "123"}) assert penv.env["SIGIL"] == "123" + penv = nox.virtualenv.ProcessEnv(bin_paths=["/bin"]) + assert penv.bin == "/bin" + def test_process_env_create(): penv = nox.virtualenv.ProcessEnv()