diff --git a/src/spdx_tools/spdx/model/relationship.py b/src/spdx_tools/spdx/model/relationship.py index 02b1326a9..d803990dc 100644 --- a/src/spdx_tools/spdx/model/relationship.py +++ b/src/spdx_tools/spdx/model/relationship.py @@ -73,3 +73,6 @@ def __init__( comment: Optional[str] = None, ): check_types_and_set_values(self, locals()) + + def __hash__(self): + return hash((self.spdx_element_id, self.related_spdx_element_id, self.relationship_type)) diff --git a/src/spdx_tools/spdx/parser/jsonlikedict/relationship_parser.py b/src/spdx_tools/spdx/parser/jsonlikedict/relationship_parser.py index 17374bef5..6393470e3 100644 --- a/src/spdx_tools/spdx/parser/jsonlikedict/relationship_parser.py +++ b/src/spdx_tools/spdx/parser/jsonlikedict/relationship_parser.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: 2022 spdx contributors # # SPDX-License-Identifier: Apache-2.0 -from beartype.typing import Dict, List, Optional +from beartype.typing import Dict, List, Optional, Set from spdx_tools.common.typing.constructor_type_errors import ConstructorTypeErrors from spdx_tools.spdx.model import Relationship, RelationshipType @@ -35,9 +35,9 @@ def parse_all_relationships(self, input_doc_dict: Dict) -> List[Relationship]: document_describes: List[str] = delete_duplicates_from_list(input_doc_dict.get("documentDescribes", [])) doc_spdx_id: Optional[str] = input_doc_dict.get("SPDXID") - existing_relationships_without_comments: List[Relationship] = self.get_all_relationships_without_comments( + existing_relationships_without_comments: Set[Relationship] = set(self.get_all_relationships_without_comments( relationships - ) + )) relationships.extend( parse_field_or_log_error( self.logger, @@ -52,9 +52,6 @@ def parse_all_relationships(self, input_doc_dict: Dict) -> List[Relationship]: ) package_dicts: List[Dict] = input_doc_dict.get("packages", []) - existing_relationships_without_comments: List[Relationship] = self.get_all_relationships_without_comments( - relationships - ) relationships.extend( parse_field_or_log_error( @@ -110,7 +107,7 @@ def parse_relationship_type(relationship_type_str: str) -> RelationshipType: return relationship_type def parse_document_describes( - self, doc_spdx_id: str, described_spdx_ids: List[str], existing_relationships: List[Relationship] + self, doc_spdx_id: str, described_spdx_ids: List[str], existing_relationships: Set[Relationship] ) -> List[Relationship]: logger = Logger() describes_relationships = [] @@ -131,10 +128,11 @@ def parse_document_describes( return describes_relationships def parse_has_files( - self, package_dicts: List[Dict], existing_relationships: List[Relationship] + self, package_dicts: List[Dict], existing_relationships: Set[Relationship] ) -> List[Relationship]: # assume existing relationships are stripped of comments logger = Logger() + contains_relationships = [] for package in package_dicts: package_spdx_id: Optional[str] = package.get("SPDXID") @@ -160,7 +158,7 @@ def parse_has_files( return contains_relationships def check_if_relationship_exists( - self, relationship: Relationship, existing_relationships: List[Relationship] + self, relationship: Relationship, existing_relationships: Set[Relationship] ) -> bool: # assume existing relationships are stripped of comments if relationship in existing_relationships: diff --git a/src/spdx_tools/spdx/validation/annotation_validator.py b/src/spdx_tools/spdx/validation/annotation_validator.py index 3fec188c7..4eeca49cc 100644 --- a/src/spdx_tools/spdx/validation/annotation_validator.py +++ b/src/spdx_tools/spdx/validation/annotation_validator.py @@ -2,29 +2,32 @@ # # SPDX-License-Identifier: Apache-2.0 -from beartype.typing import List +from beartype.typing import List, Set from spdx_tools.spdx.model import Annotation, Document from spdx_tools.spdx.validation.actor_validator import validate_actor -from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id +from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id, get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage def validate_annotations(annotations: List[Annotation], document: Document) -> List[ValidationMessage]: validation_messages = [] + + all_spdx_ids: Set[str] = get_all_spdx_ids(document) + for annotation in annotations: - validation_messages.extend(validate_annotation(annotation, document)) + validation_messages.extend(validate_annotation(annotation, document, all_spdx_ids)) return validation_messages -def validate_annotation(annotation: Annotation, document: Document) -> List[ValidationMessage]: +def validate_annotation(annotation: Annotation, document: Document, all_spdx_ids: Set[str]) -> List[ValidationMessage]: validation_messages = [] context = ValidationContext(element_type=SpdxElementType.ANNOTATION, full_element=annotation) validation_messages.extend(validate_actor(annotation.annotator, "annotation")) - messages: List[str] = validate_spdx_id(annotation.spdx_id, document, check_document=True) + messages: List[str] = validate_spdx_id(annotation.spdx_id, document, all_spdx_ids, check_document=True) for message in messages: validation_messages.append(ValidationMessage(message, context)) diff --git a/src/spdx_tools/spdx/validation/file_validator.py b/src/spdx_tools/spdx/validation/file_validator.py index a14efd4c4..0050e0d18 100644 --- a/src/spdx_tools/spdx/validation/file_validator.py +++ b/src/spdx_tools/spdx/validation/file_validator.py @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: Apache-2.0 -from beartype.typing import List, Optional +from beartype.typing import List, Optional, Set from spdx_tools.spdx.model import ChecksumAlgorithm, Document, File from spdx_tools.spdx.validation.checksum_validator import validate_checksums @@ -10,7 +10,7 @@ validate_license_expression, validate_license_expressions, ) -from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id +from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id, get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage @@ -19,8 +19,9 @@ def validate_files( ) -> List[ValidationMessage]: validation_messages = [] if document: + all_spdx_ids: Set[str] = get_all_spdx_ids(document) for file in files: - validation_messages.extend(validate_file_within_document(file, spdx_version, document)) + validation_messages.extend(validate_file_within_document(file, spdx_version, document, all_spdx_ids)) else: for file in files: validation_messages.extend(validate_file(file, spdx_version)) @@ -28,7 +29,9 @@ def validate_files( return validation_messages -def validate_file_within_document(file: File, spdx_version: str, document: Document) -> List[ValidationMessage]: +def validate_file_within_document( + file: File, spdx_version: str, document: Document, all_spdx_ids: Set[str] +) -> List[ValidationMessage]: validation_messages: List[ValidationMessage] = [] context = ValidationContext( spdx_id=file.spdx_id, @@ -37,8 +40,8 @@ def validate_file_within_document(file: File, spdx_version: str, document: Docum full_element=file, ) - for message in validate_spdx_id(file.spdx_id, document): - validation_messages.append(ValidationMessage(message, context)) + for message in validate_spdx_id(file.spdx_id, document, all_spdx_ids): + validation_messages.append(ValidationMessage(message, context, all_spdx_ids)) validation_messages.extend(validate_license_expression(file.license_concluded, document, file.spdx_id)) diff --git a/src/spdx_tools/spdx/validation/package_validator.py b/src/spdx_tools/spdx/validation/package_validator.py index 25cd6147f..14e1d8bf8 100644 --- a/src/spdx_tools/spdx/validation/package_validator.py +++ b/src/spdx_tools/spdx/validation/package_validator.py @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: Apache-2.0 -from beartype.typing import List, Optional +from beartype.typing import List, Optional, Set from spdx_tools.spdx.model import Document, File, Package, Relationship, RelationshipType from spdx_tools.spdx.model.relationship_filters import filter_by_type_and_origin, filter_by_type_and_target @@ -14,7 +14,7 @@ validate_license_expressions, ) from spdx_tools.spdx.validation.package_verification_code_validator import validate_verification_code -from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id +from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id, get_all_spdx_ids from spdx_tools.spdx.validation.uri_validators import validate_download_location, validate_url from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage @@ -23,9 +23,10 @@ def validate_packages( packages: List[Package], spdx_version: str, document: Optional[Document] = None ) -> List[ValidationMessage]: validation_messages: List[ValidationMessage] = [] + all_spdx_ids: Set[str] = get_all_spdx_ids(document) if document: for package in packages: - validation_messages.extend(validate_package_within_document(package, spdx_version, document)) + validation_messages.extend(validate_package_within_document(package, spdx_version, document, all_spdx_ids)) else: for package in packages: validation_messages.extend(validate_package(package, spdx_version)) @@ -34,7 +35,7 @@ def validate_packages( def validate_package_within_document( - package: Package, spdx_version: str, document: Document + package: Package, spdx_version: str, document: Document, all_spdx_ids: Set[str] ) -> List[ValidationMessage]: validation_messages: List[ValidationMessage] = [] context = ValidationContext( @@ -44,7 +45,7 @@ def validate_package_within_document( full_element=package, ) - for message in validate_spdx_id(package.spdx_id, document): + for message in validate_spdx_id(package.spdx_id, document, all_spdx_ids): validation_messages.append(ValidationMessage(message, context)) if not package.files_analyzed: diff --git a/src/spdx_tools/spdx/validation/relationship_validator.py b/src/spdx_tools/spdx/validation/relationship_validator.py index 1c194ac99..61c906f6c 100644 --- a/src/spdx_tools/spdx/validation/relationship_validator.py +++ b/src/spdx_tools/spdx/validation/relationship_validator.py @@ -2,10 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 -from beartype.typing import List +from beartype.typing import List, Set from spdx_tools.spdx.model import Document, Relationship, RelationshipType, SpdxNoAssertion, SpdxNone -from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id +from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id, get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage @@ -13,26 +13,30 @@ def validate_relationships( relationships: List[Relationship], spdx_version: str, document: Document ) -> List[ValidationMessage]: validation_messages = [] + + all_spdx_ids: Set[str] = get_all_spdx_ids(document) + for relationship in relationships: - validation_messages.extend(validate_relationship(relationship, spdx_version, document)) + validation_messages.extend(validate_relationship(relationship, spdx_version, document, all_spdx_ids)) return validation_messages def validate_relationship( - relationship: Relationship, spdx_version: str, document: Document + relationship: Relationship, spdx_version: str, document: Document, all_spdx_ids: Set[str], ) -> List[ValidationMessage]: validation_messages = [] context = ValidationContext(element_type=SpdxElementType.RELATIONSHIP, full_element=relationship) relationship_type: RelationshipType = relationship.relationship_type - messages: List[str] = validate_spdx_id(relationship.spdx_element_id, document, check_document=True) + messages: List[str] = validate_spdx_id(relationship.spdx_element_id, document, all_spdx_ids, check_document=True) + for message in messages: validation_messages.append(ValidationMessage(message, context)) if relationship.related_spdx_element_id not in [SpdxNone(), SpdxNoAssertion()]: - messages: List[str] = validate_spdx_id(relationship.related_spdx_element_id, document, check_document=True) + messages: List[str] = validate_spdx_id(relationship.related_spdx_element_id, document, all_spdx_ids, check_document=True) for message in messages: validation_messages.append(ValidationMessage(message, context)) diff --git a/src/spdx_tools/spdx/validation/snippet_validator.py b/src/spdx_tools/spdx/validation/snippet_validator.py index 8c1aed9c5..c516cf656 100644 --- a/src/spdx_tools/spdx/validation/snippet_validator.py +++ b/src/spdx_tools/spdx/validation/snippet_validator.py @@ -2,14 +2,14 @@ # # SPDX-License-Identifier: Apache-2.0 -from beartype.typing import List, Optional +from beartype.typing import List, Optional, Set from spdx_tools.spdx.model import Document, Snippet from spdx_tools.spdx.validation.license_expression_validator import ( validate_license_expression, validate_license_expressions, ) -from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id +from spdx_tools.spdx.validation.spdx_id_validators import validate_spdx_id, get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage @@ -17,18 +17,19 @@ def validate_snippets( snippets: List[Snippet], spdx_version: str, document: Optional[Document] = None ) -> List[ValidationMessage]: validation_messages = [] + all_spdx_ids: Set[str] = get_all_spdx_ids(document) if document: for snippet in snippets: - validation_messages.extend(validate_snippet_within_document(snippet, spdx_version, document)) + validation_messages.extend(validate_snippet_within_document(snippet, spdx_version, document, all_spdx_ids)) else: for snippet in snippets: - validation_messages.extend(validate_snippet(snippet, spdx_version)) + validation_messages.extend(validate_snippet(snippet, spdx_version, all_spdx_ids)) return validation_messages def validate_snippet_within_document( - snippet: Snippet, spdx_version: str, document: Document + snippet: Snippet, spdx_version: str, document: Document, all_spdx_ids: Set[str] ) -> List[ValidationMessage]: validation_messages: List[ValidationMessage] = [] context = ValidationContext( @@ -38,11 +39,11 @@ def validate_snippet_within_document( full_element=snippet, ) - messages: List[str] = validate_spdx_id(snippet.spdx_id, document) + messages: List[str] = validate_spdx_id(snippet.spdx_id, document, all_spdx_ids) for message in messages: validation_messages.append(ValidationMessage(message, context)) - messages: List[str] = validate_spdx_id(snippet.file_spdx_id, document, check_files=True) + messages: List[str] = validate_spdx_id(snippet.file_spdx_id, document, all_spdx_ids, check_files=True) for message in messages: validation_messages.append(ValidationMessage(message, context)) diff --git a/src/spdx_tools/spdx/validation/spdx_id_validators.py b/src/spdx_tools/spdx/validation/spdx_id_validators.py index 6441236a9..3f8a6f02b 100644 --- a/src/spdx_tools/spdx/validation/spdx_id_validators.py +++ b/src/spdx_tools/spdx/validation/spdx_id_validators.py @@ -4,7 +4,7 @@ import re -from beartype.typing import List +from beartype.typing import List, Set from spdx_tools.spdx.document_utils import get_contained_spdx_element_ids from spdx_tools.spdx.model import Document, File @@ -23,11 +23,15 @@ def is_spdx_id_present_in_files(spdx_id: str, files: List[File]) -> bool: def is_spdx_id_present_in_document(spdx_id: str, document: Document) -> bool: - all_spdx_ids_in_document: List[str] = get_list_of_all_spdx_ids(document) + all_spdx_ids_in_document: Set[str] = get_all_spdx_ids(document) return spdx_id in all_spdx_ids_in_document +def get_all_spdx_ids(document: Document) -> Set[str]: + return set(get_list_of_all_spdx_ids(document)) + + def get_list_of_all_spdx_ids(document: Document) -> List[str]: all_spdx_ids_in_document: List[str] = [document.creation_info.spdx_id] all_spdx_ids_in_document.extend(get_contained_spdx_element_ids(document)) @@ -44,7 +48,7 @@ def is_external_doc_ref_present_in_document(external_ref_id: str, document: Docu def validate_spdx_id( - spdx_id: str, document: Document, check_document: bool = False, check_files: bool = False + spdx_id: str, document: Document, all_spdx_ids: Set[str], check_document: bool = False, check_files: bool = False ) -> List[str]: """Test that the given spdx_id (and a potential DocumentRef to an external document) is valid and, if it is a reference, actually exists in the document. Optionally checks files or the whole document @@ -87,7 +91,7 @@ def validate_spdx_id( ) if check_document: - if not is_spdx_id_present_in_document(spdx_id, document): + if not spdx_id in all_spdx_ids: validation_messages.append(f'did not find the referenced spdx_id "{spdx_id}" in the SPDX document') if check_files: diff --git a/tests/spdx/validation/test_annotation_validator.py b/tests/spdx/validation/test_annotation_validator.py index 2462f9cda..ae30819c6 100644 --- a/tests/spdx/validation/test_annotation_validator.py +++ b/tests/spdx/validation/test_annotation_validator.py @@ -8,12 +8,13 @@ from spdx_tools.spdx.model import Annotation, Document from spdx_tools.spdx.validation.annotation_validator import validate_annotation +from spdx_tools.spdx.validation.spdx_id_validators import get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage from tests.spdx.fixtures import annotation_fixture, document_fixture, file_fixture def test_valid_annotation(): - validation_messages: List[ValidationMessage] = validate_annotation(annotation_fixture(), document_fixture()) + validation_messages: List[ValidationMessage] = validate_annotation(annotation_fixture(), document_fixture(), get_all_spdx_ids(document_fixture())) assert validation_messages == [] @@ -31,7 +32,7 @@ def test_valid_annotation(): def test_invalid_annotation(annotation_id, file_id, expected_message): annotation: Annotation = annotation_fixture(spdx_id=annotation_id) document: Document = document_fixture(files=[file_fixture(spdx_id=file_id)]) - validation_messages: List[ValidationMessage] = validate_annotation(annotation, document) + validation_messages: List[ValidationMessage] = validate_annotation(annotation, document, get_all_spdx_ids(document)) expected = ValidationMessage( expected_message, ValidationContext(element_type=SpdxElementType.ANNOTATION, full_element=annotation) diff --git a/tests/spdx/validation/test_file_validator.py b/tests/spdx/validation/test_file_validator.py index 73f107a9f..e09931ec8 100644 --- a/tests/spdx/validation/test_file_validator.py +++ b/tests/spdx/validation/test_file_validator.py @@ -9,13 +9,15 @@ from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm from spdx_tools.spdx.validation.file_validator import validate_file, validate_file_within_document +from spdx_tools.spdx.validation.spdx_id_validators import get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage from tests.spdx.fixtures import document_fixture, file_fixture def test_valid_file(): file = file_fixture() - validation_messages: List[ValidationMessage] = validate_file_within_document(file, "SPDX-2.3", document_fixture()) + document = document_fixture() + validation_messages: List[ValidationMessage] = validate_file_within_document(file, "SPDX-2.3", document, get_all_spdx_ids(document)) assert validation_messages == [] @@ -36,8 +38,9 @@ def test_valid_file(): ], ) def test_invalid_file(file_input, spdx_id, expected_message): + document = document_fixture() validation_messages: List[ValidationMessage] = validate_file_within_document( - file_input, "SPDX-2.3", document_fixture() + file_input, "SPDX-2.3", document, get_all_spdx_ids(document) ) expected = ValidationMessage( diff --git a/tests/spdx/validation/test_package_validator.py b/tests/spdx/validation/test_package_validator.py index a6ef976ef..de8b1fc65 100644 --- a/tests/spdx/validation/test_package_validator.py +++ b/tests/spdx/validation/test_package_validator.py @@ -9,16 +9,18 @@ from license_expression import Licensing from spdx_tools.spdx.constants import DOCUMENT_SPDX_ID -from spdx_tools.spdx.model import Relationship, RelationshipType, SpdxNoAssertion, SpdxNone +from spdx_tools.spdx.model import Document, Relationship, RelationshipType, SpdxNoAssertion, SpdxNone from spdx_tools.spdx.validation.package_validator import validate_package, validate_package_within_document +from spdx_tools.spdx.validation.spdx_id_validators import get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage from tests.spdx.fixtures import document_fixture, file_fixture, package_fixture, package_verification_code_fixture def test_valid_package(): + document: Document = document_fixture() package = package_fixture() validation_messages: List[ValidationMessage] = validate_package_within_document( - package, "SPDX-2.3", document_fixture() + package, "SPDX-2.3", document, get_all_spdx_ids(document) ) assert validation_messages == [] @@ -54,8 +56,9 @@ def test_valid_package(): ], ) def test_invalid_package(package_input, expected_message): + document: Document = document_fixture(relationships=[]) validation_messages: List[ValidationMessage] = validate_package_within_document( - package_input, "SPDX-2.3", document_fixture(relationships=[]) + package_input, "SPDX-2.3", document, get_all_spdx_ids(document) ) expected = ValidationMessage( @@ -85,7 +88,7 @@ def test_valid_package_with_contains(relationships): ) package = package_fixture(files_analyzed=False, verification_code=None, license_info_from_files=[]) - validation_messages: List[ValidationMessage] = validate_package_within_document(package, "SPDX-2.3", document) + validation_messages: List[ValidationMessage] = validate_package_within_document(package, "SPDX-2.3", document, get_all_spdx_ids(document)) assert validation_messages == [] @@ -114,7 +117,7 @@ def test_invalid_package_with_contains(relationships): full_element=package, ) - validation_messages: List[ValidationMessage] = validate_package_within_document(package, "SPDX-2.3", document) + validation_messages: List[ValidationMessage] = validate_package_within_document(package, "SPDX-2.3", document, get_all_spdx_ids(document)) assert validation_messages == [ ValidationMessage( diff --git a/tests/spdx/validation/test_relationship_validator.py b/tests/spdx/validation/test_relationship_validator.py index 28cae9d7d..5c80fe528 100644 --- a/tests/spdx/validation/test_relationship_validator.py +++ b/tests/spdx/validation/test_relationship_validator.py @@ -9,6 +9,7 @@ from spdx_tools.spdx.constants import DOCUMENT_SPDX_ID from spdx_tools.spdx.model import Document, Relationship, RelationshipType, SpdxNoAssertion, SpdxNone from spdx_tools.spdx.validation.relationship_validator import validate_relationship +from spdx_tools.spdx.validation.spdx_id_validators import get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage from tests.spdx.fixtures import document_fixture, relationship_fixture @@ -16,7 +17,7 @@ @pytest.mark.parametrize("related_spdx_element", ["SPDXRef-Package", SpdxNoAssertion(), SpdxNone()]) def test_valid_relationship(related_spdx_element): relationship = Relationship(DOCUMENT_SPDX_ID, RelationshipType.DESCRIBES, related_spdx_element, comment="comment") - validation_messages: List[ValidationMessage] = validate_relationship(relationship, "SPDX-2.3", document_fixture()) + validation_messages: List[ValidationMessage] = validate_relationship(relationship, "SPDX-2.3", document_fixture(), get_all_spdx_ids(document_fixture())) assert validation_messages == [] @@ -40,7 +41,7 @@ def test_unknown_spdx_id(spdx_element_id, related_spdx_element_id, expected_mess relationship: Relationship = relationship_fixture( spdx_element_id=spdx_element_id, related_spdx_element_id=related_spdx_element_id ) - validation_messages: List[ValidationMessage] = validate_relationship(relationship, "SPDX-2.3", document_fixture()) + validation_messages: List[ValidationMessage] = validate_relationship(relationship, "SPDX-2.3", document_fixture(), get_all_spdx_ids(document_fixture())) expected = ValidationMessage( expected_message, ValidationContext(element_type=SpdxElementType.RELATIONSHIP, full_element=relationship) @@ -65,7 +66,7 @@ def test_unknown_spdx_id(spdx_element_id, related_spdx_element_id, expected_mess def test_v2_3_only_types(relationship, expected_message): document: Document = document_fixture() - validation_message: List[ValidationMessage] = validate_relationship(relationship, "SPDX-2.2", document) + validation_message: List[ValidationMessage] = validate_relationship(relationship, "SPDX-2.2", document, get_all_spdx_ids(document)) expected = [ ValidationMessage( diff --git a/tests/spdx/validation/test_snippet_validator.py b/tests/spdx/validation/test_snippet_validator.py index 8b8d2af4b..6415e01f6 100644 --- a/tests/spdx/validation/test_snippet_validator.py +++ b/tests/spdx/validation/test_snippet_validator.py @@ -8,14 +8,16 @@ import pytest from spdx_tools.spdx.validation.snippet_validator import validate_snippet, validate_snippet_within_document +from spdx_tools.spdx.validation.spdx_id_validators import get_all_spdx_ids from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage from tests.spdx.fixtures import document_fixture, snippet_fixture def test_valid_snippet(): + document = document_fixture() snippet = snippet_fixture() validation_messages: List[ValidationMessage] = validate_snippet_within_document( - snippet, "SPDX-2.3", document_fixture() + snippet, "SPDX-2.3", document, get_all_spdx_ids(document) ) assert validation_messages == [] @@ -43,8 +45,9 @@ def test_valid_snippet(): ], ) def test_invalid_ranges(snippet_input, expected_message): + document = document_fixture() validation_messages: List[ValidationMessage] = validate_snippet_within_document( - snippet_input, "SPDX-2.3", document_fixture() + snippet_input, "SPDX-2.3", document, get_all_spdx_ids(document) ) expected = ValidationMessage( diff --git a/tests/spdx/validation/test_spdx_id_validators.py b/tests/spdx/validation/test_spdx_id_validators.py index 8cc2d3b11..e9f8e9899 100644 --- a/tests/spdx/validation/test_spdx_id_validators.py +++ b/tests/spdx/validation/test_spdx_id_validators.py @@ -8,6 +8,7 @@ from spdx_tools.spdx.constants import DOCUMENT_SPDX_ID from spdx_tools.spdx.validation.spdx_id_validators import ( get_list_of_all_spdx_ids, + get_all_spdx_ids, is_external_doc_ref_present_in_document, is_spdx_id_present_in_document, is_valid_external_doc_ref_id, @@ -90,7 +91,7 @@ def test_list_of_all_spdx_ids(): @pytest.mark.parametrize("spdx_id", ["DocumentRef-external:SPDXRef-File", "SPDXRef-Package"]) def test_valid_spdx_id(spdx_id): - validation_messages = validate_spdx_id(spdx_id, DOCUMENT) + validation_messages = validate_spdx_id(spdx_id, DOCUMENT, get_all_spdx_ids(DOCUMENT)) assert validation_messages == [] @@ -140,7 +141,7 @@ def test_valid_spdx_id(spdx_id): ], ) def test_invalid_spdx_id(spdx_id, expected_messages): - validation_messages = validate_spdx_id(spdx_id, DOCUMENT) + validation_messages = validate_spdx_id(spdx_id, DOCUMENT, get_all_spdx_ids(DOCUMENT)) TestCase().assertCountEqual(validation_messages, expected_messages) @@ -150,23 +151,23 @@ def test_invalid_spdx_id(spdx_id, expected_messages): ["DocumentRef-external:SPDXRef-File", DOCUMENT_SPDX_ID, "SPDXRef-File1", "SPDXRef-Package1", "SPDXRef-Snippet1"], ) def test_valid_spdx_id_with_check_document(spdx_id): - validation_messages = validate_spdx_id(spdx_id, DOCUMENT, check_document=True) + validation_messages = validate_spdx_id(spdx_id, DOCUMENT, get_all_spdx_ids(DOCUMENT), check_document=True) assert validation_messages == [] def test_invalid_spdx_id_with_check_document(): - validation_messages = validate_spdx_id("SPDXRef-Filet", DOCUMENT, check_document=True) + validation_messages = validate_spdx_id("SPDXRef-Filet", DOCUMENT, get_all_spdx_ids(DOCUMENT), check_document=True) assert validation_messages == ['did not find the referenced spdx_id "SPDXRef-Filet" in the SPDX document'] @pytest.mark.parametrize("spdx_id", ["DocumentRef-external:SPDXRef-File", "SPDXRef-File1"]) def test_valid_spdx_id_with_check_files(spdx_id): - validation_messages = validate_spdx_id(spdx_id, DOCUMENT, check_files=True) + validation_messages = validate_spdx_id(spdx_id, DOCUMENT, get_all_spdx_ids(DOCUMENT), check_files=True) assert validation_messages == [] def test_invalid_spdx_id_with_check_files(): - validation_messages = validate_spdx_id("SPDXRef-Package1", DOCUMENT, check_files=True) + validation_messages = validate_spdx_id("SPDXRef-Package1", DOCUMENT, get_all_spdx_ids(DOCUMENT), check_files=True) assert validation_messages == [ 'did not find the referenced spdx_id "SPDXRef-Package1" in the SPDX document\'s files' ]