Skip to content

Commit 2402596

Browse files
make "Package CONTAINS Package" valid even when files_analyzed == False
Signed-off-by: Armin Tänzer <[email protected]>
1 parent 17767bd commit 2402596

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/spdx_tools/spdx/spdx_element_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
# SPDX-License-Identifier: Apache-2.0
44
import hashlib
55

6-
from beartype.typing import List, Union
6+
from beartype.typing import List, Optional, Type, Union
77

88
from spdx_tools.spdx.model import (
99
ChecksumAlgorithm,
10+
Document,
1011
ExternalDocumentRef,
1112
File,
1213
Package,
@@ -15,6 +16,18 @@
1516
)
1617

1718

19+
def get_element_type_from_spdx_id(
20+
spdx_id: str, document: Document
21+
) -> Optional[Union[Type[Package], Type[File], Type[Snippet]]]:
22+
if spdx_id in [package.spdx_id for package in document.packages]:
23+
return Package
24+
if spdx_id in [file.spdx_id for file in document.files]:
25+
return File
26+
if spdx_id in [snippet.spdx_id for snippet in document.snippets]:
27+
return Snippet
28+
return None
29+
30+
1831
def get_full_element_spdx_id(
1932
element: Union[Package, File, Snippet],
2033
document_namespace: str,

src/spdx_tools/spdx/validation/package_validator.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from beartype.typing import List, Optional
66

7-
from spdx_tools.spdx.model import Document, Package, Relationship, RelationshipType
7+
from spdx_tools.spdx.model import Document, File, Package, Relationship, RelationshipType
88
from spdx_tools.spdx.model.relationship_filters import filter_by_type_and_origin, filter_by_type_and_target
9+
from spdx_tools.spdx.spdx_element_utils import get_element_type_from_spdx_id
910
from spdx_tools.spdx.validation.checksum_validator import validate_checksums
1011
from spdx_tools.spdx.validation.external_package_ref_validator import validate_external_package_refs
1112
from spdx_tools.spdx.validation.license_expression_validator import (
@@ -50,12 +51,23 @@ def validate_package_within_document(
5051
package_contains_relationships = filter_by_type_and_origin(
5152
document.relationships, RelationshipType.CONTAINS, package.spdx_id
5253
)
54+
package_contains_file_relationships = [
55+
relationship
56+
for relationship in package_contains_relationships
57+
if get_element_type_from_spdx_id(relationship.related_spdx_element_id, document) == File
58+
]
59+
5360
contained_in_package_relationships = filter_by_type_and_target(
5461
document.relationships, RelationshipType.CONTAINED_BY, package.spdx_id
5562
)
63+
file_contained_in_package_relationships = [
64+
relationship
65+
for relationship in contained_in_package_relationships
66+
if get_element_type_from_spdx_id(relationship.spdx_element_id, document) == File
67+
]
5668

5769
combined_relationships: List[Relationship] = (
58-
package_contains_relationships + contained_in_package_relationships
70+
package_contains_file_relationships + file_contained_in_package_relationships
5971
)
6072

6173
if combined_relationships:

tests/spdx/validation/test_package_validator.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,27 @@ def test_invalid_package(package_input, expected_message):
7474
@pytest.mark.parametrize(
7575
"relationships",
7676
[
77-
[Relationship("SPDXRef-Package", RelationshipType.CONTAINS, "SPDXRef-File1")],
7877
[Relationship("SPDXRef-Package", RelationshipType.CONTAINS, "DocumentRef-external:SPDXRef-File")],
79-
[Relationship("SPDXRef-File2", RelationshipType.CONTAINED_BY, "SPDXRef-Package")],
8078
[Relationship("DocumentRef-external:SPDXRef-File", RelationshipType.CONTAINED_BY, "SPDXRef-Package")],
79+
],
80+
)
81+
def test_valid_package_with_contains(relationships):
82+
document = document_fixture(
83+
relationships=relationships,
84+
files=[file_fixture(spdx_id="SPDXRef-File1"), file_fixture(spdx_id="SPDXRef-File2")],
85+
)
86+
package = package_fixture(files_analyzed=False, verification_code=None, license_info_from_files=[])
87+
88+
validation_messages: List[ValidationMessage] = validate_package_within_document(package, "SPDX-2.3", document)
89+
90+
assert validation_messages == []
91+
92+
93+
@pytest.mark.parametrize(
94+
"relationships",
95+
[
96+
[Relationship("SPDXRef-Package", RelationshipType.CONTAINS, "SPDXRef-File1")],
97+
[Relationship("SPDXRef-File2", RelationshipType.CONTAINED_BY, "SPDXRef-Package")],
8198
[
8299
Relationship("SPDXRef-Package", RelationshipType.CONTAINS, "SPDXRef-File2"),
83100
Relationship("SPDXRef-File1", RelationshipType.CONTAINED_BY, "SPDXRef-Package"),

0 commit comments

Comments
 (0)