Skip to content

Commit

Permalink
Use poetry without shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus committed Oct 12, 2024
1 parent 56ee925 commit 14b422f
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 147 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ Note that this command starts a new shell and activates the virtual environment.

As such, `exit` should be used to properly exit the shell and the virtual environment instead of `deactivate`.

> ![NOTE]
> [!NOTE]
> This plugin internally uses the [Shellingham](https://github.com/sarugaku/shellingham) project to detect current active shell.
300 changes: 175 additions & 125 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include = [

[tool.poetry.dependencies]
python = "^3.8"
poetry = ">=1.8.0,<3.0.0"
poetry = { git = "https://github.com/Secrus/poetry.git", branch = "remove-shell" }
pexpect = "^4.7.0"
shellingham = "^1.5"

Expand Down
2 changes: 1 addition & 1 deletion src/poetry_plugin_shell/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ShellCommand(EnvCommand):
"""

def handle(self) -> int:
from poetry.utils.shell import Shell
from poetry_plugin_shell.shell import Shell

# Check if it's already activated or doesn't exist and won't be created
if self._is_venv_activated():
Expand Down
2 changes: 0 additions & 2 deletions src/poetry_plugin_shell/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def commands(self) -> list[type[Command]]:

def activate(self, application: Application) -> None:
# Removing the existing shell command to avoid an error

# If you're checking this code out to get inspiration
# for your own plugins: DON'T DO THIS!
if application.command_loader.has("shell"):
del application.command_loader._factories["shell"]

super().activate(application=application)
19 changes: 7 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest

from cleo.io.null_io import NullIO
from cleo.testers.command_tester import CommandTester
from poetry.config.config import Config as BaseConfig
from poetry.config.dict_config_source import DictConfigSource
Expand Down Expand Up @@ -179,15 +180,10 @@ def _factory(

poetry = Factory().create_poetry(project_dir)

try:
# with https://github.com/python-poetry/poetry/pull/9133
locker = TestLocker(
poetry.locker.lock,
poetry.locker._pyproject_data, # type: ignore[attr-defined]
)
except AttributeError:
# before https://github.com/python-poetry/poetry/pull/9133
locker = TestLocker(poetry.locker.lock, poetry.locker._local_config)
locker = TestLocker(
poetry.locker.lock,
poetry.locker._pyproject_data,
)
locker.write()

poetry.set_locker(locker)
Expand Down Expand Up @@ -221,12 +217,11 @@ def _tester(
executor: Executor | None = None,
environment: Env | None = None,
) -> CommandTester:
app._load_plugins(NullIO())

command_obj = app.find(command)
tester = CommandTester(command_obj)

print(id(tester.command))
print(tester.command.name)

# Setting the formatter from the application
# TODO: Find a better way to do this in Cleo
app_io = app.create_io()
Expand Down
5 changes: 0 additions & 5 deletions tests/test_shell_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ def tester(command_tester_factory: CommandTesterFactory) -> CommandTester:


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

tester.execute()
print(id(tester.command))
assert isinstance(tester.command, ShellCommand)
expected_output = f"Spawning shell within {tester.command.env.path}\n"

Expand All @@ -37,13 +35,11 @@ def test_shell(tester: CommandTester, mocker: MockerFixture) -> None:


def test_shell_already_active(tester: CommandTester, mocker: MockerFixture) -> None:
print(tester.command.name)
os.environ["POETRY_ACTIVE"] = "1"
shell_activate = mocker.patch("poetry_plugin_shell.shell.Shell.activate")

tester.execute()

print(id(tester.command))
assert isinstance(tester.command, ShellCommand)
expected_output = (
f"Virtual environment already activated: {tester.command.env.path}\n"
Expand Down Expand Up @@ -79,7 +75,6 @@ def test__is_venv_activated(
prefix: str,
expected: bool,
) -> None:
print(tester.command.name)
assert isinstance(tester.command, ShellCommand)
mocker.patch.object(tester.command.env, "_path", Path("foobar"))
mocker.patch("sys.prefix", prefix)
Expand Down

0 comments on commit 14b422f

Please sign in to comment.