Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-378] implement invalidation of duplicated SPDX Ids #459

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/spdx/validation/document_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from spdx.validation.package_validator import validate_packages
from spdx.validation.relationship_validator import validate_relationships
from spdx.validation.snippet_validator import validate_snippets
from spdx.validation.spdx_id_validators import get_list_of_all_spdx_ids
from spdx.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType


Expand Down Expand Up @@ -74,4 +75,16 @@ def validate_full_spdx_document(document: Document, spdx_version: str = None) ->
ValidationContext(spdx_id=document_id,
element_type=SpdxElementType.DOCUMENT)))

all_spdx_ids: List[str] = get_list_of_all_spdx_ids(document)
auxiliary_set = set()
duplicated_spdx_ids = set(
spdx_id for spdx_id in all_spdx_ids if spdx_id in auxiliary_set or auxiliary_set.add(spdx_id))

if duplicated_spdx_ids:
validation_messages.append(
ValidationMessage(
f"every spdx_id must be unique within the document, but found the following duplicates: {sorted(duplicated_spdx_ids)}",
context)
)

return validation_messages
17 changes: 16 additions & 1 deletion tests/spdx/validation/test_document_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from spdx.model.document import Document, CreationInfo
from spdx.validation.document_validator import validate_full_spdx_document
from spdx.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType
from tests.spdx.fixtures import document_fixture, creation_info_fixture
from tests.spdx.fixtures import document_fixture, creation_info_fixture, file_fixture, package_fixture, snippet_fixture


def test_valid_document():
Expand Down Expand Up @@ -56,3 +56,18 @@ def test_spdx_version_handling(creation_info: CreationInfo, version_input: str,
assert validation_messages == expected

# TODO: https://github.com/spdx/tools-python/issues/375


def test_duplicated_spdx_ids():
document = document_fixture(
files=[file_fixture(spdx_id="SPDXRef-File"), file_fixture(spdx_id="SPDXRef-2"), file_fixture(spdx_id="SPDXRef-3")],
packages=[package_fixture(spdx_id="SPDXRef-2"), package_fixture(spdx_id="SPDXRef-DOCUMENT")],
snippets=[snippet_fixture(spdx_id="SPDXRef-2"), snippet_fixture(spdx_id="SPDXRef-3")])

context = ValidationContext(spdx_id=document.creation_info.spdx_id, element_type=SpdxElementType.DOCUMENT)

validation_messages: List[ValidationMessage] = validate_full_spdx_document(document)

assert validation_messages == [ValidationMessage(
"every spdx_id must be unique within the document, but found the following duplicates: ['SPDXRef-2', 'SPDXRef-3', 'SPDXRef-DOCUMENT']",
context)]