Skip to content
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

Fix shell detection failure #2188

Merged
merged 2 commits into from
Aug 17, 2023
Merged
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
1 change: 1 addition & 0 deletions news/2187.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Default behavior for pdm venv activate when shell detection fails.
5 changes: 4 additions & 1 deletion src/pdm/cli/commands/venv/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
project.core.ui.echo(self.get_activate_command(venv))

def get_activate_command(self, venv: VirtualEnv) -> str: # pragma: no cover
shell, _ = shellingham.detect_shell()
try:
shell, _ = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
shell = None
if shell == "fish":
command, filename = "source", "activate.fish"
elif shell == "csh":
Expand Down
21 changes: 21 additions & 0 deletions tests/cli/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest.mock import ANY

import pytest
import shellingham

from pdm.cli.commands.venv import backends
from pdm.cli.commands.venv.utils import get_venv_prefix
Expand Down Expand Up @@ -168,6 +169,26 @@ def test_venv_activate_error(pdm, project):
assert "Can't activate a non-venv Python" in result.stderr


@pytest.mark.usefixtures("venv_backends")
def test_venv_activate_no_shell(pdm, mocker, project):
project.project_config["venv.in_project"] = False
result = pdm(["venv", "create"], obj=project)
assert result.exit_code == 0, result.stderr
venv_path = re.match(r"Virtualenv (.+) is created successfully", result.output).group(1)
key = os.path.basename(venv_path)[len(get_venv_prefix(project)) :]

mocker.patch("shellingham.detect_shell", side_effect=shellingham.ShellDetectionFailure())
result = pdm(["venv", "activate", key], obj=project)
assert result.exit_code == 0, result.stderr
backend = project.config["venv.backend"]

if backend == "conda":
assert result.output.startswith("conda activate")
else:
assert result.output.strip("'\"\n").endswith("activate")
assert result.output.startswith("source")


@pytest.mark.usefixtures("fake_create")
@pytest.mark.parametrize("keep_pypackages", [True, False])
def test_venv_auto_create(pdm, mocker, project, keep_pypackages):
Expand Down