Skip to content

Commit 01b8a75

Browse files
committed
[issue-394] add tests for tag_value writer
Signed-off-by: Meret Behrens <[email protected]>
1 parent 91bb744 commit 01b8a75

9 files changed

+362
-28
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2023 spdx contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from unittest.mock import MagicMock, call, mock_open, patch
5+
6+
from spdx.writer.tagvalue.annotation_writer import write_annotation
7+
from tests.spdx.fixtures import annotation_fixture
8+
9+
10+
def test_annotation_writer():
11+
annotation = annotation_fixture()
12+
13+
mock: MagicMock = mock_open()
14+
with patch(f"{__name__}.open", mock, create=True):
15+
with open("foo", "w") as file:
16+
write_annotation(annotation, file)
17+
18+
mock.assert_called_once_with("foo", "w")
19+
handle = mock()
20+
handle.write.assert_has_calls(
21+
[
22+
call(f"Annotator: Person: {annotation.annotator.name} ({annotation.annotator.email})\n"),
23+
call("AnnotationDate: 2022-12-01T00:00:00Z\n"),
24+
call(f"AnnotationType: {annotation.annotation_type.name}\n"),
25+
call(f"SPDXREF: {annotation.spdx_id}\n"),
26+
call(f"AnnotationComment: {annotation.annotation_comment}\n"),
27+
]
28+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SPDX-FileCopyrightText: 2023 spdx contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
import pytest
5+
6+
from spdx.model.checksum import ChecksumAlgorithm
7+
from spdx.writer.tagvalue.checksum_writer import write_checksum_to_tag_value
8+
from tests.spdx.fixtures import checksum_fixture
9+
10+
11+
@pytest.mark.parametrize(
12+
"checksum, expected_string",
13+
[
14+
(checksum_fixture(), "SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c"),
15+
(checksum_fixture(algorithm=ChecksumAlgorithm.SHA3_256, value="fdsef"), "SHA3-256: fdsef"),
16+
(checksum_fixture(algorithm=ChecksumAlgorithm.SHA3_384, value="fdsef"), "SHA3-384: fdsef"),
17+
(checksum_fixture(algorithm=ChecksumAlgorithm.SHA3_512, value="fdsef"), "SHA3-512: fdsef"),
18+
(checksum_fixture(algorithm=ChecksumAlgorithm.BLAKE2B_256, value="fdsef"), "BLAKE2b-256: fdsef"),
19+
(checksum_fixture(algorithm=ChecksumAlgorithm.BLAKE2B_384, value="fdsef"), "BLAKE2b-384: fdsef"),
20+
(checksum_fixture(algorithm=ChecksumAlgorithm.BLAKE2B_512, value="fdsef"), "BLAKE2b-512: fdsef"),
21+
],
22+
)
23+
def test_checksum_writer(checksum, expected_string):
24+
checksum_string = write_checksum_to_tag_value(checksum)
25+
26+
assert checksum_string == expected_string
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2023 spdx contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from unittest.mock import MagicMock, call, mock_open, patch
5+
6+
from spdx.writer.tagvalue.extracted_licensing_info_writer import write_extracted_licensing_info
7+
from tests.spdx.fixtures import extracted_licensing_info_fixture
8+
9+
10+
def test_extracted_licensing_info_writer():
11+
extracted_licensing_info = extracted_licensing_info_fixture()
12+
13+
mock: MagicMock = mock_open()
14+
with patch(f"{__name__}.open", mock, create=True):
15+
with open("foo", "w") as file:
16+
write_extracted_licensing_info(extracted_licensing_info, file)
17+
18+
mock.assert_called_once_with("foo", "w")
19+
handle = mock()
20+
handle.write.assert_has_calls(
21+
[
22+
call(f"LicenseID: {extracted_licensing_info.license_id}\n"),
23+
call(f"ExtractedText: {extracted_licensing_info.extracted_text}\n"),
24+
call(f"LicenseName: {extracted_licensing_info.license_name}\n"),
25+
call(f"LicenseCrossReference: {extracted_licensing_info.cross_references[0]}\n"),
26+
call(f"LicenseComment: {extracted_licensing_info.comment}\n"),
27+
]
28+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2023 spdx contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from unittest.mock import MagicMock, call, mock_open, patch
5+
6+
from spdx.writer.tagvalue.file_writer import write_file
7+
from tests.spdx.fixtures import file_fixture
8+
9+
10+
def test_file_writer():
11+
spdx_file = file_fixture()
12+
13+
mock: MagicMock = mock_open()
14+
with patch(f"{__name__}.open", mock, create=True):
15+
with open("foo", "w") as file:
16+
write_file(spdx_file, file)
17+
18+
mock.assert_called_once_with("foo", "w")
19+
handle = mock()
20+
handle.write.assert_has_calls(
21+
[
22+
call("## File Information\n"),
23+
call(f"FileName: {spdx_file.name}\n"),
24+
call(f"SPDXID: {spdx_file.spdx_id}\n"),
25+
call(f"FileType: {spdx_file.file_types[0].name}\n"),
26+
call("FileChecksum: SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"),
27+
call(f"LicenseConcluded: {spdx_file.license_concluded}\n"),
28+
call(f"LicenseInfoInFile: {spdx_file.license_info_in_file[0]}\n"),
29+
call(f"LicenseInfoInFile: {spdx_file.license_info_in_file[1]}\n"),
30+
call(f"LicenseInfoInFile: {spdx_file.license_info_in_file[2]}\n"),
31+
call(f"LicenseComments: {spdx_file.license_comment}\n"),
32+
call(f"FileCopyrightText: {spdx_file.copyright_text}\n"),
33+
call(f"FileComment: {spdx_file.comment}\n"),
34+
call(f"FileNotice: {spdx_file.notice}\n"),
35+
call(f"FileContributor: {spdx_file.contributors[0]}\n"),
36+
call(f"FileAttributionText: {spdx_file.attribution_texts[0]}\n"),
37+
]
38+
)

tests/spdx/writer/tagvalue/test_package_writer.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,35 @@ def test_package_writer():
2020
handle.write.assert_has_calls(
2121
[
2222
call("## Package Information\n"),
23-
call("PackageName: packageName\n"),
24-
call("SPDXID: SPDXRef-Package\n"),
25-
call("PackageVersion: 12.2\n"),
26-
call("PackageFileName: ./packageFileName\n"),
27-
call("PackageSupplier: Person: supplierName ([email protected])\n"),
28-
call("PackageOriginator: Person: originatorName ([email protected])\n"),
29-
call("PackageDownloadLocation: https://download.com\n"),
23+
call(f"PackageName: {package.name}\n"),
24+
call(f"SPDXID: {package.spdx_id}\n"),
25+
call(f"PackageVersion: {package.version}\n"),
26+
call(f"PackageFileName: {package.file_name}\n"),
27+
call(f"PackageSupplier: Person: {package.supplier.name} ({package.supplier.email})\n"),
28+
call(f"PackageOriginator: Person: {package.originator.name} ({package.originator.email})\n"),
29+
call(f"PackageDownloadLocation: {package.download_location}\n"),
3030
call("FilesAnalyzed: True\n"),
31-
call("PackageVerificationCode: 85ed0817af83a24ad8da68c2b5094de69833983c (excludes: ./exclude.py)\n"),
31+
call(f"PackageVerificationCode: {package.verification_code.value} (excludes: ./exclude.py)\n"),
3232
call("PackageChecksum: SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"),
33-
call("PackageHomePage: https://homepage.com\n"),
34-
call("PackageSourceInfo: sourceInfo\n"),
35-
call("PackageLicenseConcluded: MIT AND GPL-2.0-only\n"),
36-
call("PackageLicenseInfoFromFiles: MIT\n"),
37-
call("PackageLicenseInfoFromFiles: GPL-2.0-only\n"),
38-
call("PackageLicenseInfoFromFiles: NOASSERTION\n"),
39-
call("PackageLicenseDeclared: MIT AND GPL-2.0-only\n"),
40-
call("PackageLicenseComments: packageLicenseComment\n"),
41-
call("PackageCopyrightText: packageCopyrightText\n"),
42-
call("PackageSummary: packageSummary\n"),
43-
call("PackageDescription: packageDescription\n"),
44-
call("PackageComment: packageComment\n"),
45-
call("ExternalRef: PACKAGE-MANAGER maven-central org.apache.tomcat:tomcat:9.0.0.M4\n"),
46-
call("ExternalRefComment: externalPackageRefComment\n"),
47-
call("PackageAttributionText: packageAttributionText\n"),
48-
call("PrimaryPackagePurpose: SOURCE\n"),
33+
call(f"PackageHomePage: {package.homepage}\n"),
34+
call(f"PackageSourceInfo: {package.source_info}\n"),
35+
call(f"PackageLicenseConcluded: {package.license_concluded}\n"),
36+
call(f"PackageLicenseInfoFromFiles: {package.license_info_from_files[0]}\n"),
37+
call(f"PackageLicenseInfoFromFiles: {package.license_info_from_files[1]}\n"),
38+
call(f"PackageLicenseInfoFromFiles: {package.license_info_from_files[2]}\n"),
39+
call(f"PackageLicenseDeclared: {package.license_declared}\n"),
40+
call(f"PackageLicenseComments: {package.license_comment}\n"),
41+
call(f"PackageCopyrightText: {package.copyright_text}\n"),
42+
call(f"PackageSummary: {package.summary}\n"),
43+
call(f"PackageDescription: {package.description}\n"),
44+
call(f"PackageComment: {package.comment}\n"),
45+
call(
46+
f"ExternalRef: PACKAGE-MANAGER {package.external_references[0].reference_type} "
47+
f"{package.external_references[0].locator}\n"
48+
),
49+
call(f"ExternalRefComment: {package.external_references[0].comment}\n"),
50+
call(f"PackageAttributionText: {package.attribution_texts[0]}\n"),
51+
call(f"PrimaryPackagePurpose: {package.primary_package_purpose.name}\n"),
4952
call("ReleaseDate: 2022-12-01T00:00:00Z\n"),
5053
call("BuiltDate: 2022-12-02T00:00:00Z\n"),
5154
call("ValidUntilDate: 2022-12-03T00:00:00Z\n"),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: 2023 spdx contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from unittest.mock import MagicMock, call, mock_open, patch
5+
6+
import pytest
7+
8+
from spdx.model.spdx_no_assertion import SpdxNoAssertion
9+
from spdx.model.spdx_none import SpdxNone
10+
from spdx.writer.tagvalue.relationship_writer import write_relationship
11+
from tests.spdx.fixtures import relationship_fixture
12+
13+
14+
@pytest.mark.parametrize(
15+
"relationship, expected_calls",
16+
[
17+
(
18+
relationship_fixture(),
19+
[
20+
call("Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-File\n"),
21+
call("RelationshipComment: relationshipComment\n"),
22+
],
23+
),
24+
(
25+
relationship_fixture(related_spdx_element_id=SpdxNoAssertion(), comment=None),
26+
[call("Relationship: SPDXRef-DOCUMENT DESCRIBES NOASSERTION\n")],
27+
),
28+
(
29+
relationship_fixture(
30+
spdx_element_id="DocumentRef-External:SPDXRef-DOCUMENT",
31+
related_spdx_element_id=SpdxNone(),
32+
comment=None,
33+
),
34+
[call("Relationship: DocumentRef-External:SPDXRef-DOCUMENT DESCRIBES NONE\n")],
35+
),
36+
],
37+
)
38+
def test_relationship_writer(relationship, expected_calls):
39+
mock: MagicMock = mock_open()
40+
with patch(f"{__name__}.open", mock, create=True):
41+
with open("foo", "w") as file:
42+
write_relationship(relationship, file)
43+
44+
mock.assert_called_once_with("foo", "w")
45+
handle = mock()
46+
handle.write.assert_has_calls(expected_calls)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2023 spdx contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from unittest.mock import MagicMock, call, mock_open, patch
5+
6+
from spdx.writer.tagvalue.snippet_writer import write_snippet
7+
from tests.spdx.fixtures import snippet_fixture
8+
9+
10+
def test_snippet_writer():
11+
snippet = snippet_fixture()
12+
13+
mock: MagicMock = mock_open()
14+
with patch(f"{__name__}.open", mock, create=True):
15+
with open("foo", "w") as file:
16+
write_snippet(snippet, file)
17+
18+
mock.assert_called_once_with("foo", "w")
19+
handle = mock()
20+
handle.write.assert_has_calls(
21+
[
22+
call("## Snippet Information\n"),
23+
call(f"SnippetSPDXID: {snippet.spdx_id}\n"),
24+
call(f"SnippetFromFileSPDXID: {snippet.file_spdx_id}\n"),
25+
call("SnippetByteRange: 1:2\n"),
26+
call("SnippetLineRange: 3:4\n"),
27+
call(f"SnippetLicenseConcluded: {snippet.license_concluded}\n"),
28+
call(f"LicenseInfoInSnippet: {snippet.license_info_in_snippet[0]}\n"),
29+
call(f"LicenseInfoInSnippet: {snippet.license_info_in_snippet[1]}\n"),
30+
call(f"LicenseInfoInSnippet: {snippet.license_info_in_snippet[2]}\n"),
31+
call(f"SnippetLicenseComments: {snippet.license_comment}\n"),
32+
call(f"SnippetCopyrightText: {snippet.copyright_text}\n"),
33+
call(f"SnippetComment: {snippet.comment}\n"),
34+
call(f"SnippetName: {snippet.name}\n"),
35+
call(f"SnippetAttributionText: {snippet.attribution_texts[0]}\n"),
36+
]
37+
)

tests/spdx/writer/tagvalue/test_tagvalue_writer.py

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import os
6+
from unittest.mock import MagicMock, call, mock_open, patch
67

78
import pytest
89

10+
from spdx.model.file import File
11+
from spdx.model.package import Package
12+
from spdx.model.relationship import Relationship, RelationshipType
13+
from spdx.model.snippet import Snippet
914
from spdx.parser.tagvalue import tagvalue_parser
10-
from spdx.writer.tagvalue.tagvalue_writer import write_document_to_file
11-
from tests.spdx.fixtures import document_fixture
15+
from spdx.writer.tagvalue.tagvalue_writer import write_document, write_document_to_file
16+
from tests.spdx.fixtures import checksum_fixture, document_fixture
1217

1318

1419
@pytest.fixture
@@ -26,3 +31,97 @@ def test_write_tag_value(temporary_file_path: str):
2631
parsed_document = tagvalue_parser.parse_from_file(temporary_file_path)
2732

2833
assert parsed_document == document
34+
35+
36+
def test_correct_order_of_elements():
37+
packages = [
38+
Package(name="Test Package A", spdx_id="SPDXRef-Package-A", download_location=""),
39+
Package(name="Test Package B", spdx_id="SPDXRef-Package-B", download_location=""),
40+
]
41+
files = [
42+
File(name="Test File A", spdx_id="SPDXRef-File-A", checksums=[checksum_fixture()]),
43+
File(name="Test File B", spdx_id="SPDXRef-File-B", checksums=[checksum_fixture()]),
44+
]
45+
snippets = [
46+
Snippet(spdx_id="SPDXRef-Snippet-A", file_spdx_id="DocumentRef-External:SPDXRef-File", byte_range=(1, 2)),
47+
Snippet(spdx_id="SPDXRef-Snippet-B", file_spdx_id="SPDXRef-File-A", byte_range=(1, 2)),
48+
Snippet(spdx_id="SPDXRef-Snippet-C", file_spdx_id="SPDXRef-File-B", byte_range=(3, 4)),
49+
]
50+
relationships = [Relationship("SPDXRef-Package-A", RelationshipType.CONTAINS, "SPDXRef-File-B")]
51+
document = document_fixture(
52+
files=files,
53+
packages=packages,
54+
snippets=snippets,
55+
relationships=relationships,
56+
annotations=[],
57+
extracted_licensing_info=[],
58+
)
59+
60+
mock: MagicMock = mock_open()
61+
with patch(f"{__name__}.open", mock, create=True):
62+
with open("foo", "w") as file:
63+
write_document(document, file)
64+
65+
mock.assert_called_once_with("foo", "w")
66+
handle = mock()
67+
handle.write.assert_has_calls(
68+
[
69+
call("## Document Information\n"),
70+
call("SPDXVersion: SPDX-2.3\n"),
71+
call("DataLicense: CC0-1.0\n"),
72+
call("SPDXID: SPDXRef-DOCUMENT\n"),
73+
call("DocumentName: documentName\n"),
74+
call("DocumentNamespace: https://some.namespace\n"),
75+
call("DocumentComment: documentComment\n"),
76+
call("\n## External Document References\n"),
77+
call(
78+
"ExternalDocumentRef: DocumentRef-external https://namespace.com "
79+
"SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"
80+
),
81+
call("\n"),
82+
call("## Creation Information\n"),
83+
call("LicenseListVersion: 3.19\n"),
84+
call("Creator: Person: creatorName ([email protected])\n"),
85+
call("Created: 2022-12-01T00:00:00Z\n"),
86+
call("CreatorComment: creatorComment\n"),
87+
call("\n"),
88+
call("## Snippet Information\n"),
89+
call("SnippetSPDXID: SPDXRef-Snippet-A\n"),
90+
call("SnippetFromFileSPDXID: DocumentRef-External:SPDXRef-File\n"),
91+
call("SnippetByteRange: 1:2\n"),
92+
call("\n"),
93+
call("## File Information\n"),
94+
call("FileName: Test File A\n"),
95+
call("SPDXID: SPDXRef-File-A\n"),
96+
call("FileChecksum: SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"),
97+
call("\n"),
98+
call("## Snippet Information\n"),
99+
call("SnippetSPDXID: SPDXRef-Snippet-B\n"),
100+
call("SnippetFromFileSPDXID: SPDXRef-File-A\n"),
101+
call("SnippetByteRange: 1:2\n"),
102+
call("\n"),
103+
call("## Package Information\n"),
104+
call("PackageName: Test Package A\n"),
105+
call("SPDXID: SPDXRef-Package-A\n"),
106+
call("PackageDownloadLocation: \n"),
107+
call("FilesAnalyzed: True\n"),
108+
call("\n"),
109+
call("## File Information\n"),
110+
call("FileName: Test File B\n"),
111+
call("SPDXID: SPDXRef-File-B\n"),
112+
call("FileChecksum: SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"),
113+
call("\n"),
114+
call("## Snippet Information\n"),
115+
call("SnippetSPDXID: SPDXRef-Snippet-C\n"),
116+
call("SnippetFromFileSPDXID: SPDXRef-File-B\n"),
117+
call("SnippetByteRange: 3:4\n"),
118+
call("\n"),
119+
call("## Package Information\n"),
120+
call("PackageName: Test Package B\n"),
121+
call("SPDXID: SPDXRef-Package-B\n"),
122+
call("PackageDownloadLocation: \n"),
123+
call("FilesAnalyzed: True\n"),
124+
call("\n"),
125+
call("\n"),
126+
]
127+
)

0 commit comments

Comments
 (0)