Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/poetry/console/commands/env/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class EnvActivateCommand(EnvCommand):
description = "Print the command to activate a virtual environment."

def handle(self) -> int:
from poetry.utils.env import EnvManager

env = EnvManager(self.poetry).get()
env = self.env

try:
shell, _ = shellingham.detect_shell()
Expand Down
29 changes: 26 additions & 3 deletions tests/console/commands/env/test_activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

from poetry.console.commands.env.activate import EnvActivateCommand
from poetry.console.commands.env.activate import ShellNotSupportedError
from poetry.utils._compat import WINDOWS

Expand Down Expand Up @@ -43,7 +44,8 @@ def test_env_activate_prints_correct_script(
ext: str,
) -> None:
mocker.patch("shellingham.detect_shell", return_value=(shell, None))
mocker.patch("poetry.utils.env.EnvManager.get", return_value=tmp_venv)
assert isinstance(tester.command, EnvActivateCommand)
tester.command.set_env(tmp_venv)

if WINDOWS and shell in {"csh", "tcsh"}:
with pytest.raises(ShellNotSupportedError):
Expand Down Expand Up @@ -74,7 +76,8 @@ def test_env_activate_prints_correct_script_for_windows_shells(
ext: str,
) -> None:
mocker.patch("shellingham.detect_shell", return_value=(shell, None))
mocker.patch("poetry.utils.env.EnvManager.get", return_value=tmp_venv)
assert isinstance(tester.command, EnvActivateCommand)
tester.command.set_env(tmp_venv)

tester.execute()

Expand All @@ -91,10 +94,30 @@ def test_no_additional_output_in_verbose_mode(
verbosity: str,
) -> None:
mocker.patch("shellingham.detect_shell", return_value=("pwsh", None))
mocker.patch("poetry.utils.env.EnvManager.get", return_value=tmp_venv)
mocker.patch("poetry.utils.env.EnvManager.create_venv", return_value=tmp_venv)

# use an AppTester instead of a CommandTester to catch additional output
app_tester.execute(f"env activate {verbosity}")

lines = app_tester.io.fetch_output().splitlines()
assert len(lines) == 1


def test_env_activate_uses_configured_environment(
tmp_venv: VirtualEnv,
mocker: MockerFixture,
tester: CommandTester,
) -> None:
mocker.patch("shellingham.detect_shell", return_value=("bash", None))
get_env = mocker.patch(
"poetry.utils.env.EnvManager.get",
side_effect=AssertionError("env activate should use the configured env"),
)
assert isinstance(tester.command, EnvActivateCommand)
tester.command.set_env(tmp_venv)

tester.execute()

assert not get_env.called
line = tester.io.fetch_output().rstrip("\n")
assert line == f"source {tmp_venv.bin_dir.as_posix()}/activate"
Loading