From 31a3aa41925e6f1f1db330b4cced4767ecb5eb93 Mon Sep 17 00:00:00 2001 From: Kent Tonino Date: Fri, 10 Apr 2026 00:34:14 +0800 Subject: [PATCH 1/4] fix: should return false if poetry is active but not virtual env --- src/poetry_plugin_shell/command.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/poetry_plugin_shell/command.py b/src/poetry_plugin_shell/command.py index 234ac85..e3a9bea 100644 --- a/src/poetry_plugin_shell/command.py +++ b/src/poetry_plugin_shell/command.py @@ -52,6 +52,10 @@ def handle(self) -> int: return 0 def _is_venv_activated(self) -> bool: + if os.environ.get("POETRY_ACTIVE") and not os.environ.get("VIRTUAL_ENV"): + os.environ.pop("POETRY_ACTIVE") + return False + return bool(os.environ.get("POETRY_ACTIVE")) or getattr( sys, "real_prefix", sys.prefix ) == str(self.env.path) From 8d866af5017f62dd8a46c6d39f80ee55eb41f56b Mon Sep 17 00:00:00 2001 From: Kent Tonino Date: Fri, 10 Apr 2026 00:45:08 +0800 Subject: [PATCH 2/4] fix: pop the POETRY_ACTIVE if it is set but not the VIRTUAL_ENV --- src/poetry_plugin_shell/command.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/poetry_plugin_shell/command.py b/src/poetry_plugin_shell/command.py index e3a9bea..fb05ae6 100644 --- a/src/poetry_plugin_shell/command.py +++ b/src/poetry_plugin_shell/command.py @@ -28,6 +28,9 @@ class ShellCommand(EnvCommand): def handle(self) -> int: from poetry_plugin_shell.shell import Shell + if os.environ.get("POETRY_ACTIVE") == "1" and not os.environ.get("VIRTUAL_ENV"): + os.environ.pop("POETRY_ACTIVE") + # Check if it's already activated or doesn't exist and won't be created if self._is_venv_activated(): self.line( @@ -52,10 +55,6 @@ def handle(self) -> int: return 0 def _is_venv_activated(self) -> bool: - if os.environ.get("POETRY_ACTIVE") and not os.environ.get("VIRTUAL_ENV"): - os.environ.pop("POETRY_ACTIVE") - return False - return bool(os.environ.get("POETRY_ACTIVE")) or getattr( sys, "real_prefix", sys.prefix ) == str(self.env.path) From c08095e49622f02108e563fe13e63c1030d436db Mon Sep 17 00:00:00 2001 From: Kent Tonino Date: Fri, 10 Apr 2026 00:57:14 +0800 Subject: [PATCH 3/4] fix: set the VIRTUAL_ENV in test --- tests/test_shell_command.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_shell_command.py b/tests/test_shell_command.py index 8b05732..c24fa30 100644 --- a/tests/test_shell_command.py +++ b/tests/test_shell_command.py @@ -35,7 +35,9 @@ def test_shell(tester: CommandTester, mocker: MockerFixture) -> None: def test_shell_already_active(tester: CommandTester, mocker: MockerFixture) -> None: + assert isinstance(tester.command, ShellCommand) os.environ["POETRY_ACTIVE"] = "1" + os.environ["VIRTUAL_ENV"] = str(tester.command.env.path) shell_activate = mocker.patch("poetry_plugin_shell.shell.Shell.activate") tester.execute() From 96ebc6bbaaea6745b4ae8ae0b20f900b39ee5de0 Mon Sep 17 00:00:00 2001 From: Kent Tonino Date: Sun, 7 Jun 2026 21:16:43 +0800 Subject: [PATCH 4/4] feat: assert POETRY_ACTIVE without VIRTUAL_ENV path --- tests/test_shell_command.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_shell_command.py b/tests/test_shell_command.py index c24fa30..264abd1 100644 --- a/tests/test_shell_command.py +++ b/tests/test_shell_command.py @@ -52,6 +52,23 @@ def test_shell_already_active(tester: CommandTester, mocker: MockerFixture) -> N assert tester.status_code == 0 +def test_shell_stale_poetry_active( + tester: CommandTester, mocker: MockerFixture +) -> None: + assert isinstance(tester.command, ShellCommand) + os.environ["POETRY_ACTIVE"] = "1" + shell_activate = mocker.patch("poetry_plugin_shell.shell.Shell.activate") + + tester.execute() + + assert isinstance(tester.command, ShellCommand) + expected_output = f"Spawning shell within {tester.command.env.path}\n" + + shell_activate.assert_called_once_with(tester.command.env) + assert tester.io.fetch_output() == expected_output + assert tester.status_code == 0 + + @pytest.mark.parametrize( ("poetry_active", "real_prefix", "prefix", "expected"), [