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

fix error message when env is not a virtual environment #31

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
8 changes: 7 additions & 1 deletion src/poetry_plugin_shell/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ def handle(self) -> int:

# Be sure that we have the right type of environment.
env = self.env
assert env.is_venv()
if not env.is_venv():
self.line("")
self.line_error(
f"The Python environment at <info>{env.path}</> "
"cannot be activated as it is not a virtural environment."
)
return 1
env = cast("VirtualEnv", env)

# Setting this to avoid spawning unnecessary nested shells
Expand Down
17 changes: 17 additions & 0 deletions tests/test_shell_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ def test__is_venv_activated(
os.environ["POETRY_ACTIVE"] = poetry_active

assert tester.command._is_venv_activated() is expected


def test_is_not_venv(tester: CommandTester, mocker: MockerFixture) -> None:
shell_activate = mocker.patch("poetry_plugin_shell.shell.Shell.activate")

assert isinstance(tester.command, ShellCommand)
mocker.patch.object(tester.command.env, "is_venv", new=lambda: False)

tester.execute()
expected_output = (
f"The Python environment at {tester.command.env.path} "
"cannot be activated as it is not a virtural environment.\n"
)

shell_activate.assert_not_called()
assert tester.io.fetch_error() == expected_output
assert tester.status_code == 1
Loading