|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | import os |
| 6 | +from datetime import datetime |
6 | 7 | from unittest.mock import MagicMock, call, mock_open, patch |
7 | 8 |
|
8 | 9 | import pytest |
9 | 10 |
|
10 | | -from spdx_tools.spdx.model import File, Package, Relationship, RelationshipType, Snippet |
| 11 | +from spdx_tools.spdx.model import ( |
| 12 | + Actor, |
| 13 | + ActorType, |
| 14 | + Checksum, |
| 15 | + ChecksumAlgorithm, |
| 16 | + CreationInfo, |
| 17 | + Document, |
| 18 | + File, |
| 19 | + Package, |
| 20 | + Relationship, |
| 21 | + RelationshipType, |
| 22 | + Snippet, |
| 23 | +) |
11 | 24 | from spdx_tools.spdx.parser.tagvalue import tagvalue_parser |
12 | 25 | from spdx_tools.spdx.writer.tagvalue.tagvalue_writer import write_document, write_document_to_file |
13 | 26 | from tests.spdx.fixtures import checksum_fixture, document_fixture |
@@ -122,3 +135,87 @@ def test_correct_order_of_elements(): |
122 | 135 | call("\n"), |
123 | 136 | ] |
124 | 137 | ) |
| 138 | + |
| 139 | + |
| 140 | +def test_same_file_in_multiple_packages(): |
| 141 | + creation_info = CreationInfo( |
| 142 | + spdx_version="SPDX-2.3", |
| 143 | + spdx_id="SPDXRef-DOCUMENT", |
| 144 | + data_license="CC0-1.0", |
| 145 | + name="SPDX Lite Document", |
| 146 | + document_namespace="https://test.namespace.com", |
| 147 | + creators=[Actor(ActorType.PERSON, "John Doe")], |
| 148 | + created=datetime(2023, 3, 14, 8, 49), |
| 149 | + ) |
| 150 | + package_a = Package( |
| 151 | + name="Example package A", |
| 152 | + spdx_id="SPDXRef-Package-A", |
| 153 | + download_location="https://download.com", |
| 154 | + ) |
| 155 | + package_b = Package( |
| 156 | + name="Example package B", |
| 157 | + spdx_id="SPDXRef-Package-B", |
| 158 | + download_location="https://download.com", |
| 159 | + ) |
| 160 | + file = File( |
| 161 | + name="Example file", |
| 162 | + spdx_id="SPDXRef-File", |
| 163 | + checksums=[Checksum(ChecksumAlgorithm.SHA1, "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12")], |
| 164 | + ) |
| 165 | + |
| 166 | + relationships = [ |
| 167 | + Relationship("SPDXRef-DOCUMENT", RelationshipType.DESCRIBES, "SPDXRef-Package-A"), |
| 168 | + Relationship("SPDXRef-DOCUMENT", RelationshipType.DESCRIBES, "SPDXRef-Package-B"), |
| 169 | + Relationship("SPDXRef-Package-A", RelationshipType.CONTAINS, "SPDXRef-File"), |
| 170 | + Relationship("SPDXRef-Package-B", RelationshipType.CONTAINS, "SPDXRef-File"), |
| 171 | + ] |
| 172 | + document = Document( |
| 173 | + creation_info=creation_info, |
| 174 | + packages=[package_a, package_b], |
| 175 | + files=[file], |
| 176 | + relationships=relationships, |
| 177 | + ) |
| 178 | + mock: MagicMock = mock_open() |
| 179 | + with patch(f"{__name__}.open", mock, create=True): |
| 180 | + with open("foo", "w") as file: |
| 181 | + write_document(document, file) |
| 182 | + |
| 183 | + mock.assert_called_once_with("foo", "w") |
| 184 | + handle = mock() |
| 185 | + handle.write.assert_has_calls( |
| 186 | + [ |
| 187 | + call("## Document Information\n"), |
| 188 | + call("SPDXVersion: SPDX-2.3\n"), |
| 189 | + call("DataLicense: CC0-1.0\n"), |
| 190 | + call("SPDXID: SPDXRef-DOCUMENT\n"), |
| 191 | + call("DocumentName: SPDX Lite Document\n"), |
| 192 | + call("DocumentNamespace: https://test.namespace.com\n"), |
| 193 | + call("\n"), |
| 194 | + call("## Creation Information\n"), |
| 195 | + call("Creator: Person: John Doe\n"), |
| 196 | + call("Created: 2023-03-14T08:49:00Z\n"), |
| 197 | + call("\n"), |
| 198 | + call("## Package Information\n"), |
| 199 | + call("PackageName: Example package A\n"), |
| 200 | + call("SPDXID: SPDXRef-Package-A\n"), |
| 201 | + call("PackageDownloadLocation: https://download.com\n"), |
| 202 | + call("FilesAnalyzed: True\n"), |
| 203 | + call("\n"), |
| 204 | + call("## File Information\n"), |
| 205 | + call("FileName: Example file\n"), |
| 206 | + call("SPDXID: SPDXRef-File\n"), |
| 207 | + call("FileChecksum: SHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12\n"), |
| 208 | + call("\n"), |
| 209 | + call("## Package Information\n"), |
| 210 | + call("PackageName: Example package B\n"), |
| 211 | + call("SPDXID: SPDXRef-Package-B\n"), |
| 212 | + call("PackageDownloadLocation: https://download.com\n"), |
| 213 | + call("FilesAnalyzed: True\n"), |
| 214 | + call("\n"), |
| 215 | + call("## Relationships\n"), |
| 216 | + call("Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-Package-A\n"), |
| 217 | + call("Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-Package-B\n"), |
| 218 | + call("Relationship: SPDXRef-Package-B CONTAINS SPDXRef-File\n"), |
| 219 | + call("\n"), |
| 220 | + ] |
| 221 | + ) |
0 commit comments