Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable running session.install outside a venv #580

Merged
merged 4 commits into from Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions nox/sessions.py
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(
DiddiLeija marked this conversation as resolved.
Show resolved Hide resolved
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
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",
)
DiddiLeija marked this conversation as resolved.
Show resolved Hide resolved
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