Skip to content

Commit

Permalink
Disable running session.install outside a venv (#580)
Browse files Browse the repository at this point in the history
* Disable running `session.install` outside a venv

* Fix linting

* Replace the old test by a new one (almost)

Co-authored-by: Claudio Jolowicz <mail@claudiojolowicz.com>
  • Loading branch information
DiddiLeija and cjolowicz committed Mar 1, 2022
1 parent d590f58 commit 2093e74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
19 changes: 11 additions & 8 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import re
import sys
import unicodedata
import warnings
from types import TracebackType
from typing import Any, Callable, Iterable, Mapping, Sequence

Expand Down Expand Up @@ -533,6 +532,12 @@ def install(self, *args: str, **kwargs: Any) -> None:
Additional keyword args are the same as for :meth:`run`.
.. warning::
Running ``session.install`` without a virtual environment
is no longer supported. If you still want to do that, please
use ``session.run("pip", "install", ...)`` instead.
.. _pip: https://pip.readthedocs.org
"""
venv = self._runner.venv
Expand All @@ -544,13 +549,11 @@ def install(self, *args: str, **kwargs: Any) -> None:
"A session without a virtualenv can not install dependencies."
)
if isinstance(venv, PassthroughEnv):
warnings.warn(
f"Session {self.name} does not have a virtual environment, "
"so use of session.install() is deprecated since it would modify "
"the global Python environment. If you're really sure that is "
'what you want to do, use session.run("pip", "install", ...) instead.',
category=FutureWarning,
stacklevel=2,
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"
" global Python environment. If you're really sure that is what you"
' want to do, use session.run("pip", "install", ...) instead.'
)
if not args:
raise ValueError("At least one argument required to install().")
Expand Down
29 changes: 9 additions & 20 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ class SessionNoSlots(nox.sessions.Session):
external="error",
)

def test_install_no_venv_deprecated(self):
def test_install_no_venv_failure(self):
runner = nox.sessions.SessionRunner(
name="test",
signatures=["test"],
Expand All @@ -662,25 +662,14 @@ class SessionNoSlots(nox.sessions.Session):

session = SessionNoSlots(runner=runner)

with mock.patch.object(session, "_run", autospec=True) as run:
with pytest.warns(
FutureWarning,
match=(
r"use of session\.install\(\) is deprecated since it would modify"
r" the global Python environment"
),
):
session.install("requests", "urllib3")
run.assert_called_once_with(
"python",
"-m",
"pip",
"install",
"requests",
"urllib3",
silent=True,
external="error",
)
with pytest.raises(
ValueError,
match=(
r"use of session\.install\(\) is no longer allowed since"
r" it would modify the global Python environment"
),
):
session.install("requests", "urllib3")

def test_notify(self):
session, runner = self.make_session_and_runner()
Expand Down

0 comments on commit 2093e74

Please sign in to comment.