From 9fe671c83685fd70c0105ac8b869972cd938c83c Mon Sep 17 00:00:00 2001 From: taconi Date: Sat, 11 Jan 2025 20:45:49 -0300 Subject: [PATCH 1/5] changing AssertionError to PoetryConsoleError --- src/poetry_plugin_shell/command.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/poetry_plugin_shell/command.py b/src/poetry_plugin_shell/command.py index 234ac85..0e3e9c2 100644 --- a/src/poetry_plugin_shell/command.py +++ b/src/poetry_plugin_shell/command.py @@ -7,6 +7,7 @@ from typing import cast from poetry.console.commands.env_command import EnvCommand +from poetry.console.exceptions import PoetryConsoleError if TYPE_CHECKING: @@ -40,7 +41,9 @@ 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(): + error_msg = "Virtual environment cannot be activated" + raise PoetryConsoleError(error_msg) env = cast("VirtualEnv", env) # Setting this to avoid spawning unnecessary nested shells From ccbc94973ec982d001b6a4bd9a20d834701a7668 Mon Sep 17 00:00:00 2001 From: taconi Date: Tue, 14 Jan 2025 16:00:07 -0300 Subject: [PATCH 2/5] fix error message when env is not a virtual environment --- src/poetry_plugin_shell/command.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/poetry_plugin_shell/command.py b/src/poetry_plugin_shell/command.py index 0e3e9c2..ef5bc23 100644 --- a/src/poetry_plugin_shell/command.py +++ b/src/poetry_plugin_shell/command.py @@ -7,7 +7,6 @@ from typing import cast from poetry.console.commands.env_command import EnvCommand -from poetry.console.exceptions import PoetryConsoleError if TYPE_CHECKING: @@ -42,8 +41,12 @@ def handle(self) -> int: # Be sure that we have the right type of environment. env = self.env if not env.is_venv(): - error_msg = "Virtual environment cannot be activated" - raise PoetryConsoleError(error_msg) + 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 From 9679a744d9cb1ec43b742fd83e41ca34d78df2b7 Mon Sep 17 00:00:00 2001 From: taconi Date: Tue, 14 Jan 2025 18:51:43 -0300 Subject: [PATCH 3/5] adding tests for the scenario where the environment is not a virtual environment --- tests/test_shell_command.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_shell_command.py b/tests/test_shell_command.py index 8b05732..069f559 100644 --- a/tests/test_shell_command.py +++ b/tests/test_shell_command.py @@ -86,3 +86,19 @@ 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") + tester.command.env.is_venv = lambda: False + + tester.execute() + assert isinstance(tester.command, ShellCommand) + 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 From 5df452393a530ad968f4e406bd96b2b8a69dc026 Mon Sep 17 00:00:00 2001 From: taconi Date: Tue, 14 Jan 2025 18:59:46 -0300 Subject: [PATCH 4/5] fix mypy in the tests file --- tests/test_shell_command.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_shell_command.py b/tests/test_shell_command.py index 069f559..8a2b3a5 100644 --- a/tests/test_shell_command.py +++ b/tests/test_shell_command.py @@ -90,7 +90,9 @@ def test__is_venv_activated( def test_is_not_venv(tester: CommandTester, mocker: MockerFixture) -> None: shell_activate = mocker.patch("poetry_plugin_shell.shell.Shell.activate") - tester.command.env.is_venv = lambda: False + + assert isinstance(tester.command, ShellCommand) + mocker.patch.object(tester.command.env, "is_venv", new=lambda: False) tester.execute() assert isinstance(tester.command, ShellCommand) From b964173f80225bfdc9be37903ee81af9a4bada8f Mon Sep 17 00:00:00 2001 From: taconi Date: Tue, 14 Jan 2025 19:03:02 -0300 Subject: [PATCH 5/5] removing duplicate assert --- tests/test_shell_command.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_shell_command.py b/tests/test_shell_command.py index 8a2b3a5..02d8f73 100644 --- a/tests/test_shell_command.py +++ b/tests/test_shell_command.py @@ -95,7 +95,6 @@ def test_is_not_venv(tester: CommandTester, mocker: MockerFixture) -> None: mocker.patch.object(tester.command.env, "is_venv", new=lambda: False) tester.execute() - assert isinstance(tester.command, ShellCommand) expected_output = ( f"The Python environment at {tester.command.env.path} " "cannot be activated as it is not a virtural environment.\n"