Skip to content

Commit

Permalink
Tests for multi-package install dependency patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-cbarber committed Jan 7, 2024
1 parent 5621d8b commit 42a0cfe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/whl2conda/api/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def __compile_requires_dist_re() -> re.Pattern:
# NOTE: these are currently fairly forgiving and will accept bad syntax
name_re = r"(?P<name>[a-zA-Z0-9_.-]+)"
extra_re = r"(?:\[(?P<extra>.+?)\])?"
version_re = r"(?:\(?(?P<version>.*?)\)?)?"
version_re = r"(?:\(?(?P<version>[^;]*?)\)?)?"
marker_re = r"(?:;\s*(?P<marker>.*?)\s*)?"
space = r"\s*"
return re.compile(
name_re + space + extra_re + space + version_re + space + marker_re
space + name_re + space + extra_re + space + version_re + space + marker_re
)


Expand Down
27 changes: 10 additions & 17 deletions src/whl2conda/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import argparse
import json
import re
import shutil
import subprocess
import tempfile
Expand All @@ -30,7 +31,6 @@
from conda_package_handling.api import extract as extract_conda_pkg

from .common import dedent, existing_path, add_markdown_help, get_conda_bld_path
from ..api.converter import pip_version_re, requires_dist_re

__all__ = ["install_main"]

Expand Down Expand Up @@ -297,6 +297,9 @@ def conda_env_install(parsed: InstallArgs, dependencies: list[str]):
subprocess.check_call(set_solver_cmd)


conda_depend_re = re.compile(r"\s*(?P<name>[\w\d.-]+)\s*(?P<version>.*)")


def _prune_dependencies(
dependencies: list[str], file_specs: list[tuple[str, str]]
) -> list[str]:
Expand All @@ -308,7 +311,7 @@ def _prune_dependencies(
- Removes references to packages in file_specs
Arguments:
dependencies: input list of dependency strings (package and optional version match specifier)
dependencies: input list of conda dependency strings (package and optional version match specifier)
file_specs: list of package name/version tuple for packages being installed from file
Returns:
Expand All @@ -319,27 +322,17 @@ def _prune_dependencies(
deps: set[str] = set()

for dep in dependencies:
if m := requires_dist_re.match(dep):
if m := conda_depend_re.fullmatch(dep): # pragma: no branch
name = m.group("name")
extra = m.group("extra")
version = m.group("version")
marker = m.group("marker")
if vm := pip_version_re.match(dep):
# normalize version
operator = vm.group("operator")
version_number = vm.group("version")
if version.startswith("v"):
version = version[1:]
version = f"{operator}{version_number}"
if version:
version = version.replace(" ", "") # remove spaces from version spec
if name in exclude_packages:
# TODO check version and warn or error if not match dependency
continue
dep = name
if extra:
dep += f"[{extra}]"
dep += f" {version}"
if marker:
dep += f" ; {marker}"
if version:
dep += f" {version}"
deps.add(dep)

return sorted(deps)
17 changes: 17 additions & 0 deletions test/cli/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import pytest

from whl2conda.cli import main
from whl2conda.cli.install import _prune_dependencies
from ..test_conda import conda_config, conda_output, conda_json

# pylint: disable=unused-import
Expand Down Expand Up @@ -281,3 +282,19 @@ def fake_call(cmd: Sequence[str]) -> Any:

out, err = capsys.readouterr()
assert "Running" in out


def test_prune_dependencies() -> None:
"""Unit test for internal _prune_dependencies function"""
assert _prune_dependencies([], []) == []

assert _prune_dependencies([" foo ", "bar >= 1.2.3", "baz 1.5.*"], []) == [
"bar >=1.2.3",
"baz 1.5.*",
"foo",
]

assert _prune_dependencies(
[" foo ", "bar >= 1.2.3", "baz 1.5.*"],
[("bar", "1.2.3"), ("blah", "3.4.5")],
) == ["baz 1.5.*", "foo"]

0 comments on commit 42a0cfe

Please sign in to comment.