Skip to content

Commit c3f78a1

Browse files
[issue-403] restructure spdx version validation
Signed-off-by: Armin Tänzer <armin.taenzer@tngtech.com>
1 parent 5f61b05 commit c3f78a1

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

src/validation/creation_info_validator.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,11 @@
1919
from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType
2020

2121

22-
def validate_creation_info(creation_info: CreationInfo, spdx_version: str) -> List[ValidationMessage]:
22+
def validate_creation_info(creation_info: CreationInfo) -> List[ValidationMessage]:
2323
validation_messages: List[ValidationMessage] = []
2424

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

27-
if not re.match(r"^SPDX-\d+.\d+$", creation_info.spdx_version):
28-
validation_messages.append(
29-
ValidationMessage(
30-
f'spdx_version must be of the form "SPDX-[major].[minor]" but is: {creation_info.spdx_version}',
31-
context
32-
)
33-
)
34-
elif spdx_version != creation_info.spdx_version:
35-
validation_messages.append(
36-
ValidationMessage(f"provided SPDX version {spdx_version} does not match "
37-
f"the document's SPDX version {creation_info.spdx_version}", context)
38-
)
39-
4027
if creation_info.spdx_id != "SPDXRef-DOCUMENT":
4128
validation_messages.append(
4229
ValidationMessage(

src/validation/document_validator.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
11-
11+
import re
1212
from typing import List
1313

1414
from src.model.document import Document
@@ -27,10 +27,32 @@
2727
def validate_full_spdx_document(document: Document, spdx_version: str = None) -> List[ValidationMessage]:
2828
validation_messages: List[ValidationMessage] = []
2929

30+
# SPDX version validation has to happen here because subsequent validators rely on it
31+
document_version: str = document.creation_info.spdx_version
32+
context = ValidationContext(spdx_id=document.creation_info.spdx_id, element_type=SpdxElementType.DOCUMENT)
3033
if not spdx_version:
31-
spdx_version = document.creation_info.spdx_version
34+
spdx_version = document_version
35+
36+
if not re.match(r"^SPDX-\d+.\d+$", document_version):
37+
validation_messages.append(
38+
ValidationMessage(
39+
f'the document\'s spdx_version must be of the form "SPDX-[major].[minor]" but is: {document_version}',
40+
context
41+
)
42+
)
43+
elif spdx_version != document_version:
44+
validation_messages.append(
45+
ValidationMessage(f"provided SPDX version {spdx_version} does not match "
46+
f"the document's SPDX version {document_version}", context)
47+
)
48+
49+
if validation_messages:
50+
validation_messages.append(ValidationMessage("There are issues concerning the SPDX version of the document. "
51+
"As subsequent validation relies on the correct version, "
52+
"the validation process has been cancelled.", context))
53+
return validation_messages
3254

33-
validation_messages.extend(validate_creation_info(document.creation_info, spdx_version))
55+
validation_messages.extend(validate_creation_info(document.creation_info))
3456
validation_messages.extend(validate_packages(document.packages, document))
3557
validation_messages.extend(validate_files(document.files, document))
3658
validation_messages.extend(validate_snippets(document.snippets, document))

tests/validation/test_creation_info_validator.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ def test_valid_creation_info():
2727

2828
@pytest.mark.parametrize \
2929
("creation_info_input, spdx_id, expected_message",
30-
[(creation_info_fixture(spdx_version="version-2.3"), "SPDXRef-DOCUMENT",
31-
'spdx_version must be of the form "SPDX-[major].[minor]" but is: version-2.3'),
32-
(creation_info_fixture(spdx_id="SPDXRef-doc"), "SPDXRef-doc",
30+
[(creation_info_fixture(spdx_id="SPDXRef-doc"), "SPDXRef-doc",
3331
'spdx_id must be "SPDXRef-DOCUMENT", but is: SPDXRef-doc'),
3432
(creation_info_fixture(data_license="MIT"), "SPDXRef-DOCUMENT",
3533
'data_license must be "CC0-1.0", but is: MIT'),

tests/validation/test_document_validator.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12-
from typing import List
12+
from typing import List, Optional
1313

14+
import pytest
15+
16+
from src.model.document import Document, CreationInfo
1417
from src.validation.document_validator import validate_full_spdx_document
15-
from src.validation.validation_message import ValidationMessage
16-
from tests.fixtures import document_fixture
18+
from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType
19+
from tests.fixtures import document_fixture, creation_info_fixture
1720

1821

1922
def test_valid_document():
@@ -22,4 +25,34 @@ def test_valid_document():
2225

2326
assert validation_messages == []
2427

25-
# TODO: https://github.com/spdx/tools-python/issues/375
28+
29+
@pytest.mark.parametrize("creation_info, version_input, expected_message",
30+
[(creation_info_fixture(spdx_version="SPDX-2.3"), "SPDX-2.3", None),
31+
(creation_info_fixture(spdx_version="SPDX-2.3"), None, None),
32+
(creation_info_fixture(spdx_version="SPDX-2.3"), "SPDX-2.2",
33+
"provided SPDX version SPDX-2.2 does not match the document's SPDX version SPDX-2.3"),
34+
(creation_info_fixture(spdx_version="SPDX-2.3"), "SPDX2.3",
35+
"provided SPDX version SPDX2.3 does not match the document's SPDX version SPDX-2.3"),
36+
(creation_info_fixture(spdx_version="SPDX2.3"), "SPDX-2.3",
37+
'the document\'s spdx_version must be of the form "SPDX-[major].[minor]" but is: SPDX2.3'),
38+
(creation_info_fixture(spdx_version="SPDX2.3"), None,
39+
'the document\'s spdx_version must be of the form "SPDX-[major].[minor]" but is: SPDX2.3'),
40+
(creation_info_fixture(spdx_version="SPDX2.3"), "SPDX2.3",
41+
'the document\'s spdx_version must be of the form "SPDX-[major].[minor]" but is: SPDX2.3'),
42+
])
43+
def test_spdx_version_handling(creation_info: CreationInfo, version_input: str, expected_message: Optional[str]):
44+
document: Document = document_fixture(creation_info=creation_info)
45+
validation_messages: List[ValidationMessage] = validate_full_spdx_document(document, version_input)
46+
47+
context = ValidationContext(spdx_id=creation_info.spdx_id, element_type=SpdxElementType.DOCUMENT)
48+
expected: List[ValidationMessage] = []
49+
50+
if expected_message:
51+
expected.append(ValidationMessage(expected_message, context))
52+
expected.append(ValidationMessage("There are issues concerning the SPDX version of the document. "
53+
"As subsequent validation relies on the correct version, "
54+
"the validation process has been cancelled.", context))
55+
56+
assert validation_messages == expected
57+
58+
# TODO: https://github.com/spdx/tools-python/issues/375

0 commit comments

Comments
 (0)