diff --git a/src/hatch/cli/application.py b/src/hatch/cli/application.py index 9c527fece..c559d0b71 100644 --- a/src/hatch/cli/application.py +++ b/src/hatch/cli/application.py @@ -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 @@ -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: diff --git a/src/hatch/cli/python/install.py b/src/hatch/cli/python/install.py index 04cfe5ace..f7fb5279e 100644 --- a/src/hatch/cli/python/install.py +++ b/src/hatch/cli/python/install.py @@ -4,6 +4,8 @@ import click +from hatch.utils.shells import get_shell_names + if TYPE_CHECKING: from hatch.cli.application import Application @@ -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() diff --git a/src/hatch/utils/shells.py b/src/hatch/utils/shells.py index 46ffa3baf..6b72ab6e3 100644 --- a/src/hatch/utils/shells.py +++ b/src/hatch/utils/shells.py @@ -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: diff --git a/tests/cli/python/conftest.py b/tests/cli/python/conftest.py index c5c6aea7c..a8df07ea9 100644 --- a/tests/cli/python/conftest.py +++ b/tests/cli/python/conftest.py @@ -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)