Skip to content
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
4 changes: 4 additions & 0 deletions src/spdx/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
DOCUMENT_SPDX_ID = "SPDXRef-DOCUMENT"
6 changes: 4 additions & 2 deletions src/spdx/parser/rdf/creation_info_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rdflib.exceptions import UniquenessError
from rdflib.term import URIRef

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.datetime_conversions import datetime_from_str
from spdx.model.document import CreationInfo
from spdx.model.external_document_ref import ExternalDocumentRef
Expand Down Expand Up @@ -98,15 +99,16 @@ def parse_namespace_and_spdx_id(graph: Graph) -> (str, str):
if "#" not in subject:
logging.error(
"No '#' found in the URI of SpdxDocument, "
"the URI for the SpdxDocument should be the namespace appended by '#SPDXRef-DOCUMENT."
f"the URI for the SpdxDocument should be the namespace appended by '#{DOCUMENT_SPDX_ID}."
)
sys.exit(1)

namespace, spdx_id = urldefrag(str(subject))

if not namespace:
logging.error(
"No namespace found, the URI for the SpdxDocument should be the namespace appended by '#SPDXRef-DOCUMENT."
f"No namespace found, the URI for the SpdxDocument should be the namespace appended by "
f"'#{DOCUMENT_SPDX_ID}."
)
sys.exit(1)

Expand Down
5 changes: 3 additions & 2 deletions src/spdx/validation/creation_info_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import List

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.document import CreationInfo
from spdx.validation.actor_validator import validate_actors
from spdx.validation.external_document_ref_validator import validate_external_document_refs
Expand All @@ -16,9 +17,9 @@ def validate_creation_info(creation_info: CreationInfo, spdx_version: str) -> Li

context = ValidationContext(spdx_id=creation_info.spdx_id, element_type=SpdxElementType.DOCUMENT)

if creation_info.spdx_id != "SPDXRef-DOCUMENT":
if creation_info.spdx_id != DOCUMENT_SPDX_ID:
validation_messages.append(
ValidationMessage(f'spdx_id must be "SPDXRef-DOCUMENT", but is: {creation_info.spdx_id}', context)
ValidationMessage(f"spdx_id must be {DOCUMENT_SPDX_ID}, but is: {creation_info.spdx_id}", context)
)

