Files
pyp2spec/tests/test_license_processor.py
T
Karolina SurmaandKarolina Surma dce4344006 Convert Python license classifiers to SPDX expressions
Alongside with the license-related code the tests were significantly
refactored to run offline. The changes are intertwined and so shipped in
a single commit.
The licensing changes:
- updated Trove-Fedora mappings (removed Callaway expressions, added
  SPDX)
- delegated the compliance check of Fedora licenses to a source
  maintained by the Fedora Legal team
- the logic of license processing was moved to a dedicated module:
  license_processor

The testing changes:
- moved betamax configuration to conftest.py to achieve a reliable global setting
- mocked fedora licensing data and PyPI package data to stop making
  unnecessary requests - most of the betamax cassettes were deleted
- several tests rewritten, many added
- regenerated remaining betamax cassettes

This is a best-effort solution which can't (nor aims to) cover all edge-cases.
Unless Python adopts SPDX standard, this will always be an approximation.
2023-04-25 10:17:47 +02:00

97 lines
3.9 KiB
Python

import pytest
from pyp2spec.license_processor import classifiers_to_spdx_identifiers, license_keyword_to_spdx_identifiers
from pyp2spec.license_processor import _is_compliant_with_fedora, good_for_fedora
@pytest.mark.parametrize(
("classifiers", "spdx_identifiers"),
(
(["License :: OSI Approved :: X.Net License"], ["Xnet"]), # good
(["License :: OSI Approved :: X.Net License", "License :: OSI Approved :: Artistic License"], None), # good & bad
(["License :: OSI Approved :: Artistic License"], None), # bad
(["License :: OSI Approved :: Eiffel Forum License", "License :: OSI Approved :: GNU General Public License (GPL)"], None), # bad & bad
(["License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)", "License :: OSI Approved :: ISC License (ISCL)"], ["LGPL-3.0-or-later", "ISC"]), # good & good
),
)
def test_classifiers_to_spdx_identifiers(classifiers, spdx_identifiers):
assert classifiers_to_spdx_identifiers(classifiers) == spdx_identifiers
@pytest.mark.parametrize(
("classifiers"), (
["Non-existing-classifier"],
[""]
),
)
def test_conversion_to_spdx_fails_for_invalid_inputs(classifiers):
with pytest.raises(KeyError):
classifiers_to_spdx_identifiers(classifiers)
@pytest.mark.parametrize(
("expression", "expected"),
(
("LGPL-2.1-only AND MIT AND BSD-2-Clause", ['BSD-2-Clause', 'LGPL-2.1-only', 'MIT']),
("MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)", ['BSD-3-Clause', 'LGPL-2.1-or-later', 'MIT']),
("(MIT AND (LGPL-2.1+ AND BSD-3-Clause))", ['BSD-3-Clause', 'LGPL-2.1-or-later', 'MIT']),
("GPL-3.0-or-later WITH GCC-exception-3.1", ["GPL-3.0-or-later WITH GCC-exception-3.1"]),
("GPL-3.0-or-later WITH GCC-exception-3.1 OR Apache-2.0 OR MPL-2.0", ["Apache-2.0", "GPL-3.0-or-later WITH GCC-exception-3.1", "MPL-2.0"]),
("", None),
),
)
def test_split_expressions_to_identifiers(expression, expected):
assert license_keyword_to_spdx_identifiers(expression) == expected
def test_perl_license_raises_exception():
with pytest.raises(NotImplementedError):
license_keyword_to_spdx_identifiers("MPL-2.0 OR GPL-2.0-or-later OR Artistic-1.0-Perl AND BSD-3-Clause")
@pytest.mark.parametrize(
("expression"),
(
("FOO AND bar OR baz WITH EXCEPTION"),
("an arbitrary string"),
("multiline\nstring"),
("BSD BSD AND BSD"),
("string"),
),
)
def test_invalid_expressions_raise_errors(expression):
with pytest.raises(ValueError):
license_keyword_to_spdx_identifiers(expression)
@pytest.mark.parametrize(
("identifier", "expected"),
(
("CECILL-C", True),
("GPL-3.0-or-later WITH Classpath-exception-2.0", True),
("GPL-3.0-or-later WITH GCC-exception-3.1", True),
("CC0-1.0", False), # allowed content license
("LicenseRef-LPPL", False), # allowed font license
("EUPL-1.0", False), # not allowed at all
("Bitstream-Vera", False), # existing SPDX identifiers, but missing in fake_fedora_licenses
)
)
def test_compliance_check_with_fedora(identifier, expected, fake_fedora_licenses):
assert _is_compliant_with_fedora(identifier, fake_fedora_licenses) is expected
@pytest.mark.parametrize(
("identifiers", "expected"),
(
(["AAL", "MIT", "CECILL-B"], (True, [])),
([], (False, [])),
(["LicenseRef-LPPL", "MIT"], (False, ["LicenseRef-LPPL"])), # mixed not allowed/allowed
(["LicenseRef-LPPL", "Aladdin", "LicenseRef-OpenFlow", "APL-1.0"], (False, ["LicenseRef-LPPL", "Aladdin", "LicenseRef-OpenFlow", "APL-1.0"])),
),
)
def test_is_allowed_in_fedora(identifiers, expected, fake_fedora_licenses):
"""Test the function logic, bear in mind that Fedora status may change in time"""
assert good_for_fedora(identifiers, licenses_dict=fake_fedora_licenses) == expected