Skip to content

Commit

Permalink
Add test for _get_pkgs_dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Oct 16, 2024
1 parent fe93cd9 commit 2ada1d2
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions tests/test_conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

from glob import glob
from pathlib import Path
from typing import Any, ContextManager, Dict, List, Literal, Set, Tuple, Union
from typing import Any, ContextManager, Dict, List, Literal, Optional, Set, Tuple, Union
from unittest import mock
from unittest.mock import MagicMock
from urllib.parse import urldefrag, urlsplit

Expand Down Expand Up @@ -47,7 +48,11 @@
render_lockfile_for_platform,
run_lock,
)
from conda_lock.conda_solver import extract_json_object, fake_conda_environment
from conda_lock.conda_solver import (
_get_pkgs_dirs,
extract_json_object,
fake_conda_environment,
)
from conda_lock.errors import (
ChannelAggregationError,
MissingEnvVarError,
Expand Down Expand Up @@ -2815,6 +2820,43 @@ def test_extract_json_object():
assert extract_json_object('{"key1": true }') == '{"key1": true }'


@pytest.mark.parametrize(
"info_file,expected",
[
("conda-info.json", [Path("/home/runner/conda_pkgs_dir")]),
("mamba-info.json", [Path("/home/runner/conda_pkgs_dir")]),
("micromamba-1.4.5-info.json", None),
(
"micromamba-1.4.6-info.json",
[Path("/home/user/micromamba/pkgs"), Path("/home/user/.mamba/pkgs")],
),
(
"micromamba-config-list-pkgs_dirs.json",
[Path("/home/user/micromamba/pkgs"), Path("/home/user/.mamba/pkgs")],
),
],
)
def test_get_pkgs_dirs(info_file: str, expected: Optional[List[Path]]):
info_path = TESTS_DIR / "test-get-pkgs-dirs" / info_file
command_output = info_path.read_bytes()
conda = info_path.stem.split("-")[0]
method: Literal["config", "info"]
if "config" in info_file:
method = "config"
elif "info" in info_file:
method = "info"
else:
raise ValueError(f"Unknown method for {info_file}")

with mock.patch("subprocess.check_output", return_value=command_output):
# If expected is None, we expect a ValueError to be raised
with pytest.raises(
ValueError
) if expected is None else contextlib.nullcontext():
result = _get_pkgs_dirs(conda=conda, platform="linux-64", method=method)
assert result == expected


def test_cli_version(capsys: "pytest.CaptureFixture[str]"):
"""It should correctly report its version."""

Expand Down

0 comments on commit 2ada1d2

Please sign in to comment.