if creation_info.data_license != "CC0-1.0":
Expand Down
5 changes: 3 additions & 2 deletions tests/spdx/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from license_expression import get_spdx_licensing

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.actor import Actor, ActorType
from spdx.model.annotation import Annotation, AnnotationType
from spdx.model.checksum import Checksum, ChecksumAlgorithm
Expand Down Expand Up @@ -46,7 +47,7 @@ def package_verification_code_fixture(

def creation_info_fixture(
spdx_version="SPDX-2.3",
spdx_id="SPDXRef-DOCUMENT",
spdx_id=DOCUMENT_SPDX_ID,
name="documentName",
document_namespace="https://some.namespace",
creators=None,
Expand Down Expand Up @@ -264,7 +265,7 @@ def extracted_licensing_info_fixture(


def relationship_fixture(
spdx_element_id="SPDXRef-DOCUMENT",
spdx_element_id=DOCUMENT_SPDX_ID,
relationship_type=RelationshipType.DESCRIBES,
related_spdx_element_id="SPDXRef-File",
comment="relationshipComment",
Expand Down
9 changes: 5 additions & 4 deletions tests/spdx/parser/jsonlikedict/test_annotation_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.actor import Actor, ActorType
from spdx.model.annotation import Annotation, AnnotationType
from spdx.parser.error import SPDXParsingError
Expand All @@ -21,19 +22,19 @@ def test_parse_annotation():
"comment": "Document level annotation",
}

annotation = annotation_parser.parse_annotation(annotation_dict, spdx_id="SPDXRef-DOCUMENT")
annotation = annotation_parser.parse_annotation(annotation_dict, spdx_id=DOCUMENT_SPDX_ID)

assert annotation.annotator == Actor(ActorType.PERSON, name="Jane Doe")
assert annotation.annotation_type == AnnotationType.OTHER
assert annotation.annotation_date == datetime.datetime(2010, 1, 29, 18, 30, 22)
assert annotation.annotation_comment == "Document level annotation"
assert annotation.spdx_id == "SPDXRef-DOCUMENT"
assert annotation.spdx_id == DOCUMENT_SPDX_ID


def test_parse_all_annotations():
annotation_parser = AnnotationParser()
doc_dict = {
"SPDXID": "SPDXRef-DOCUMENT",
"SPDXID": DOCUMENT_SPDX_ID,
"packages": [
{
"SPDXID": "SPDXRef-Package",
Expand Down Expand Up @@ -87,7 +88,7 @@ def test_parse_all_annotations():
annotations,
[
Annotation(
spdx_id="SPDXRef-DOCUMENT",
spdx_id=DOCUMENT_SPDX_ID,
annotation_type=AnnotationType.REVIEW,
annotator=Actor(actor_type=ActorType.PERSON, name="Jane Doe", email=None),
annotation_date=datetime.datetime(2010, 1, 29, 18, 30, 22),
Expand Down
9 changes: 5 additions & 4 deletions tests/spdx/parser/jsonlikedict/test_creation_info_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.actor import Actor, ActorType
from spdx.model.checksum import Checksum, ChecksumAlgorithm
from spdx.model.external_document_ref import ExternalDocumentRef
Expand All @@ -18,7 +19,7 @@ def test_parse_creation_info():
creation_info_parser = CreationInfoParser()
doc_dict = {
"spdxVersion": "2.3",
"SPDXID": "SPDXRef-DOCUMENT",
"SPDXID": DOCUMENT_SPDX_ID,
"name": "Example Document",
"dataLicense": "CC0-1.0",
"documentNamespace": "namespace",
Expand All @@ -39,7 +40,7 @@ def test_parse_creation_info():
creation_info = creation_info_parser.parse_creation_info(doc_dict)

assert creation_info.spdx_version == "2.3"
assert creation_info.spdx_id == "SPDXRef-DOCUMENT"
assert creation_info.spdx_id == DOCUMENT_SPDX_ID
assert creation_info.name == "Example Document"
assert creation_info.document_namespace == "namespace"
assert creation_info.created == datetime(2010, 1, 29, 18, 30, 22)
Expand All @@ -65,7 +66,7 @@ def test_parse_creation_info():
"incomplete_dict,expected_message",
[
(
{"spdxVersion": "2.3", "SPDXID": "SPDXRef-DOCUMENT", "name": "Example Document"},
{"spdxVersion": "2.3", "SPDXID": DOCUMENT_SPDX_ID, "name": "Example Document"},
["Error while parsing document Example Document: ['CreationInfo does not exist.']"],
),
(
Expand Down Expand Up @@ -98,7 +99,7 @@ def test_parse_invalid_creation_info():
creation_info_parser = CreationInfoParser()
doc_dict = {
"spdxVersion": "2.3",
"SPDXID": "SPDXRef-DOCUMENT",
"SPDXID": DOCUMENT_SPDX_ID,
"name": "Example Document",
"creationInfo": {
"created": "2010-01-29T18:30:22Z",
Expand Down
31 changes: 16 additions & 15 deletions tests/spdx/parser/jsonlikedict/test_relationship_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.relationship import Relationship, RelationshipType
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.parser.error import SPDXParsingError
Expand All @@ -15,7 +16,7 @@ def test_parse_relationship():
relationship_parser = RelationshipParser()

relationship_dict = {
"spdxElementId": "SPDXRef-DOCUMENT",
"spdxElementId": DOCUMENT_SPDX_ID,
"relationshipType": "CONTAINS",
"relatedSpdxElement": "NOASSERTION",
"comment": "Comment.",
Expand All @@ -24,15 +25,15 @@ def test_parse_relationship():
relationship = relationship_parser.parse_relationship(relationship_dict)

assert relationship.relationship_type == RelationshipType.CONTAINS
assert relationship.spdx_element_id == "SPDXRef-DOCUMENT"
assert relationship.spdx_element_id == DOCUMENT_SPDX_ID
assert relationship.related_spdx_element_id == SpdxNoAssertion()
assert relationship.comment == "Comment."


def test_parse_incomplete_relationship():
relationship_parser = RelationshipParser()
relationship_dict = {
"spdxElementId": "SPDXRef-DOCUMENT",
"spdxElementId": DOCUMENT_SPDX_ID,
"relatedSpdxElement": "SPDXRef-Package",
"comment": "Comment.",
}
Expand Down Expand Up @@ -62,12 +63,12 @@ def test_parse_document_describes():
relationship_parser = RelationshipParser()

document_dict = {
"SPDXID": "SPDXRef-DOCUMENT",
"SPDXID": DOCUMENT_SPDX_ID,
"documentDescribes": ["SPDXRef-Package", "SPDXRef-File", "SPDXRef-Snippet"],
}

relationships = relationship_parser.parse_document_describes(
doc_spdx_id="SPDXRef-DOCUMENT",
doc_spdx_id=DOCUMENT_SPDX_ID,
described_spdx_ids=document_dict.get("documentDescribes"),
existing_relationships=[],
)
Expand All @@ -76,9 +77,9 @@ def test_parse_document_describes():
TestCase().assertCountEqual(
relationships,
[
Relationship("SPDXRef-DOCUMENT", RelationshipType.DESCRIBES, "SPDXRef-Package"),
Relationship("SPDXRef-DOCUMENT", RelationshipType.DESCRIBES, "SPDXRef-File"),
Relationship("SPDXRef-DOCUMENT", RelationshipType.DESCRIBES, "SPDXRef-Snippet"),
Relationship(DOCUMENT_SPDX_ID, RelationshipType.DESCRIBES, "SPDXRef-Package"),
Relationship(DOCUMENT_SPDX_ID, RelationshipType.DESCRIBES, "SPDXRef-File"),
Relationship(DOCUMENT_SPDX_ID, RelationshipType.DESCRIBES, "SPDXRef-Snippet"),
],
)

Expand All @@ -90,14 +91,14 @@ def test_parse_document_describes():
["SPDXRef-Package", "SPDXRef-File"],
[
{
"spdxElementId": "SPDXRef-DOCUMENT",
"spdxElementId": DOCUMENT_SPDX_ID,
"relatedSpdxElement": "SPDXRef-Package",
"relationshipType": "DESCRIBES",
"comment": "This relationship has a comment.",
},
{
"spdxElementId": "SPDXRef-File",
"relatedSpdxElement": "SPDXRef-DOCUMENT",
"relatedSpdxElement": DOCUMENT_SPDX_ID,
"relationshipType": "DESCRIBED_BY",
"comment": "This relationship has a comment.",
},
Expand All @@ -106,11 +107,11 @@ def test_parse_document_describes():
Relationship(
related_spdx_element_id="SPDXRef-Package",
relationship_type=RelationshipType.DESCRIBES,
spdx_element_id="SPDXRef-DOCUMENT",
spdx_element_id=DOCUMENT_SPDX_ID,
comment="This relationship has a comment.",
),
Relationship(
related_spdx_element_id="SPDXRef-DOCUMENT",
related_spdx_element_id=DOCUMENT_SPDX_ID,
relationship_type=RelationshipType.DESCRIBED_BY,
spdx_element_id="SPDXRef-File",
comment="This relationship has a comment.",
Expand All @@ -124,12 +125,12 @@ def test_parse_document_describes():
Relationship(
related_spdx_element_id="SPDXRef-Package",
relationship_type=RelationshipType.DESCRIBES,
spdx_element_id="SPDXRef-DOCUMENT",
spdx_element_id=DOCUMENT_SPDX_ID,
),
Relationship(
related_spdx_element_id="SPDXRef-File",
relationship_type=RelationshipType.DESCRIBES,
spdx_element_id="SPDXRef-DOCUMENT",
spdx_element_id=DOCUMENT_SPDX_ID,
),
],
),
Expand All @@ -140,7 +141,7 @@ def test_parse_document_describes_without_duplicating_relationships(
):
relationship_parser = RelationshipParser()
document_dict = {
"SPDXID": "SPDXRef-DOCUMENT",
"SPDXID": DOCUMENT_SPDX_ID,
"documentDescribes": document_describes,
"relationships": relationships,
}
Expand Down
7 changes: 4 additions & 3 deletions tests/spdx/parser/rdf/test_creation_info_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rdflib import RDF, Graph, URIRef
from rdflib.term import Node

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.actor import Actor, ActorType
from spdx.model.checksum import Checksum, ChecksumAlgorithm
from spdx.model.version import Version
Expand All @@ -24,7 +25,7 @@ def test_parse_creation_info():
graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml"))

creation_info, _ = parse_creation_info(graph)
assert creation_info.spdx_id == "SPDXRef-DOCUMENT"
assert creation_info.spdx_id == DOCUMENT_SPDX_ID
assert creation_info.spdx_version == "SPDX-2.3"
assert creation_info.name == "documentName"
assert creation_info.document_namespace == "https://some.namespace"
Expand Down Expand Up @@ -53,7 +54,7 @@ def test_parse_namespace_and_spdx_id():
r"No '#' found in the URI of SpdxDocument",
),
([(URIRef(""), RDF.type, URIRef(""))], r"No SpdxDocument found, can't parse rdf file."),
([(URIRef("#SPDXRef-DOCUMENT"), RDF.type, SPDX_NAMESPACE.SpdxDocument)], "No namespace found"),
([(URIRef(f"#{DOCUMENT_SPDX_ID}"), RDF.type, SPDX_NAMESPACE.SpdxDocument)], "No namespace found"),
(
[
(URIRef("docNamespace1"), RDF.type, SPDX_NAMESPACE.SpdxDocument),
Expand All @@ -80,7 +81,7 @@ def test_parse_external_document_refs():
graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml"))
doc_namespace = "https://some.namespace"
external_doc_ref_node = graph.value(
subject=URIRef(f"{doc_namespace}#SPDXRef-DOCUMENT"), predicate=SPDX_NAMESPACE.externalDocumentRef
subject=URIRef(f"{doc_namespace}#{DOCUMENT_SPDX_ID}"), predicate=SPDX_NAMESPACE.externalDocumentRef
)
assert isinstance(external_doc_ref_node, URIRef)

Expand Down
5 changes: 3 additions & 2 deletions tests/spdx/parser/rdf/test_relationship_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
from rdflib import RDF, Graph, URIRef

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.relationship import RelationshipType
from spdx.parser.rdf.relationship_parser import parse_implicit_relationship, parse_relationship
from spdx.rdfschema.namespace import SPDX_NAMESPACE
Expand All @@ -20,7 +21,7 @@ def test_relationship_parser():

relationship = parse_relationship(relationship_node, graph, parent_node, doc_namespace)

assert relationship.spdx_element_id == "SPDXRef-DOCUMENT"
assert relationship.spdx_element_id == DOCUMENT_SPDX_ID
assert relationship.relationship_type == RelationshipType.DESCRIBES
assert relationship.related_spdx_element_id == "SPDXRef-File"
assert relationship.comment == "relationshipComment"
Expand All @@ -32,7 +33,7 @@ def test_relationship_parser():
(
SPDX_NAMESPACE.SpdxDocument,
SPDX_NAMESPACE.describesPackage,
"SPDXRef-DOCUMENT",
DOCUMENT_SPDX_ID,
RelationshipType.DESCRIBES,
"SPDXRef-Package",
),
Expand Down
7 changes: 4 additions & 3 deletions tests/spdx/parser/tagvalue/test_annotation_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.annotation import AnnotationType
from spdx.parser.error import SPDXParsingError
from spdx.parser.tagvalue.parser import Parser
Expand All @@ -19,7 +20,7 @@ def test_parse_annotation():
"AnnotationDate: 2010-01-29T18:30:22Z",
"AnnotationComment: <text>Document level annotation</text>",
"AnnotationType: OTHER",
"SPDXREF: SPDXRef-DOCUMENT",
f"SPDXREF: {DOCUMENT_SPDX_ID}",
]
)
document = parser.parse("\n".join([DOCUMENT_STR, annotation_str]))
Expand All @@ -30,7 +31,7 @@ def test_parse_annotation():
assert annotation.annotation_date == datetime(2010, 1, 29, 18, 30, 22)
assert annotation.annotation_comment == "Document level annotation"
assert annotation.annotation_type == AnnotationType.OTHER
assert annotation.spdx_id == "SPDXRef-DOCUMENT"
assert annotation.spdx_id == DOCUMENT_SPDX_ID


@pytest.mark.parametrize(
Expand All @@ -51,7 +52,7 @@ def test_parse_annotation():
(
"Annotator: Jane Doe()\nAnnotationDate: 201001-29T18:30:22Z\n"
"AnnotationComment: <text>Document level annotation</text>\nAnnotationType: OTHER\n"
"SPDXREF: SPDXRef-DOCUMENT",
f"SPDXREF: {DOCUMENT_SPDX_ID}",
"Error while parsing Annotation: ['Error while parsing Annotator: Token did "
"not match specified grammar rule. Line: 1', 'Error while parsing "
"AnnotationDate: Token did not match specified grammar rule. Line: 2']",
Expand Down
Loading