Skip to content

Add activate command alias for env commad #10339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/managing-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ The `poetry env activate` command prints the activate command of the virtual env
You can run the output command manually or feed it to the eval command of your shell to activate the environment.
This way you won't leave the current shell.

The `poetry activate` command acts as an alias for `poetry env activate` and is interchangeable with each other.

{{< tabs tabTotal="3" tabID1="bash-csh-zsh" tabID2="fish" tabID3="powershell" tabName1="Bash/Zsh/Csh" tabName2="Fish" tabName3="Powershell" >}}

{{< tab tabID="bash-csh-zsh" >}}
Expand Down
2 changes: 2 additions & 0 deletions src/poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def _load() -> Command:
COMMANDS = [
"about",
"add",
# Alias commands
"activate",
"build",
"check",
"config",
Expand Down
8 changes: 8 additions & 0 deletions src/poetry/console/commands/activate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from poetry.console.commands.env.activate import EnvActivateCommand


class ActivateCommand(EnvActivateCommand):
name = "activate"
description = "Alias for the env activate command"
92 changes: 92 additions & 0 deletions tests/console/commands/test_activate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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("activate")


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


@pytest.mark.parametrize(
"shell, command, ext",
(
("bash", "source", ""),
("zsh", "source", ""),
("fish", "source", ".fish"),
("nu", "overlay use", ".nu"),
("csh", "source", ".csh"),
("tcsh", "source", ".csh"),
),
)
@pytest.mark.skipif(WINDOWS, reason="Only Unix shells")
def test_activate_prints_correct_script_and_matches_env_activate(
tmp_venv: VirtualEnv,
mocker: MockerFixture,
tester: CommandTester,
env_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()
env_tester.execute()

line = tester.io.fetch_output().split("\n")[0]
env_line = env_tester.io.fetch_output().split("\n")[0]

assert line.startswith(command)
assert line.endswith(f"activate{ext}")
assert line == env_line


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

tester.execute()
env_tester.execute()

line = tester.io.fetch_output().split("\n")[0]
env_line = env_tester.io.fetch_output().split("\n")[0]

assert line == f'{prefix}"{tmp_venv.bin_dir / ext!s}"'
assert line == env_line
Loading