Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tag value writer #517

Merged
merged 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/spdx/writer/tagvalue/creation_info_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def write_creation_info(creation_info: CreationInfo, text_output: TextIO):
write_separator(text_output)

text_output.write("## Creation Information\n")
write_value("LicenseListVersion", str(creation_info.license_list_version), text_output)
write_value("LicenseListVersion", creation_info.license_list_version, text_output)
for creator in creation_info.creators:
write_value("Creator", creator.to_serialized_string(), text_output)
write_value("Created", datetime_to_iso_string(creation_info.created), text_output)
Expand Down
44 changes: 44 additions & 0 deletions tests/spdx/writer/tagvalue/test_creation_info_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2023 SPDX Contributors
#
# SPDX-License-Identifier: Apache-2.0
import datetime
from unittest.mock import mock_open, patch, call, MagicMock

import pytest

from spdx.model.document import CreationInfo
from tests.spdx.fixtures import creation_info_fixture, actor_fixture

from spdx.writer.tagvalue.creation_info_writer import write_creation_info


@pytest.mark.parametrize("creation_info, expected_calls",
[(creation_info_fixture(), [call("SPDXVersion: SPDX-2.3\n"), call("DataLicense: CC0-1.0\n"),
call("SPDXID: SPDXRef-DOCUMENT\n"),
call("DocumentName: documentName\n"),
call("DocumentNamespace: https://some.namespace\n"),
call("DocumentComment: documentComment\n"),
call("\n## External Document References\n"), call(
"ExternalDocumentRef: DocumentRef-external https://namespace.com SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"),
call("\n"), call("## Creation Information\n"),
call("LicenseListVersion: 3.19\n"),
call("Creator: Person: creatorName ([email protected])\n"),
call("Created: 2022-12-01T00:00:00Z\n"),
call("CreatorComment: creatorComment\n")]),
(CreationInfo(spdx_version="SPDX-2.3", spdx_id="SPDXRef-DOCUMENT", creators=[actor_fixture()],
name="Test document", document_namespace="https://namespace.com",
created=datetime.datetime(2022, 3, 10)),
[call("SPDXVersion: SPDX-2.3\n"), call("DataLicense: CC0-1.0\n"),
call("SPDXID: SPDXRef-DOCUMENT\n"), call("DocumentName: Test document\n"),
call("DocumentNamespace: https://namespace.com\n"), call("\n"),
call("## Creation Information\n"), call("Creator: Person: actorName ([email protected])\n"),
call("Created: 2022-03-10T00:00:00Z\n")])])
def test_creation_info_writer(creation_info, expected_calls):
mock: MagicMock = mock_open()
with patch(f"{__name__}.open", mock, create=True):
with open("foo", "w") as file:
write_creation_info(creation_info, file)

mock.assert_called_once_with("foo", "w")
handle = mock()
handle.write.assert_has_calls(expected_calls)
72 changes: 36 additions & 36 deletions tests/spdx/writer/tagvalue/test_package_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# 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 unittest.mock import patch, mock_open, call
from unittest.mock import patch, mock_open, call, MagicMock

from tests.spdx.fixtures import package_fixture
from spdx.writer.tagvalue.package_writer import write_package
Expand All @@ -17,40 +17,40 @@
def test_package_writer():
package = package_fixture()

m = mock_open()
with patch('{}.open'.format(__name__), m, create=True):
with open('foo', 'w') as h:
write_package(package, h)
mock: MagicMock = mock_open()
with patch(f"{__name__}.open", mock, create=True):
with open("foo", "w") as file:
write_package(package, file)

m.assert_called_once_with('foo', 'w')
handle = m()
mock.assert_called_once_with("foo", "w")
handle = mock()
handle.write.assert_has_calls(
[call('## Package Information\n'),
call('PackageName: packageName\n'),
call('SPDXID: SPDXRef-Package\n'),
call('PackageVersion: 12.2\n'),
call('PackageFileName: ./packageFileName\n'),
call('PackageSupplier: Person: supplierName ([email protected])\n'),
call('PackageOriginator: Person: originatorName ([email protected])\n'),
call('PackageDownloadLocation: https://download.com\n'),
call('FilesAnalyzed: True\n'),
call('PackageVerificationCode: 85ed0817af83a24ad8da68c2b5094de69833983c (excludes: ./exclude.py)\n'),
call('PackageChecksum: SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n'),
call('PackageHomePage: https://homepage.com\n'),
call('PackageSourceInfo: sourceInfo\n'),
call('PackageLicenseConcluded: MIT AND GPL-2.0-only\n'),
call('PackageLicenseInfoFromFiles: MIT\n'),
call('PackageLicenseInfoFromFiles: GPL-2.0-only\n'),
call('PackageLicenseDeclared: MIT AND GPL-2.0-only\n'),
call('PackageLicenseComments: packageLicenseComment\n'),
call('PackageCopyrightText: packageCopyrightText\n'),
call('PackageSummary: packageSummary\n'),
call('PackageDescription: packageDescription\n'),
call('PackageComment: packageComment\n'),
call('ExternalRef: PACKAGE-MANAGER maven-central org.apache.tomcat:tomcat:9.0.0.M4\n'),
call('ExternalRefComment: externalPackageRefComment\n'),
call('PackageAttributionText: packageAttributionText\n'),
call('PrimaryPackagePurpose: SOURCE\n'),
call('ReleaseDate: 2022-12-01T00:00:00Z\n'),
call('BuiltDate: 2022-12-02T00:00:00Z\n'),
call('ValidUntilDate: 2022-12-03T00:00:00Z\n')])
[call("## Package Information\n"),
call("PackageName: packageName\n"),
call("SPDXID: SPDXRef-Package\n"),
call("PackageVersion: 12.2\n"),
call("PackageFileName: ./packageFileName\n"),
call("PackageSupplier: Person: supplierName ([email protected])\n"),
call("PackageOriginator: Person: originatorName ([email protected])\n"),
call("PackageDownloadLocation: https://download.com\n"),
call("FilesAnalyzed: True\n"),
call("PackageVerificationCode: 85ed0817af83a24ad8da68c2b5094de69833983c (excludes: ./exclude.py)\n"),
call("PackageChecksum: SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"),
call("PackageHomePage: https://homepage.com\n"),
call("PackageSourceInfo: sourceInfo\n"),
call("PackageLicenseConcluded: MIT AND GPL-2.0-only\n"),
call("PackageLicenseInfoFromFiles: MIT\n"),
call("PackageLicenseInfoFromFiles: GPL-2.0-only\n"),
call("PackageLicenseDeclared: MIT AND GPL-2.0-only\n"),
call("PackageLicenseComments: packageLicenseComment\n"),
call("PackageCopyrightText: packageCopyrightText\n"),
call("PackageSummary: packageSummary\n"),
call("PackageDescription: packageDescription\n"),
call("PackageComment: packageComment\n"),
call("ExternalRef: PACKAGE-MANAGER maven-central org.apache.tomcat:tomcat:9.0.0.M4\n"),
call("ExternalRefComment: externalPackageRefComment\n"),
call("PackageAttributionText: packageAttributionText\n"),
call("PrimaryPackagePurpose: SOURCE\n"),
call("ReleaseDate: 2022-12-01T00:00:00Z\n"),
call("BuiltDate: 2022-12-02T00:00:00Z\n"),
call("ValidUntilDate: 2022-12-03T00:00:00Z\n")])