diff --git a/pythondistdeps.py b/pythondistdeps.py index 8be1a4d..25ca6cb 100755 --- a/pythondistdeps.py +++ b/pythondistdeps.py @@ -73,6 +73,14 @@ class Requirement(Requirement_): class Distribution(PathDistribution): def __init__(self, path): super(Distribution, self).__init__(Path(path)) + + # Check that the initialization went well and metadata are not missing or corrupted + # name is the most important attribute, if it doesn't exist, import failed + if not self.name or not isinstance(self.name, str): + print("*** PYTHON_METADATA_FAILED_TO_PARSE_ERROR___SEE_STDERR ***") + print('Error: Python metadata at `{}` are missing or corrupted.'.format(path), file=stderr) + exit(65) # os.EX_DATAERR + self.normalized_name = normalize_name(self.name) self.legacy_normalized_name = legacy_normalize_name(self.name) self.requirements = [Requirement(r) for r in self.requires or []] diff --git a/tests/data/scripts_pythondistdeps/corrupted.dist-info/corrupted b/tests/data/scripts_pythondistdeps/corrupted.dist-info/corrupted new file mode 100644 index 0000000..b04b2ba --- /dev/null +++ b/tests/data/scripts_pythondistdeps/corrupted.dist-info/corrupted @@ -0,0 +1 @@ +Corrupted dist-info metadata diff --git a/tests/data/scripts_pythondistdeps/test-data.yaml b/tests/data/scripts_pythondistdeps/test-data.yaml index 0a81eaa..afd928e 100644 --- a/tests/data/scripts_pythondistdeps/test-data.yaml +++ b/tests/data/scripts_pythondistdeps/test-data.yaml @@ -1207,6 +1207,15 @@ python3.9dist(zope-testing) --requires --normalized-names-format pep503 --require-extras-subpackages: --provides --normalized-names-format pep503: + corrupted.dist-info: + stderr: + provides: |- + Error: Python metadata at `*/corrupted.dist-info` are missing or corrupted. + requires: |- + Error: Python metadata at `*/corrupted.dist-info` are missing or corrupted. + stdout: + provides: '*** PYTHON_METADATA_FAILED_TO_PARSE_ERROR___SEE_STDERR ***' + requires: '*** PYTHON_METADATA_FAILED_TO_PARSE_ERROR___SEE_STDERR ***' pyreq2rpm.tests-2020.04.07.024dab0-py3.9.egg-info: provides: python3.9dist(pyreq2rpm-tests) = 2020.04.07.024dab0 requires: |- diff --git a/tests/test_scripts_pythondistdeps.py b/tests/test_scripts_pythondistdeps.py index 3636f1c..79f0c6f 100644 --- a/tests/test_scripts_pythondistdeps.py +++ b/tests/test_scripts_pythondistdeps.py @@ -29,6 +29,7 @@ from pathlib import Path +from fnmatch import fnmatch import pytest import shlex import shutil @@ -224,8 +225,21 @@ def fixture_check_and_install_test_data(): def test_pythondistdeps(provides_params, requires_params, dist_egg_info_path, expected): """Runs pythondistdeps with the given parameters and dist-info/egg-info path, compares the results with the expected results""" + expect_failure = "stderr" in expected - assert expected == run_pythondistdeps(provides_params, requires_params, dist_egg_info_path, expect_failure) + tested = run_pythondistdeps(provides_params, requires_params, dist_egg_info_path, expect_failure) + + if expect_failure: + for k1, k2 in ((k1, k2) for k1 in expected.keys() for k2 in expected[k1].keys()): + if k1 == "stderr": + # Some stderr messages contain full file paths. To get around + # this, asterisk is used in the test-data and we compare with + # fnmatch that understands Unix-style wildcards. + assert fnmatch(tested[k1][k2], expected[k1][k2]) + else: + assert expected[k1][k2] == tested[k1][k2] + else: + assert expected == tested if __name__ == "__main__":