Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace shell command with activator
Browse files Browse the repository at this point in the history
Secrus committed Oct 14, 2024
1 parent a9793c9 commit bfaa2b3
Showing 9 changed files with 144 additions and 347 deletions.
28 changes: 1 addition & 27 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -42,7 +42,6 @@ installer = "^0.7.0"
keyring = "^25.1.0"
# packaging uses calver, so version is unclamped
packaging = ">=24.0"
pexpect = "^4.7.0"
pkginfo = "^1.10"
platformdirs = ">=3.0.0,<5"
pyproject-hooks = "^1.0.0"
2 changes: 1 addition & 1 deletion src/poetry/console/application.py
Original file line number Diff line number Diff line change
@@ -62,7 +62,6 @@ def _load() -> Command:
"remove",
"run",
"search",
"shell",
"show",
"update",
"version",
@@ -73,6 +72,7 @@ def _load() -> Command:
"debug info",
"debug resolve",
# Env commands
"env activate",
"env info",
"env list",
"env remove",
64 changes: 64 additions & 0 deletions src/poetry/console/commands/env/activate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import annotations

import shlex

from typing import TYPE_CHECKING

import shellingham

from poetry.console.commands.command import Command
from poetry.utils._compat import WINDOWS


if TYPE_CHECKING:
from poetry.utils.env import Env


class ShellNotSupportedError(Exception):
"""Raised when a shell doesn't have an activator in virtual environment"""


class EnvActivateCommand(Command):
name = "env activate"
description = "Print the command to activate a virtual environment"

def handle(self) -> int:
from poetry.utils.env import EnvManager

env = EnvManager(self.poetry).get()

if command := self.get_activate_command(env):
self.line(command)
return 0
else:
raise ShellNotSupportedError(
"Discovered shell doesn't have an activator in virtual environment"
)

def get_activate_command(self, env: Env) -> str:
try:
shell, _ = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
shell = ""
if shell == "fish":
command, filename = "source", "activate.fish"
elif shell == "nu":
command, filename = "overlay use", "activate.nu"
elif shell == "csh":
command, filename = "source", "activate.csh"
elif shell in ["powershell", "pwsh"]:
command, filename = ".", "Activate.ps1"
else:
command, filename = "source", "activate"

if (activation_script := env.bin_dir / filename).exists():
if WINDOWS:
return f"{self.quote(str(activation_script), shell)}"
return f"{command} {self.quote(str(activation_script), shell)}"
return ""

@staticmethod
def quote(command: str, shell: str) -> str:
if shell in ["powershell", "pwsh"] or WINDOWS:
return "{}".format(command.replace("'", "''"))
return shlex.quote(command)
57 changes: 0 additions & 57 deletions src/poetry/console/commands/shell.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
@@ -67,6 +67,10 @@ def __init__(self, path: Path, base: Path | None = None) -> None:

self._embedded_pip_path: Path | None = None

@property
def bin_dir(self) -> Path:
return self._bin_dir

@property
def path(self) -> Path:
return self._path
172 changes: 0 additions & 172 deletions src/poetry/utils/shell.py

This file was deleted.

74 changes: 74 additions & 0 deletions tests/console/commands/env/test_activate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from poetry.utils._compat import WINDOWS


if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture

from poetry.utils.env import VirtualEnv
from tests.types import CommandTesterFactory


@pytest.fixture
def tester(command_tester_factory: CommandTesterFactory) -> CommandTester:
return command_tester_factory("env activate")


@pytest.mark.parametrize(
"shell, command, ext",
(
("bash", "source", ""),
("zsh", "source", ""),
("fish", "source", ".fish"),
("nu", "overlay use", ".nu"),
("csh", "source", ".csh"),
),
)
@pytest.mark.skipif(WINDOWS, reason="Only Unix shells")
def test_env_activate_prints_correct_script_windows(
tmp_venv: VirtualEnv,
mocker: MockerFixture,
tester: CommandTester,
shell: str,
command: str,
ext: str,
) -> None:
mocker.patch("shellingham.detect_shell", return_value=(shell, None))
mocker.patch("poetry.utils.env.EnvManager.get", return_value=tmp_venv)

tester.execute()

line = tester.io.fetch_output().split("\n")[0]
assert line.startswith(command)
assert line.endswith(f"activate{ext}")


@pytest.mark.parametrize(
"shell, command, ext",
(
("pwsh", ".", "Activate.ps1"),
("powershell", ".", "Activate.ps1"),
),
)
@pytest.mark.skipif(not WINDOWS, reason="Only Windows shells")
def test_env_activate_prints_correct_script(
tmp_venv: VirtualEnv,
mocker: MockerFixture,
tester: CommandTester,
shell: str,
command: str,
ext: str,
) -> None:
mocker.patch("shellingham.detect_shell", return_value=(shell, None))
mocker.patch("poetry.utils.env.EnvManager.get", return_value=tmp_venv)

tester.execute()

line = tester.io.fetch_output().split("\n")[0]
assert line == str(tmp_venv.bin_dir / ext)
89 changes: 0 additions & 89 deletions tests/console/commands/test_shell.py

This file was deleted.

0 comments on commit bfaa2b3

Please sign in to comment.