Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Aug 22, 2023
2 parents 87046f6 + 131953a commit 777940f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions news/2182.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Makes `comarable_version("1.2.3+local1") == Version("1.2.3")`.
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
14 changes: 7 additions & 7 deletions src/pdm/models/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
class JSONFileCache(Generic[KT, VT]):
"""A file cache that stores key-value pairs in a json file."""

def __init__(self, cache_file: Path) -> None:
self.cache_file = cache_file
def __init__(self, cache_file: Path | str) -> None:
self.cache_file = Path(cache_file)
self._cache: dict[str, VT] = {}
self._read_cache()

Expand Down Expand Up @@ -118,8 +118,8 @@ class HashCache:
FAVORITE_HASH = "sha256"
STRONG_HASHES = ("sha256", "sha384", "sha512")

def __init__(self, directory: Path) -> None:
self.directory = directory
def __init__(self, directory: Path | str) -> None:
self.directory = Path(directory)

def _read_from_link(self, link: Link, session: Session) -> Iterable[bytes]:
if link.is_file:
Expand Down Expand Up @@ -192,8 +192,8 @@ class WheelCache:
one sdist, the one with most preferred tag will be returned.
"""

def __init__(self, directory: Path) -> None:
self.directory = directory
def __init__(self, directory: Path | str) -> None:
self.directory = Path(directory)
self.ephemeral_directory = Path(create_tracked_tempdir(prefix="pdm-wheel-cache-"))

def _get_candidates(self, path: Path) -> Iterable[Path]:
Expand Down Expand Up @@ -321,5 +321,5 @@ def delete(self, key: str) -> None:


@lru_cache(maxsize=128)
def get_wheel_cache(directory: Path) -> WheelCache:
def get_wheel_cache(directory: Path | str) -> WheelCache:
return WheelCache(directory)
13 changes: 12 additions & 1 deletion src/pdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pathlib import Path
from typing import TYPE_CHECKING

from packaging.version import Version
from packaging.version import Version, _cmpkey

from pdm.compat import importlib_metadata

Expand Down Expand Up @@ -344,6 +344,17 @@ def comparable_version(version: str) -> Version:
if parsed.local is not None:
# strip the local part
parsed._version = parsed._version._replace(local=None)

# To make comparable_version("1.2.3+local1") == Version("1.2.3")
parsed._key = _cmpkey(
parsed._version.epoch,
parsed._version.release,
parsed._version.pre,
parsed._version.post,
parsed._version.dev,
parsed._version.local,
)

return parsed


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
6 changes: 6 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
import tomlkit
from packaging.version import Version

from pdm import utils
from pdm.cli import utils as cli_utils
Expand Down Expand Up @@ -145,3 +146,8 @@ def test_deprecation_warning():

with pytest.raises(FutureWarning):
utils.deprecation_warning("Test warning", raise_since="0.0")


def test_comparable_version():
assert utils.comparable_version("1.2.3") == Version("1.2.3")
assert utils.comparable_version("1.2.3a1+local1") == Version("1.2.3a1")

0 comments on commit 777940f

Please sign in to comment.