Skip to content

Commit

Permalink
[issue-466] bug fix and add test
Browse files Browse the repository at this point in the history
check against RelationshipType to let file information precede package information which contains the file

Signed-off-by: Meret Behrens <meret.behrens@tngtech.com>
  • Loading branch information
meretp committed Feb 8, 2023
1 parent e25467f commit 2fc49b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/spdx/writer/tagvalue/tagvalue_writer_helper_functions.py
Expand Up @@ -14,7 +14,7 @@
from spdx.model.file import File
from license_expression import LicenseExpression
from spdx.model.package import Package
from spdx.model.relationship import Relationship
from spdx.model.relationship import Relationship, RelationshipType
from spdx.model.snippet import Snippet
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
Expand Down Expand Up @@ -81,19 +81,19 @@ def scan_relationships(relationships: List[Relationship], packages: List[Package
files_by_spdx_id = {file.spdx_id: file for file in files}
packages_spdx_ids = [package.spdx_id for package in packages]
for relationship in relationships:
if relationship.relationship_type == "CONTAINS" and \
if relationship.relationship_type == RelationshipType.CONTAINS and \
relationship.spdx_element_id in packages_spdx_ids and \
relationship.related_spdx_element in files_by_spdx_id.keys():
relationship.related_spdx_element_id in files_by_spdx_id.keys():
contained_files_by_package_id.setdefault(relationship.spdx_element_id, []).append(
files_by_spdx_id[relationship.related_spdx_element])
if relationship.has_comment:
files_by_spdx_id[relationship.related_spdx_element_id])
if relationship.comment:
relationships_to_write.append(relationship)
elif relationship.relationship_type == "CONTAINED_BY" and \
relationship.related_spdx_element in packages_spdx_ids and \
elif relationship.relationship_type == RelationshipType.CONTAINED_BY and \
relationship.related_spdx_element_id in packages_spdx_ids and \
relationship.spdx_element_id in files_by_spdx_id:
contained_files_by_package_id.setdefault(relationship.related_spdx_element, []).append(
contained_files_by_package_id.setdefault(relationship.related_spdx_element_id, []).append(
files_by_spdx_id[relationship.spdx_element_id])
if relationship.has_comment:
if relationship.comment:
relationships_to_write.append(relationship)
else:
relationships_to_write.append(relationship)
Expand Down
@@ -0,0 +1,33 @@
# Copyright (c) 2023 spdx contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from spdx.model.relationship import RelationshipType
from spdx.writer.tagvalue.tagvalue_writer_helper_functions import scan_relationships
from tests.spdx.fixtures import package_fixture, file_fixture, relationship_fixture


def test_scan_relationships():
first_package_spdx_id = "SPDXRef-Package1"
second_package_spdx_id = "SPDXRef-Package2"
packages = [package_fixture(spdx_id=first_package_spdx_id), package_fixture(spdx_id=second_package_spdx_id)]
file_spdx_id = "SPDXRef-File"
files = [file_fixture(spdx_id=file_spdx_id)]
relationships = [
relationship_fixture(spdx_element_id=first_package_spdx_id, relationship_type=RelationshipType.CONTAINS,
related_spdx_element_id=file_spdx_id, comment=None),
relationship_fixture(spdx_element_id=second_package_spdx_id, relationship_type=RelationshipType.CONTAINS,
related_spdx_element_id=file_spdx_id, comment=None)
]

relationships_to_write, contained_files_by_package_id = scan_relationships(relationships, packages, files)

assert relationships_to_write == []
assert contained_files_by_package_id == {first_package_spdx_id: files,
second_package_spdx_id: files}

0 comments on commit 2fc49b4

Please sign in to comment.