Update warning, add tests for project requirements check

* Make warning more general for differences between PEP 508 and pip
* Add tests for _check_requirements
This commit is contained in:
Adriane Boyd 2022-11-09 08:37:50 +01:00
parent 20bbbe3e44
commit 5183a3b071
2 changed files with 36 additions and 3 deletions

View File

@ -344,9 +344,8 @@ def _check_requirements(requirements: List[str]) -> Tuple[bool, bool]:
conflicting_pkgs_msgs.append(vc.report())
except Exception:
msg.warn(f"Unable to check requirement: {req} "
"Check that the requirement is formatted according to PEP "
"440, in particular that URLs are formatted as "
"'package_name @ URL'")
"Checks are currently limited to requirement specifiers "
"(PEP 508)")
if len(failed_pkgs_msgs) or len(conflicting_pkgs_msgs):
msg.warn(

View File

@ -1,5 +1,6 @@
import os
import math
import pkg_resources
from random import sample
from typing import Counter
@ -25,6 +26,7 @@ from spacy.cli.download import get_compatibility, get_version
from spacy.cli.init_config import RECOMMENDATIONS, init_config, fill_config
from spacy.cli.package import get_third_party_dependencies
from spacy.cli.package import _is_permitted_package_name
from spacy.cli.project.run import _check_requirements
from spacy.cli.validate import get_model_pkgs
from spacy.lang.en import English
from spacy.lang.nl import Dutch
@ -855,3 +857,35 @@ def test_span_length_freq_dist_output_must_be_correct():
span_freqs = _get_spans_length_freq_dist(sample_span_lengths, threshold)
assert sum(span_freqs.values()) >= threshold
assert list(span_freqs.keys()) == [3, 1, 4, 5, 2]
def test_project_check_requirements():
reqs = """
spacy
# comment
thinc"""
output = _check_requirements([req.strip() for req in reqs.split("\n")])
assert output == (False, False)
reqs = """# comment
--some-flag
spacy"""
output = _check_requirements([req.strip() for req in reqs.split("\n")])
assert output == (False, False)
reqs = """# comment
--some-flag
spacy; python_version >= '3.6'"""
output = _check_requirements([req.strip() for req in reqs.split("\n")])
assert output == (False, False)
# excessive guard against unlikely package name
try:
pkg_resources.require("spacyunknowndoesnotexist12345")
except pkg_resources.DistributionNotFound:
reqs = """# comment
spacyunknowndoesnotexist12345"""
output = _check_requirements([req.strip() for req in reqs.split("\n")])
assert output == (True, False)