|
| 1 | +# SPDX-FileCopyrightText: 2023 SPDX Contributors |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +import datetime |
| 5 | +from unittest.mock import mock_open, patch, call, MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from spdx.model.document import CreationInfo |
| 10 | +from tests.spdx.fixtures import creation_info_fixture, actor_fixture |
| 11 | + |
| 12 | +from spdx.writer.tagvalue.creation_info_writer import write_creation_info |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize("creation_info, expected_calls", |
| 16 | + [(creation_info_fixture(), [call("SPDXVersion: SPDX-2.3\n"), call("DataLicense: CC0-1.0\n"), |
| 17 | + call("SPDXID: SPDXRef-DOCUMENT\n"), |
| 18 | + call("DocumentName: documentName\n"), |
| 19 | + call("DocumentNamespace: https://some.namespace\n"), |
| 20 | + call("DocumentComment: documentComment\n"), |
| 21 | + call("\n## External Document References\n"), call( |
| 22 | + "ExternalDocumentRef: DocumentRef-external https://namespace.com SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"), |
| 23 | + call("\n"), call("## Creation Information\n"), |
| 24 | + call("LicenseListVersion: 3.19\n"), |
| 25 | + call("Creator: Person: creatorName (some@mail.com)\n"), |
| 26 | + call("Created: 2022-12-01T00:00:00Z\n"), |
| 27 | + call("CreatorComment: creatorComment\n")]), |
| 28 | + (CreationInfo(spdx_version="SPDX-2.3", spdx_id="SPDXRef-DOCUMENT", creators=[actor_fixture()], |
| 29 | + name="Test document", document_namespace="https://namespace.com", |
| 30 | + created=datetime.datetime(2022, 3, 10)), |
| 31 | + [call("SPDXVersion: SPDX-2.3\n"), call("DataLicense: CC0-1.0\n"), |
| 32 | + call("SPDXID: SPDXRef-DOCUMENT\n"), call("DocumentName: Test document\n"), |
| 33 | + call("DocumentNamespace: https://namespace.com\n"), call("\n"), |
| 34 | + call("## Creation Information\n"), call("Creator: Person: actorName (some@mail.com)\n"), |
| 35 | + call("Created: 2022-03-10T00:00:00Z\n")])]) |
| 36 | +def test_creation_info_writer(creation_info, expected_calls): |
| 37 | + mock: MagicMock = mock_open() |
| 38 | + with patch(f"{__name__}.open", mock, create=True): |
| 39 | + with open("foo", "w") as file: |
| 40 | + write_creation_info(creation_info, file) |
| 41 | + |
| 42 | + mock.assert_called_once_with("foo", "w") |
| 43 | + handle = mock() |
| 44 | + handle.write.assert_has_calls(expected_calls) |
0 commit comments