diff --git a/src/poetry_plugin_shell/command.py b/src/poetry_plugin_shell/command.py index 234ac85..ef5bc23 100644 --- a/src/poetry_plugin_shell/command.py +++ b/src/poetry_plugin_shell/command.py @@ -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 {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 diff --git a/tests/test_shell_command.py b/tests/test_shell_command.py index 8b05732..02d8f73 100644 --- a/tests/test_shell_command.py +++ b/tests/test_shell_command.py @@ -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