diff --git a/examples/spdx2_document_from_scratch.py b/examples/spdx2_document_from_scratch.py index e74ccf3a1..1e9360010 100644 --- a/examples/spdx2_document_from_scratch.py +++ b/examples/spdx2_document_from_scratch.py @@ -49,6 +49,7 @@ # The document currently does not describe anything. Let's create a package that we can add to it. # The Package class has quite a few properties (have a look there!), # but only name, spdx_id and download_location are mandatory in SPDX v2.3. +spdx_licensing = get_spdx_licensing() # this getter takes quite long and should be called as few times as possible package = Package( name="package name", spdx_id="SPDXRef-Package", @@ -65,9 +66,9 @@ Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"), Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"), ], - license_concluded=get_spdx_licensing().parse("GPL-2.0-only OR MIT"), - license_info_from_files=[get_spdx_licensing().parse("GPL-2.0-only"), get_spdx_licensing().parse("MIT")], - license_declared=get_spdx_licensing().parse("GPL-2.0-only AND MIT"), + license_concluded=spdx_licensing.parse("GPL-2.0-only OR MIT"), + license_info_from_files=[spdx_licensing.parse("GPL-2.0-only"), spdx_licensing.parse("MIT")], + license_declared=spdx_licensing.parse("GPL-2.0-only AND MIT"), license_comment="license comment", copyright_text="Copyright 2022 Jane Doe", description="package description", @@ -100,8 +101,8 @@ Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"), Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"), ], - license_concluded=get_spdx_licensing().parse("MIT"), - license_info_in_file=[get_spdx_licensing().parse("MIT")], + license_concluded=spdx_licensing.parse("MIT"), + license_info_in_file=[spdx_licensing.parse("MIT")], copyright_text="Copyright 2022 Jane Doe", ) file2 = File( @@ -110,7 +111,7 @@ checksums=[ Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2759"), ], - license_concluded=get_spdx_licensing().parse("GPL-2.0-only"), + license_concluded=spdx_licensing.parse("GPL-2.0-only"), ) # Assuming the package contains those two files, we create two CONTAINS relationships. diff --git a/src/spdx_tools/spdx/validation/license_expression_validator.py b/src/spdx_tools/spdx/validation/license_expression_validator.py index bce5c9eb3..3956cbff5 100644 --- a/src/spdx_tools/spdx/validation/license_expression_validator.py +++ b/src/spdx_tools/spdx/validation/license_expression_validator.py @@ -8,6 +8,8 @@ from spdx_tools.spdx.model import Document, SpdxNoAssertion, SpdxNone from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage +spdx_licensing = get_spdx_licensing() + def validate_license_expressions( license_expressions: List[Union[LicenseExpression, SpdxNoAssertion, SpdxNone]], document: Document, parent_id: str @@ -40,7 +42,7 @@ def validate_license_expression( validation_messages = [] license_ref_ids: List[str] = [license_ref.license_id for license_ref in document.extracted_licensing_info] - for non_spdx_token in get_spdx_licensing().validate(license_expression).invalid_symbols: + for non_spdx_token in spdx_licensing.validate(license_expression).invalid_symbols: if non_spdx_token not in license_ref_ids: validation_messages.append( ValidationMessage( @@ -51,14 +53,14 @@ def validate_license_expression( ) try: - get_spdx_licensing().parse(str(license_expression), validate=True, strict=True) + spdx_licensing.parse(str(license_expression), validate=True, strict=True) except ExpressionParseError as err: # This error is raised when an exception symbol is used as a license symbol and vice versa. # So far, it only catches the first such error in the provided string. validation_messages.append(ValidationMessage(f"{err}. for license_expression: {license_expression}", context)) except ExpressionError: # This error is raised for invalid symbols within the license_expression, but it provides only a string of - # these. On the other hand, get_spdx_licensing().validate() gives an actual list of invalid symbols, so this is + # these. On the other hand, spdx_licensing.validate() gives an actual list of invalid symbols, so this is # handled above. pass diff --git a/src/spdx_tools/spdx/writer/rdf/license_expression_writer.py b/src/spdx_tools/spdx/writer/rdf/license_expression_writer.py index 1057f6efd..52bd324b5 100644 --- a/src/spdx_tools/spdx/writer/rdf/license_expression_writer.py +++ b/src/spdx_tools/spdx/writer/rdf/license_expression_writer.py @@ -18,6 +18,8 @@ from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone from spdx_tools.spdx.rdfschema.namespace import LICENSE_NAMESPACE, SPDX_NAMESPACE +spdx_licensing = get_spdx_licensing() + def add_license_expression_or_none_or_no_assertion( value: Union[ @@ -75,7 +77,7 @@ def add_license_expression_to_graph( def license_or_exception_is_on_spdx_licensing_list(license_symbol: LicenseSymbol) -> bool: - symbol_info: ExpressionInfo = get_spdx_licensing().validate(license_symbol) + symbol_info: ExpressionInfo = spdx_licensing.validate(license_symbol) return not symbol_info.errors diff --git a/src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py b/src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py index ddd04ecdd..f85700401 100644 --- a/src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py +++ b/src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py @@ -28,6 +28,8 @@ ) from spdx_tools.spdx.model import ExtractedLicensingInfo, SpdxNoAssertion, SpdxNone +spdx_licensing = get_spdx_licensing() + def bump_license_expression_or_none_or_no_assertion( element: Union[LicenseExpression, SpdxNoAssertion, SpdxNone], @@ -61,7 +63,7 @@ def bump_license_expression( subject_addition=bump_license_exception(license_expression.exception_symbol, extracted_licensing_info), ) if isinstance(license_expression, LicenseSymbol): - if not get_spdx_licensing().validate(license_expression).invalid_symbols: + if not spdx_licensing.validate(license_expression).invalid_symbols: return ListedLicense(license_expression.key, license_expression.obj, "blank") else: for licensing_info in extracted_licensing_info: @@ -80,7 +82,7 @@ def bump_license_expression( def bump_license_exception( license_exception: LicenseSymbol, extracted_licensing_info: List[ExtractedLicensingInfo] ) -> LicenseAddition: - if not get_spdx_licensing().validate(license_exception).invalid_symbols: + if not spdx_licensing.validate(license_exception).invalid_symbols: return ListedLicenseException(license_exception.key, "", "") else: for licensing_info in extracted_licensing_info: diff --git a/tests/spdx/examples/test_spdx2_document_from_scratch.py b/tests/spdx/examples/test_spdx2_document_from_scratch.py index 538610bb6..b2a7d0c97 100644 --- a/tests/spdx/examples/test_spdx2_document_from_scratch.py +++ b/tests/spdx/examples/test_spdx2_document_from_scratch.py @@ -51,6 +51,7 @@ def test_spdx2_document_from_scratch(): # The document currently does not describe anything. Let's create a package that we can add to it. # The Package class has quite a few properties (have a look there!), # but only name, spdx_id and download_location are mandatory in SPDX v2.3. + spdx_licensing = get_spdx_licensing() # this getter takes quite long and should be called as few times as possible package = Package( name="package name", spdx_id="SPDXRef-Package", @@ -67,9 +68,9 @@ def test_spdx2_document_from_scratch(): Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"), Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"), ], - license_concluded=get_spdx_licensing().parse("GPL-2.0-only OR MIT"), - license_info_from_files=[get_spdx_licensing().parse("GPL-2.0-only"), get_spdx_licensing().parse("MIT")], - license_declared=get_spdx_licensing().parse("GPL-2.0-only AND MIT"), + license_concluded=spdx_licensing.parse("GPL-2.0-only OR MIT"), + license_info_from_files=[spdx_licensing.parse("GPL-2.0-only"), spdx_licensing.parse("MIT")], + license_declared=spdx_licensing.parse("GPL-2.0-only AND MIT"), license_comment="license comment", copyright_text="Copyright 2022 Jane Doe", description="package description", @@ -102,8 +103,8 @@ def test_spdx2_document_from_scratch(): Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"), Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"), ], - license_concluded=get_spdx_licensing().parse("MIT"), - license_info_in_file=[get_spdx_licensing().parse("MIT")], + license_concluded=spdx_licensing.parse("MIT"), + license_info_in_file=[spdx_licensing.parse("MIT")], copyright_text="Copyright 2022 Jane Doe", ) file2 = File( @@ -112,7 +113,7 @@ def test_spdx2_document_from_scratch(): checksums=[ Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2759"), ], - license_concluded=get_spdx_licensing().parse("GPL-2.0-only"), + license_concluded=spdx_licensing.parse("GPL-2.0-only"), ) # Assuming the package contains those two files, we create two CONTAINS relationships. diff --git a/tests/spdx/fixtures.py b/tests/spdx/fixtures.py index f0d8f14b7..8c78ee5f9 100644 --- a/tests/spdx/fixtures.py +++ b/tests/spdx/fixtures.py @@ -32,6 +32,9 @@ Version, ) +spdx_licensing = get_spdx_licensing() + + # Utility methods to create data model instances. All properties have valid defaults, so they don't need to be # specified unless relevant for the test. @@ -88,7 +91,7 @@ def file_fixture( spdx_id="SPDXRef-File", checksums=None, file_types=None, - license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"), + license_concluded=spdx_licensing.parse("MIT and GPL-2.0"), license_info_in_file=None, license_comment="licenseComment", copyright_text="copyrightText", @@ -100,7 +103,7 @@ def file_fixture( checksums = [checksum_fixture()] if checksums is None else checksums file_types = [FileType.TEXT] if file_types is None else file_types license_info_in_file = ( - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()] + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()] if license_info_in_file is None else license_info_in_file ) @@ -135,9 +138,9 @@ def package_fixture( checksums=None, homepage="https://homepage.com", source_info="sourceInfo", - license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"), + license_concluded=spdx_licensing.parse("MIT and GPL-2.0"), license_info_from_files=None, - license_declared=get_spdx_licensing().parse("MIT and GPL-2.0"), + license_declared=spdx_licensing.parse("MIT and GPL-2.0"), license_comment="packageLicenseComment", copyright_text="packageCopyrightText", summary="packageSummary", @@ -152,7 +155,7 @@ def package_fixture( ) -> Package: checksums = [checksum_fixture()] if checksums is None else checksums license_info_from_files = ( - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()] + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()] if license_info_from_files is None else license_info_from_files ) @@ -208,7 +211,7 @@ def snippet_fixture( file_spdx_id="SPDXRef-File", byte_range=(1, 2), line_range=(3, 4), - license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"), + license_concluded=spdx_licensing.parse("MIT and GPL-2.0"), license_info_in_snippet=None, license_comment="snippetLicenseComment", copyright_text="licenseCopyrightText", @@ -217,7 +220,7 @@ def snippet_fixture( attribution_texts=None, ) -> Snippet: license_info_in_snippet = ( - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNone()] + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNone()] if license_info_in_snippet is None else license_info_in_snippet ) diff --git a/tests/spdx/parser/jsonlikedict/test_license_expression_parser.py b/tests/spdx/parser/jsonlikedict/test_license_expression_parser.py index a1364d556..1c99555f4 100644 --- a/tests/spdx/parser/jsonlikedict/test_license_expression_parser.py +++ b/tests/spdx/parser/jsonlikedict/test_license_expression_parser.py @@ -10,12 +10,14 @@ from spdx_tools.spdx.parser.error import SPDXParsingError from spdx_tools.spdx.parser.jsonlikedict.license_expression_parser import LicenseExpressionParser +spdx_licensing = get_spdx_licensing() + @pytest.mark.parametrize( "license_expression_str, expected_license", [ - ("First License", get_spdx_licensing().parse("First License")), - ("Second License", get_spdx_licensing().parse("Second License")), + ("First License", spdx_licensing.parse("First License")), + ("Second License", spdx_licensing.parse("Second License")), ("NOASSERTION", SpdxNoAssertion()), ("NONE", SpdxNone()), ], diff --git a/tests/spdx/parser/rdf/test_file_parser.py b/tests/spdx/parser/rdf/test_file_parser.py index 7facfce98..b4bb7bae7 100644 --- a/tests/spdx/parser/rdf/test_file_parser.py +++ b/tests/spdx/parser/rdf/test_file_parser.py @@ -13,6 +13,8 @@ from spdx_tools.spdx.parser.rdf.file_parser import parse_file from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE +spdx_licensing = get_spdx_licensing() + def test_parse_file(): graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml")) @@ -29,10 +31,10 @@ def test_parse_file(): assert file.comment == "fileComment" assert file.copyright_text == "copyrightText" assert file.contributors == ["fileContributor"] - assert file.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0") + assert file.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0") TestCase().assertCountEqual( file.license_info_in_file, - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()], + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()], ) assert file.license_comment == "licenseComment" assert file.notice == "fileNotice" diff --git a/tests/spdx/parser/rdf/test_license_expression_parser.py b/tests/spdx/parser/rdf/test_license_expression_parser.py index 5f3ada8c7..136b8a04c 100644 --- a/tests/spdx/parser/rdf/test_license_expression_parser.py +++ b/tests/spdx/parser/rdf/test_license_expression_parser.py @@ -11,6 +11,8 @@ from spdx_tools.spdx.parser.rdf.license_expression_parser import parse_license_expression from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE +spdx_licensing = get_spdx_licensing() + def test_license_expression_parser(): graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml")) @@ -19,7 +21,7 @@ def test_license_expression_parser(): license_expression = parse_license_expression(license_expression_node, graph, "https://some.namespace#") - assert license_expression == get_spdx_licensing().parse("GPL-2.0 AND MIT") + assert license_expression == spdx_licensing.parse("GPL-2.0 AND MIT") def test_license_expression_parser_with_coupled_licenses(): @@ -30,19 +32,19 @@ def test_license_expression_parser_with_coupled_licenses(): packages_by_spdx_id = {package.spdx_id: package for package in doc.packages} files_by_spdx_id = {file.spdx_id: file for file in doc.files} - assert packages_by_spdx_id["SPDXRef-Package"].license_declared == get_spdx_licensing().parse( + assert packages_by_spdx_id["SPDXRef-Package"].license_declared == spdx_licensing.parse( "LGPL-2.0-only AND LicenseRef-3" ) - assert packages_by_spdx_id["SPDXRef-Package"].license_concluded == get_spdx_licensing().parse( + assert packages_by_spdx_id["SPDXRef-Package"].license_concluded == spdx_licensing.parse( "LGPL-2.0-only OR LicenseRef-3" ) TestCase().assertCountEqual( packages_by_spdx_id["SPDXRef-Package"].license_info_from_files, [ - get_spdx_licensing().parse("GPL-2.0"), - get_spdx_licensing().parse("LicenseRef-1"), - get_spdx_licensing().parse("LicenseRef-2"), + spdx_licensing.parse("GPL-2.0"), + spdx_licensing.parse("LicenseRef-1"), + spdx_licensing.parse("LicenseRef-2"), ], ) - assert files_by_spdx_id["SPDXRef-JenaLib"].license_concluded == get_spdx_licensing().parse("LicenseRef-1") + assert files_by_spdx_id["SPDXRef-JenaLib"].license_concluded == spdx_licensing.parse("LicenseRef-1") diff --git a/tests/spdx/parser/rdf/test_package_parser.py b/tests/spdx/parser/rdf/test_package_parser.py index 814ceceee..a60d8ce45 100644 --- a/tests/spdx/parser/rdf/test_package_parser.py +++ b/tests/spdx/parser/rdf/test_package_parser.py @@ -22,6 +22,8 @@ from spdx_tools.spdx.parser.rdf.package_parser import parse_external_package_ref, parse_package from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE +spdx_licensing = get_spdx_licensing() + def test_package_parser(): graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml")) @@ -41,11 +43,11 @@ def test_package_parser(): assert package.files_analyzed is True assert package.checksums == [Checksum(ChecksumAlgorithm.SHA1, "71c4025dd9897b364f3ebbb42c484ff43d00791c")] assert package.source_info == "sourceInfo" - assert package.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0") - assert package.license_declared == get_spdx_licensing().parse("MIT AND GPL-2.0") + assert package.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0") + assert package.license_declared == spdx_licensing.parse("MIT AND GPL-2.0") TestCase().assertCountEqual( package.license_info_from_files, - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()], + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()], ) assert package.license_comment == "packageLicenseComment" assert package.copyright_text == "packageCopyrightText" diff --git a/tests/spdx/parser/rdf/test_snippet_parser.py b/tests/spdx/parser/rdf/test_snippet_parser.py index da2267221..dbde301bc 100644 --- a/tests/spdx/parser/rdf/test_snippet_parser.py +++ b/tests/spdx/parser/rdf/test_snippet_parser.py @@ -13,6 +13,8 @@ from spdx_tools.spdx.parser.rdf.snippet_parser import parse_ranges, parse_snippet from spdx_tools.spdx.rdfschema.namespace import POINTER_NAMESPACE, SPDX_NAMESPACE +spdx_licensing = get_spdx_licensing() + def test_parse_snippet(): graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml")) @@ -26,10 +28,10 @@ def test_parse_snippet(): assert snippet.file_spdx_id == "SPDXRef-File" assert snippet.byte_range == (1, 2) assert snippet.line_range == (3, 4) - assert snippet.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0") + assert snippet.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0") TestCase().assertCountEqual( snippet.license_info_in_snippet, - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()], + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()], ) assert snippet.license_comment == "snippetLicenseComment" assert snippet.copyright_text == "licenseCopyrightText" diff --git a/tests/spdx/parser/tagvalue/test_file_parser.py b/tests/spdx/parser/tagvalue/test_file_parser.py index 859516cbf..87bec7d9e 100644 --- a/tests/spdx/parser/tagvalue/test_file_parser.py +++ b/tests/spdx/parser/tagvalue/test_file_parser.py @@ -9,6 +9,8 @@ from spdx_tools.spdx.parser.tagvalue.parser import Parser from tests.spdx.parser.tagvalue.test_creation_info_parser import DOCUMENT_STR +spdx_licensing = get_spdx_licensing() + def test_parse_file(): parser = Parser() @@ -39,8 +41,8 @@ def test_parse_file(): assert spdx_file.attribution_texts == [ "Acknowledgements that might be required to be communicated in some contexts." ] - assert spdx_file.license_info_in_file == [get_spdx_licensing().parse("Apache-2.0"), SpdxNoAssertion()] - assert spdx_file.license_concluded == get_spdx_licensing().parse("Apache-2.0") + assert spdx_file.license_info_in_file == [spdx_licensing.parse("Apache-2.0"), SpdxNoAssertion()] + assert spdx_file.license_concluded == spdx_licensing.parse("Apache-2.0") def test_parse_invalid_file(): diff --git a/tests/spdx/parser/tagvalue/test_package_parser.py b/tests/spdx/parser/tagvalue/test_package_parser.py index dbbeef415..9d9e79902 100644 --- a/tests/spdx/parser/tagvalue/test_package_parser.py +++ b/tests/spdx/parser/tagvalue/test_package_parser.py @@ -13,6 +13,8 @@ from spdx_tools.spdx.parser.tagvalue.parser import Parser from tests.spdx.parser.tagvalue.test_creation_info_parser import DOCUMENT_STR +spdx_licensing = get_spdx_licensing() + def test_parse_package(): parser = Parser() @@ -57,9 +59,9 @@ def test_parse_package(): assert len(package.license_info_from_files) == 3 TestCase().assertCountEqual( package.license_info_from_files, - [get_spdx_licensing().parse("Apache-1.0"), get_spdx_licensing().parse("Apache-2.0"), SpdxNone()], + [spdx_licensing.parse("Apache-1.0"), spdx_licensing.parse("Apache-2.0"), SpdxNone()], ) - assert package.license_concluded == get_spdx_licensing().parse("LicenseRef-2.0 AND Apache-2.0") + assert package.license_concluded == spdx_licensing.parse("LicenseRef-2.0 AND Apache-2.0") assert package.files_analyzed is True assert package.comment == "Comment on the package." assert len(package.external_references) == 2 diff --git a/tests/spdx/parser/tagvalue/test_snippet_parser.py b/tests/spdx/parser/tagvalue/test_snippet_parser.py index 8bd82595c..4d0e27b8c 100644 --- a/tests/spdx/parser/tagvalue/test_snippet_parser.py +++ b/tests/spdx/parser/tagvalue/test_snippet_parser.py @@ -11,6 +11,8 @@ from spdx_tools.spdx.parser.tagvalue.parser import Parser from tests.spdx.parser.tagvalue.test_creation_info_parser import DOCUMENT_STR +spdx_licensing = get_spdx_licensing() + def test_parse_snippet(): parser = Parser() @@ -41,7 +43,7 @@ def test_parse_snippet(): assert snippet.copyright_text == " Copyright 2008-2010 John Smith " assert snippet.license_comment == "Some lic comment." assert snippet.file_spdx_id == "SPDXRef-DoapSource" - assert snippet.license_concluded == get_spdx_licensing().parse("Apache-2.0") + assert snippet.license_concluded == spdx_licensing.parse("Apache-2.0") assert snippet.license_info_in_snippet == [SpdxNoAssertion()] assert snippet.byte_range[0] == 310 assert snippet.byte_range[1] == 420 diff --git a/tests/spdx/validation/test_license_expression_validator.py b/tests/spdx/validation/test_license_expression_validator.py index 03c0eddad..be69e3e65 100644 --- a/tests/spdx/validation/test_license_expression_validator.py +++ b/tests/spdx/validation/test_license_expression_validator.py @@ -17,6 +17,7 @@ from tests.spdx.fixtures import document_fixture, extracted_licensing_info_fixture FIXTURE_LICENSE_ID = extracted_licensing_info_fixture().license_id +spdx_licensing = get_spdx_licensing() @pytest.mark.parametrize( @@ -29,7 +30,7 @@ ) def test_valid_license_expression(expression_string): document: Document = document_fixture() - license_expression: LicenseExpression = get_spdx_licensing().parse(expression_string) + license_expression: LicenseExpression = spdx_licensing.parse(expression_string) validation_messages: List[ValidationMessage] = validate_license_expression( license_expression, document, parent_id="SPDXRef-File" ) @@ -51,8 +52,8 @@ def test_none_and_no_assertion(expression): [ [SpdxNone()], [SpdxNoAssertion()], - [get_spdx_licensing().parse("MIT and GPL-3.0-only"), get_spdx_licensing().parse(FIXTURE_LICENSE_ID)], - [SpdxNone(), get_spdx_licensing().parse("MIT"), SpdxNoAssertion()], + [spdx_licensing.parse("MIT and GPL-3.0-only"), spdx_licensing.parse(FIXTURE_LICENSE_ID)], + [SpdxNone(), spdx_licensing.parse("MIT"), SpdxNoAssertion()], ], ) def test_valid_license_expressions(expression_list): @@ -72,7 +73,7 @@ def test_valid_license_expressions(expression_list): ) def test_invalid_license_expression_with_unknown_symbols(expression_string, unknown_symbols): document: Document = document_fixture() - license_expression: LicenseExpression = get_spdx_licensing().parse(expression_string) + license_expression: LicenseExpression = spdx_licensing.parse(expression_string) parent_id = "SPDXRef-File" context = ValidationContext( parent_id=parent_id, element_type=SpdxElementType.LICENSE_EXPRESSION, full_element=license_expression @@ -125,7 +126,7 @@ def test_invalid_license_expression_with_unknown_symbols(expression_string, unkn ) def test_invalid_license_expression_with_invalid_exceptions(expression_string, expected_message): document: Document = document_fixture() - license_expression: LicenseExpression = get_spdx_licensing().parse(expression_string) + license_expression: LicenseExpression = spdx_licensing.parse(expression_string) parent_id = "SPDXRef-File" context = ValidationContext( parent_id=parent_id, element_type=SpdxElementType.LICENSE_EXPRESSION, full_element=license_expression diff --git a/tests/spdx/writer/rdf/test_license_expression_writer.py b/tests/spdx/writer/rdf/test_license_expression_writer.py index d77d08ffb..a69c1da77 100644 --- a/tests/spdx/writer/rdf/test_license_expression_writer.py +++ b/tests/spdx/writer/rdf/test_license_expression_writer.py @@ -8,10 +8,12 @@ from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE from spdx_tools.spdx.writer.rdf.license_expression_writer import add_license_expression_to_graph +spdx_licensing = get_spdx_licensing() + def test_add_conjunctive_license_set_to_graph(): graph = Graph() - license_expression = get_spdx_licensing().parse("MIT AND GPL-2.0") + license_expression = spdx_licensing.parse("MIT AND GPL-2.0") add_license_expression_to_graph( license_expression, graph, URIRef("parentNode"), SPDX_NAMESPACE.licenseConcluded, "https://namespace" @@ -25,7 +27,7 @@ def test_add_conjunctive_license_set_to_graph(): def test_add_disjunctive_license_set_to_graph(): graph = Graph() - license_expression = get_spdx_licensing().parse("MIT OR GPL-2.0") + license_expression = spdx_licensing.parse("MIT OR GPL-2.0") add_license_expression_to_graph( license_expression, graph, URIRef("parentNode"), SPDX_NAMESPACE.licenseConcluded, "https://namespace" @@ -49,7 +51,7 @@ def test_add_disjunctive_license_set_to_graph(): ) def test_license_exception_to_graph(license_with_exception, expected_triple): graph = Graph() - license_expression = get_spdx_licensing().parse(license_with_exception) + license_expression = spdx_licensing.parse(license_with_exception) add_license_expression_to_graph( license_expression, graph, URIRef("parentNode"), SPDX_NAMESPACE.licenseConcluded, "https://namespace" diff --git a/tests/spdx3/bump/test_license_expression_bump.py b/tests/spdx3/bump/test_license_expression_bump.py index 6f3b8aa20..1e1afb239 100644 --- a/tests/spdx3/bump/test_license_expression_bump.py +++ b/tests/spdx3/bump/test_license_expression_bump.py @@ -22,13 +22,15 @@ from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone from tests.spdx.fixtures import extracted_licensing_info_fixture +spdx_licensing = get_spdx_licensing() + @pytest.mark.parametrize( "element, expected_class", [ (SpdxNoAssertion(), NoAssertionLicense), (SpdxNone(), NoneLicense), - (get_spdx_licensing().parse("MIT"), ListedLicense), + (spdx_licensing.parse("MIT"), ListedLicense), ], ) def test_license_expression_or_none_or_no_assertion(element, expected_class): @@ -40,22 +42,22 @@ def test_license_expression_or_none_or_no_assertion(element, expected_class): @pytest.mark.parametrize( "license_expression, extracted_licensing_info, expected_element", [ - (get_spdx_licensing().parse("MIT"), [], ListedLicense("MIT", "MIT", "blank")), - (get_spdx_licensing().parse("LGPL-2.0"), [], ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank")), + (spdx_licensing.parse("MIT"), [], ListedLicense("MIT", "MIT", "blank")), + (spdx_licensing.parse("LGPL-2.0"), [], ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank")), ( - get_spdx_licensing().parse("LicenseRef-1"), + spdx_licensing.parse("LicenseRef-1"), [extracted_licensing_info_fixture()], CustomLicense("LicenseRef-1", "licenseName", "extractedText"), ), ( - get_spdx_licensing().parse("MIT AND LGPL-2.0"), + spdx_licensing.parse("MIT AND LGPL-2.0"), [], ConjunctiveLicenseSet( [ListedLicense("MIT", "MIT", "blank"), ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank")] ), ), ( - get_spdx_licensing().parse("LicenseRef-1 OR LGPL-2.0"), + spdx_licensing.parse("LicenseRef-1 OR LGPL-2.0"), [extracted_licensing_info_fixture()], DisjunctiveLicenseSet( [ @@ -65,7 +67,7 @@ def test_license_expression_or_none_or_no_assertion(element, expected_class): ), ), ( - get_spdx_licensing().parse("LGPL-2.0 WITH 389-exception"), + spdx_licensing.parse("LGPL-2.0 WITH 389-exception"), [], WithAdditionOperator( ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank"), @@ -73,7 +75,7 @@ def test_license_expression_or_none_or_no_assertion(element, expected_class): ), ), ( - get_spdx_licensing().parse("LicenseRef-1 WITH custom-exception"), + spdx_licensing.parse("LicenseRef-1 WITH custom-exception"), [ extracted_licensing_info_fixture(), extracted_licensing_info_fixture("custom-exception", "This is a custom exception", "exceptionName"), @@ -84,7 +86,7 @@ def test_license_expression_or_none_or_no_assertion(element, expected_class): ), ), ( - get_spdx_licensing().parse("MIT AND LicenseRef-1 WITH custom-exception"), + spdx_licensing.parse("MIT AND LicenseRef-1 WITH custom-exception"), [ extracted_licensing_info_fixture(), extracted_licensing_info_fixture("custom-exception", "This is a custom exception", "exceptionName"),