Skip to content

Commit

Permalink
Fix internal tests invoked from zsh shell
Browse files Browse the repository at this point in the history
Before this fix some of the internal hatch tests were failing when
invoked from zsh shell. This change updates the test fixture to use
the same shell detection mechanism as in the actual hatch code.
  • Loading branch information
haxoza committed May 22, 2024
1 parent ae8c3fb commit cd0950c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
9 changes: 2 additions & 7 deletions src/hatch/cli/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from hatch.utils.fs import Path
from hatch.utils.platform import Platform
from hatch.utils.runner import ExecutionContext
from hatch.utils.shells import detect_shell

if TYPE_CHECKING:
from collections.abc import Generator
Expand Down Expand Up @@ -295,13 +296,7 @@ def get_python_manager(self, directory: str | None = None):

@cached_property
def shell_data(self) -> tuple[str, str]:
import shellingham

try:
return shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
path = self.platform.default_shell
return Path(path).stem, path
return detect_shell(self.platform)

@cached_property
def env_metadata(self) -> EnvironmentMetadata:
Expand Down
7 changes: 3 additions & 4 deletions src/hatch/cli/python/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import click

from hatch.utils.shells import get_shell_names

if TYPE_CHECKING:
from hatch.cli.application import Application

Expand Down Expand Up @@ -41,10 +43,7 @@ def install(app: Application, *, names: tuple[str, ...], private: bool, update:
from hatch.python.distributions import ORDERED_DISTRIBUTIONS
from hatch.python.resolve import get_distribution

shells = []
if not private and not app.platform.windows:
shell_name, _ = app.shell_data
shells.append(shell_name)
shells = get_shell_names(app.shell_data, app.platform, private=private)

manager = app.get_python_manager(directory)
installed = manager.get_installed()
Expand Down
25 changes: 24 additions & 1 deletion src/hatch/utils/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,35 @@
import sys
from typing import TYPE_CHECKING

from hatch.utils.fs import Path

if TYPE_CHECKING:
from collections.abc import Callable, Iterable
from types import FrameType
from typing import TypeAlias

from hatch.env.plugin.interface import EnvironmentInterface
from hatch.utils.fs import Path
from hatch.utils.platform import Platform


Shell: TypeAlias = tuple[str, str]


def detect_shell(platform: Platform) -> Shell:
import shellingham

try:
return shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
path = platform.default_shell
return Path(path).stem, path


def get_shell_names(shell: Shell, platform: Platform, *, private: bool = False) -> list[str]:
shells = []
if not private and not platform.windows:
shells.append(shell[0])
return shells


class ShellManager:
Expand Down
5 changes: 4 additions & 1 deletion tests/cli/python/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import pytest

from hatch.utils.shells import detect_shell, get_shell_names


@pytest.fixture(autouse=True)
def default_shells(platform):
return [] if platform.windows else ['sh']
shell = detect_shell(platform)
return get_shell_names(shell, platform)


@pytest.fixture(autouse=True)
Expand Down

0 comments on commit cd0950c

Please sign in to comment.