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

Get micromamba also in case of alias #196

Merged
merged 6 commits into from
Sep 18, 2024
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
30 changes: 24 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import shutil
import subprocess
import textwrap
from contextlib import contextmanager
from pathlib import Path
from typing import Generator
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -559,17 +561,33 @@ def test_find_conda_windows() -> None:
assert path in excinfo.value.args[0]


@contextmanager
def set_env_var(key: str, value: str) -> Generator[None, None, None]:
original_value = os.environ.get(key)
os.environ[key] = value
try:
yield
finally:
if original_value is None:
del os.environ[key]
else:
os.environ[key] = original_value


@pytest.mark.skipif(
os.name == "nt",
reason="On Windows it will search for Conda because of `_maybe_exe`.",
)
def test_maybe_conda_run() -> None:
result = _maybe_conda_run("conda", "my_env", None)
assert result == ["conda", "run", "--name", "my_env"]
with set_env_var("CONDA_EXE", "conda"):
result = _maybe_conda_run("conda", "my_env", None)
assert result == ["conda", "run", "--name", "my_env"]

p = Path("/path/to/env")
result = _maybe_conda_run("conda", None, p)
assert result == ["conda", "run", "--prefix", str(p)]
with set_env_var("CONDA_EXE", "conda"):
result = _maybe_conda_run("conda", None, p)
assert result == ["conda", "run", "--prefix", str(p)]

result = _maybe_conda_run("mamba", "my_env", None)
assert result == ["mamba", "run", "--name", "my_env"]
with set_env_var("MAMBA_EXE", "mamba"):
result = _maybe_conda_run("mamba", "my_env", None)
assert result == ["mamba", "run", "--name", "my_env"]
24 changes: 20 additions & 4 deletions unidep/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,16 +604,30 @@ def _ensure_files(files: list[Path]) -> None:
sys.exit(1)


def _get_conda_executable(which: CondaExecutable) -> str | None:
w = shutil.which(which)
if w is not None:
return which # Found in PATH so return the name
# e.g., micromamba might be a bash function, check env var in that case
env_var = "CONDA_EXE" if which == "conda" else "MAMBA_EXE"
exe = os.environ.get(env_var, None)
if exe is None:
return None
if Path(exe).name != which:
return None
return exe


def _identify_conda_executable() -> CondaExecutable: # pragma: no cover
"""Identify the conda executable to use.

This function checks for micromamba, mamba, and conda in that order.
"""
if shutil.which("micromamba"):
if _get_conda_executable("micromamba") is not None:
return "micromamba"
if shutil.which("mamba"):
if _get_conda_executable("mamba") is not None:
return "mamba"
if shutil.which("conda"):
if _get_conda_executable("conda") is not None:
return "conda"
msg = "Could not identify conda executable."
raise RuntimeError(msg)
Expand Down Expand Up @@ -652,7 +666,9 @@ def _maybe_exe(conda_executable: CondaExecutable) -> str:
f" because `{conda_executable}` was not found in PATH.",
)
return _find_windows_path(conda_executable)
return conda_executable
executable = _get_conda_executable(conda_executable)
assert executable is not None
return executable


def _capitalize_dir(path: str, *, capitalize: bool = True, index: int = -1) -> str:
Expand Down