Skip to content

Commit

Permalink
Deprecate use of session.install() without a virtual env
Browse files Browse the repository at this point in the history
  • Loading branch information
Tolker-KU committed Dec 27, 2021
1 parent 1c078d6 commit 2e2d40b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nox/sessions.py
Expand Up @@ -20,6 +20,7 @@
import re
import sys
import unicodedata
import warnings
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -488,6 +489,15 @@ def install(self, *args: str, **kwargs: Any) -> None:
raise ValueError(
"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,
)
if not args:
raise ValueError("At least one argument required to install().")

Expand Down
32 changes: 32 additions & 0 deletions tests/test_sessions.py
Expand Up @@ -578,6 +578,38 @@ class SessionNoSlots(nox.sessions.Session):
external="error",
)

def test_install_no_venv_deprecated(self):
runner = nox.sessions.SessionRunner(
name="test",
signatures=["test"],
func=mock.sentinel.func,
global_config=_options.options.namespace(posargs=[]),
manifest=mock.create_autospec(nox.manifest.Manifest),
)
runner.venv = mock.create_autospec(nox.virtualenv.PassthroughEnv)
runner.venv.env = {}

class SessionNoSlots(nox.sessions.Session):
pass

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 the global Python environment"
):
session.install("requests", "urllib3")
run.assert_called_once_with(
"python",
"-m",
"pip",
"install",
"requests",
"urllib3",
silent=True,
external="error",
)

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

Expand Down

0 comments on commit 2e2d40b

Please sign in to comment.