Skip to content

Commit ba81e62

Browse files
kartbencarlescufi
authored andcommitted
tests: cmake: add tests to validate actual SPDX files contents
The initial test was really just a basic smoke test ensuring that `west spdx` was outputting valid (syntactically correct) SPDX files. This tests a lot more thoroughly by checking the files contain what they are supposed to in terms of captured licenses/copyright information, relationship between sources and build artifacts, etc. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 8a0b4d7 commit ba81e62

File tree

4 files changed

+681
-1
lines changed

4 files changed

+681
-1
lines changed

‎tests/application_development/software_bill_of_materials/CMakeLists.txt‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.20.0)
55
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
66

77
project(sbom_spdx)
8-
target_sources(app PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
8+
target_sources(app PRIVATE src/main.c)
99

1010
enable_testing()
1111
include(CTest)
@@ -41,4 +41,19 @@ foreach(spdx_version ${SPDX_VERSIONS_TO_TEST})
4141
PROPERTIES DEPENDS spdx_gen_${version_suffix}
4242
)
4343
endforeach()
44+
45+
add_test(
46+
NAME spdx_content_validate_${version_suffix}
47+
COMMAND ${PYTHON_EXECUTABLE} -m pytest
48+
${CMAKE_CURRENT_SOURCE_DIR}/verify_spdx_content.py
49+
--build-dir ${CMAKE_BINARY_DIR}
50+
--spdx-version ${spdx_version}
51+
--source-dir ${CMAKE_CURRENT_SOURCE_DIR}
52+
-v
53+
)
54+
55+
set_tests_properties(
56+
spdx_content_validate_${version_suffix}
57+
PROPERTIES DEPENDS spdx_gen_${version_suffix}
58+
)
4459
endforeach()
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Pytest configuration for SPDX content validation tests."""
5+
6+
import os
7+
8+
import pytest
9+
from packaging import version
10+
from spdx_tools.spdx.parser.parse_anything import parse_file
11+
12+
13+
def pytest_addoption(parser):
14+
"""Add command-line options for pytest."""
15+
parser.addoption(
16+
"--build-dir",
17+
action="store",
18+
required=True,
19+
help="Path to the build directory containing SPDX files",
20+
)
21+
parser.addoption(
22+
"--spdx-version",
23+
action="store",
24+
required=True,
25+
help="Expected SPDX version (e.g., '2.2' or '2.3')",
26+
)
27+
parser.addoption(
28+
"--source-dir",
29+
action="store",
30+
required=True,
31+
help="Path to the test source directory containing src/main.c",
32+
)
33+
34+
35+
def pytest_configure(config):
36+
"""Register custom markers."""
37+
config.addinivalue_line(
38+
"markers",
39+
"min_spdx_version(version): skip test if SPDX version is less than specified",
40+
)
41+
42+
43+
def pytest_runtest_setup(item):
44+
"""Skip tests based on min_spdx_version marker."""
45+
marker = item.get_closest_marker("min_spdx_version")
46+
if marker is not None:
47+
min_version = version.parse(marker.args[0])
48+
current_version = version.parse(item.config.getoption("--spdx-version"))
49+
if current_version < min_version:
50+
pytest.skip(f"Requires SPDX version >= {min_version}, got {current_version}")
51+
52+
53+
@pytest.fixture(scope="session")
54+
def build_dir(request):
55+
"""Fixture providing the build directory path."""
56+
return request.config.getoption("--build-dir")
57+
58+
59+
@pytest.fixture(scope="session")
60+
def spdx_version(request):
61+
"""Fixture providing the expected SPDX version."""
62+
return request.config.getoption("--spdx-version")
63+
64+
65+
@pytest.fixture(scope="session")
66+
def source_dir(request):
67+
"""Fixture providing the test source directory path."""
68+
return request.config.getoption("--source-dir")
69+
70+
71+
@pytest.fixture(scope="session")
72+
def spdx_dir(build_dir):
73+
"""Fixture providing the SPDX directory path."""
74+
return os.path.join(build_dir, "spdx")
75+
76+
77+
@pytest.fixture(scope="session")
78+
def app_doc(spdx_dir):
79+
"""Fixture providing the parsed app.spdx document."""
80+
return parse_file(os.path.join(spdx_dir, "app.spdx"))
81+
82+
83+
@pytest.fixture(scope="session")
84+
def zephyr_doc(spdx_dir):
85+
"""Fixture providing the parsed zephyr.spdx document."""
86+
return parse_file(os.path.join(spdx_dir, "zephyr.spdx"))
87+
88+
89+
@pytest.fixture(scope="session")
90+
def build_doc(spdx_dir):
91+
"""Fixture providing the parsed build.spdx document."""
92+
return parse_file(os.path.join(spdx_dir, "build.spdx"))
93+
94+
95+
@pytest.fixture(scope="session")
96+
def modules_doc(spdx_dir):
97+
"""Fixture providing the parsed modules-deps.spdx document."""
98+
return parse_file(os.path.join(spdx_dir, "modules-deps.spdx"))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
3+
* Copyright (c) 2026 The Linux Foundation
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <zephyr/kernel.h>
9+
10+
int main(void)
11+
{
12+
return 0;
13+
}

0 commit comments

Comments
 (0)