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
14 changes: 11 additions & 3 deletions src/poetry_plugin_shell/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ def handle(self) -> int:

return 0

self.line(f"Spawning shell within <info>{self.env.path}</>")

# Be sure that we have the right type of environment.
env = self.env
assert env.is_venv()
if not env.is_venv():
self.line_error(
"<error>Poetry could not find a virtual environment to activate.</>\n"
"If <info>virtualenvs.create</> is set to <comment>false</>, "
"enable virtual environment creation or create/select a virtual "
"environment before running <info>poetry shell</>."
)
return 1

self.line(f"Spawning shell within <info>{self.env.path}</>")

env = cast("VirtualEnv", env)

# Setting this to avoid spawning unnecessary nested shells
Expand Down
21 changes: 21 additions & 0 deletions tests/test_shell_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import pytest

from poetry.utils.env import MockEnv

from poetry_plugin_shell.command import ShellCommand


Expand Down Expand Up @@ -50,6 +52,25 @@ def test_shell_already_active(tester: CommandTester, mocker: MockerFixture) -> N
assert tester.status_code == 0


def test_shell_without_virtualenv_shows_error(
command_tester_factory: CommandTesterFactory,
mocker: MockerFixture,
tmp_path: Path,
) -> None:
env = MockEnv(path=tmp_path / "system", is_venv=False)
tester = command_tester_factory("shell", environment=env)
shell_activate = mocker.patch("poetry_plugin_shell.shell.Shell.activate")

tester.execute()

error = tester.io.fetch_error()
shell_activate.assert_not_called()
assert "Spawning shell" not in tester.io.fetch_output()
assert "could not find a virtual environment" in error
assert "virtualenvs.create" in error
assert tester.status_code == 1


@pytest.mark.parametrize(
("poetry_active", "real_prefix", "prefix", "expected"),
[
Expand Down
Loading