Skip to content
Draft
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
50 changes: 46 additions & 4 deletions test_avail_wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ def test_get_requirements_set_requirements_file(tmp_path):
assert avail_wheels.get_requirements_set(args) == {
"numpy": Requirement("numpy"),
"dgl_cpu": Requirement("dgl_cpu"),
"ab.py": Requirement("ab.py==1.9"),
"ab_py": Requirement("ab_py==1.9"),
"scipy": Requirement("scipy"),
"dummy": Requirement("dummy~=4.0.0"),
}
Expand All @@ -1008,21 +1008,63 @@ def test_get_requirements_set_requirements_file_and_names(tmp_path):
"torch": Requirement("torch"),
"dgl_cpu": Requirement("dgl_cpu==1.0"),
"numpy": Requirement("numpy"),
"ab.py": Requirement("ab.py==1.9"),
"ab_py": Requirement("ab_py==1.9"),
}


def test_get_requirements_set_from_names():
"""
Test that requirements set comes from command line.
Names should also be normalized : dgl-cpu -> dgl_cpu.
Names should also be normalized : dgl-cpu -> dgl_cpu, ab.py -> ab_py (PEP 503).
Duplicates should not exists.
"""
args = avail_wheels.create_argparser().parse_args(["torch", "dgl-cpu", "ab.py==1.9", "dummy==4+localversion"])

assert avail_wheels.get_requirements_set(args) == {
"torch": Requirement("torch"),
"dgl_cpu": Requirement("dgl_cpu"),
"ab.py": Requirement("ab.py==1.9"),
"ab_py": Requirement("ab_py==1.9"),
"dummy": Requirement("dummy==4")
}


def test_package_name_normalization_dots_and_underscores():
"""Test that package names with dots and underscores are normalized correctly."""
import wild_requirements

# Test that dots are replaced with underscores in package names (PEP 503)
req1 = wild_requirements.Requirement('jaraco_functools')
req2 = wild_requirements.Requirement('jaraco.functools')
req3 = wild_requirements.Requirement('JARACO.FUNCTOOLS')
req4 = wild_requirements.Requirement('jaraco-functools')

# All should be normalized to the same name
assert req1.name == 'jaraco_functools'
assert req2.name == 'jaraco_functools'
assert req3.name == 'jaraco_functools'
assert req4.name == 'jaraco_functools'

# Test with wheel matching
wheel = avail_wheels.Wheel.parse_wheel_filename('jaraco_functools-3.5.0-py3-none-any.whl')

reqs1 = defaultdict(wild_requirements.Requirement)
reqs1[req1.name] = req1

reqs2 = defaultdict(wild_requirements.Requirement)
reqs2[req2.name] = req2

# Both should match the wheel
assert avail_wheels.match_version(wheel, reqs1)
assert avail_wheels.match_version(wheel, reqs2)

# Test that regex generation works for both forms
rexes1 = avail_wheels.get_rexes([req1.name])
rexes2 = avail_wheels.get_rexes([req2.name])

# Both should generate the same regex
assert rexes1[0].pattern == rexes2[0].pattern

# Both should match the wheel filename
filename = 'jaraco_functools-3.5.0-py3-none-any.whl'
assert avail_wheels.match_file(filename, rexes1)
assert avail_wheels.match_file(filename, rexes2)
4 changes: 2 additions & 2 deletions wild_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def __init__(self, requirement_string: str) -> None:
f'Parse error at "{ requirement_string[e.loc : e.loc + 8]!r}": {e.msg}'
)

# Cannonicalize name
self.name = req.name.replace("-", "_").lower() # type: str
# Cannonicalize name (PEP 503: replace dots and hyphens with underscores)
self.name = req.name.replace("-", "_").replace(".", "_").lower() # type: str
if req.url:
parsed_url = urllib.parse.urlparse(req.url)
if parsed_url.scheme == "file":
Expand Down