diff --git a/src/poetry_plugin_shell/command.py b/src/poetry_plugin_shell/command.py index 234ac85..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( diff --git a/tests/test_shell_command.py b/tests/test_shell_command.py index 8b05732..264abd1 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() @@ -50,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"), [