SBOM: Use new string(JSON ... PARTIAL_EQUALS) for unit tests

This commit is contained in:
Taylor Sasser
2026-04-23 15:29:18 -04:00
parent 7ca0f82499
commit 72661612f4
10 changed files with 80 additions and 173 deletions
@@ -1,2 +1,2 @@
file(READ "${RunCMake_TEST_BINARY_DIR}/interface_targets.spdx.json" content)
file(READ "${RunCMake_TEST_BINARY_DIR}/test_targets.spdx.json" content)
include(${CMAKE_CURRENT_LIST_DIR}/../Sbom/MissingPackageNamespace-install-check.cmake)
@@ -1,7 +1,7 @@
include(${CMAKE_CURRENT_LIST_DIR}/../Sbom/MissingPackageNamespace.cmake)
export(
SBOM interface_targets
SBOM test_targets
EXPORT test_targets
VERSION 1.0.2
)
@@ -29,7 +29,7 @@ set(SPDX_DOCUMENT_EXPECTED [=[
set(APPLICATION_EXPECTED [=[
{
"spdxId" : "application#Package",
"spdxId" : "urn:application#Package",
"name" : "application",
"software_primaryPurpose" : "application",
"type" : "software_Package"
@@ -38,7 +38,7 @@ set(APPLICATION_EXPECTED [=[
set(DEPENDENCY_EXPECTED [=[
{
"spdxId" : "bar:bar#Package",
"spdxId" : "urn:bar:bar#Package",
"name" : "bar:bar",
"originatedBy" :
[
@@ -74,7 +74,7 @@ expect_object("${CREATION_INFO}" CREATION_INFO_EXPECTED)
string(JSON SPDX_DOCUMENT GET "${content}" "@graph" "1")
expect_object("${SPDX_DOCUMENT}" SPDX_DOCUMENT_EXPECTED)
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement" "0")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element" "0")
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element")
string(JSON LINKED_LIBRARIES GET "${content}" "@graph" "2")
expect_object("${LINKED_LIBRARIES}" BUILD_LINKED_LIBRARIES_EXPECTED)
+13 -107
View File
@@ -30,109 +30,6 @@ macro(_json_fail_at type actual expected path_segments)
set(RunCMake_TEST_FAILED "${msg}" PARENT_SCOPE)
endmacro()
function(_json_verify_subset out_success actual_node expected_node path_list)
string(JSON expected_type ERROR_VARIABLE expected_is_primitive TYPE "${expected_node}")
if(expected_is_primitive OR NOT (expected_type STREQUAL "OBJECT" OR expected_type STREQUAL "ARRAY"))
if("${actual_node}" STREQUAL "${expected_node}")
set(${out_success} TRUE PARENT_SCOPE)
else()
_json_fail_at("Value Mismatch" "${actual_node}" "${expected_node}" "${path_list}")
set(${out_success} FALSE PARENT_SCOPE)
endif()
return()
endif()
string(JSON actual_type ERROR_VARIABLE actual_err TYPE "${actual_node}")
if(actual_err)
_json_fail_at("Invalid JSON" "${actual_node}" "Valid JSON Structure" "${path_list}")
set(${out_success} FALSE PARENT_SCOPE)
return()
endif()
if(NOT actual_type STREQUAL expected_type)
_json_fail_at("Type Mismatch" "${actual_type}" "${expected_type}" "${path_list}")
set(${out_success} FALSE PARENT_SCOPE)
return()
endif()
if(expected_type STREQUAL "OBJECT")
string(JSON exp_len LENGTH "${expected_node}")
math(EXPR exp_last "${exp_len}-1")
foreach(idx RANGE ${exp_last})
string(JSON key MEMBER "${expected_node}" "${idx}")
string(JSON act_child ERROR_VARIABLE err GET "${actual_node}" "${key}")
if(err)
_json_fail_at("Missing Key" "<missing>" "${key}" "${path_list}")
set(${out_success} FALSE PARENT_SCOPE)
return()
endif()
string(JSON exp_child GET "${expected_node}" "${key}")
set(next_path ${path_list})
list(APPEND next_path "${key}")
_json_verify_subset(sub_ok "${act_child}" "${exp_child}" "${next_path}")
if(NOT sub_ok)
set(${out_success} FALSE PARENT_SCOPE)
return()
endif()
endforeach()
set(${out_success} TRUE PARENT_SCOPE)
return()
elseif(expected_type STREQUAL "ARRAY")
string(JSON exp_len LENGTH "${expected_node}")
string(JSON act_len LENGTH "${actual_node}")
if(act_len LESS exp_len)
_json_fail_at("Array Length" "${act_len}" ">= ${exp_len}" "${path_list}")
set(${out_success} FALSE PARENT_SCOPE)
return()
endif()
math(EXPR exp_last "${exp_len}-1")
math(EXPR act_last "${act_len}-1")
set(used_indices "")
foreach(exp_idx RANGE ${exp_last})
string(JSON exp_item GET "${expected_node}" "${exp_idx}")
set(found_match FALSE)
foreach(act_idx RANGE ${act_last})
if("${act_idx}" IN_LIST used_indices)
continue()
endif()
string(JSON act_item GET "${actual_node}" "${act_idx}")
unset(RunCMake_TEST_FAILED)
set(next_path ${path_list})
list(APPEND next_path "${act_idx}")
_json_verify_subset(is_match "${act_item}" "${exp_item}" "${next_path}")
if(is_match)
set(found_match TRUE)
list(APPEND used_indices "${act_idx}")
break()
endif()
endforeach()
if(NOT found_match)
_json_fail_at("Array Element Missing" "<no match found>" "Element [${exp_idx}]" "${path_list}")
set(${out_success} FALSE PARENT_SCOPE)
return()
endif()
endforeach()
set(${out_success} TRUE PARENT_SCOPE)
return()
endif()
endfunction()
function(expect_object actual_json expected_var)
if(NOT DEFINED "${expected_var}")
@@ -150,11 +47,20 @@ function(expect_object actual_json expected_var)
set(actual_node "${actual_json}")
endif()
unset(RunCMake_TEST_FAILED)
_json_verify_subset(success "${actual_node}" "${expected_node}" "${path_segments}")
string(JSON actual_type TYPE "${actual_node}")
string(JSON expected_type TYPE "${expected_node}")
if(NOT success)
set(RunCMake_TEST_FAILED "${RunCMake_TEST_FAILED}" PARENT_SCOPE)
set(pattern "${expected_node}")
set(fail_type "Partial Equality Mismatch")
if(actual_type STREQUAL "ARRAY" AND expected_type STREQUAL "OBJECT")
set(pattern "[ ${expected_node} ]")
set(fail_type "Object Not Found in Array")
endif()
string(JSON is_equal PARTIAL_EQUAL "${pattern}" "${actual_node}")
if(NOT is_equal)
_json_fail_at("${fail_type}" "${actual_node}" "${expected_node}" "${path_segments}")
endif()
endfunction()
@@ -31,7 +31,7 @@ set(APPLICATION_EXPECTED [=[
{
"spdxId" : "urn:interface#Package",
"name" : "interface",
"software_primaryPurpose" : "application",
"software_primaryPurpose" : "library",
"type" : "software_Package"
}
]=])
@@ -74,7 +74,7 @@ expect_object("${CREATION_INFO}" CREATION_INFO_EXPECTED)
string(JSON SPDX_DOCUMENT GET "${content}" "@graph" "1")
expect_object("${SPDX_DOCUMENT}" SPDX_DOCUMENT_EXPECTED)
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement" "0")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element" "0")
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element")
string(JSON LINKED_LIBRARIES GET "${content}" "@graph" "2")
expect_object("${LINKED_LIBRARIES}" BUILD_LINKED_LIBRARIES_EXPECTED)
@@ -38,16 +38,16 @@ set(APPLICATION_EXPECTED [=[
set(DEPENDENCY_EXPECTED [=[
{
"spdxId" : "urn:bar:bar#Package",
"name" : "bar:bar",
"spdxId" : "urn:baz:baz#Package",
"name" : "baz:baz",
"originatedBy" :
[
{
"name" : "bar",
"name" : "baz",
"type" : "Organization"
}
],
"software_packageVersion" : "1.3.5",
"software_packageVersion" : "1.8.5",
"type" : "software_Package"
}
]=])
@@ -61,7 +61,7 @@ set(BUILD_LINKED_LIBRARIES_EXPECTED [=[
"spdxId" : "urn:Static#Relationship",
"to" :
[
"urn:bar:bar#Package"
"urn:baz:baz#Package"
],
"type" : "Relationship"
}
@@ -74,7 +74,7 @@ expect_object("${CREATION_INFO}" CREATION_INFO_EXPECTED)
string(JSON SPDX_DOCUMENT GET "${content}" "@graph" "1")
expect_object("${SPDX_DOCUMENT}" SPDX_DOCUMENT_EXPECTED)
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement" "0")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element" "0")
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element")
string(JSON LINKED_LIBRARIES GET "${content}" "@graph" "2")
expect_object("${LINKED_LIBRARIES}" BUILD_LINKED_LIBRARIES_EXPECTED)
@@ -31,7 +31,7 @@ set(APPLICATION_EXPECTED [=[
{
"spdxId" : "urn:test#Package",
"name" : "test",
"software_primaryPurpose" : "application",
"software_primaryPurpose" : "library",
"type" : "software_Package"
}
]=])
@@ -43,4 +43,4 @@ expect_object("${CREATION_INFO}" CREATION_INFO_EXPECTED)
string(JSON SPDX_DOCUMENT GET "${content}" "@graph" "1")
expect_object("${SPDX_DOCUMENT}" SPDX_DOCUMENT_EXPECTED)
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement" "0")
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement")
@@ -2,53 +2,44 @@ include(${CMAKE_CURRENT_LIST_DIR}/Assertions.cmake)
set(CREATION_INFO_EXPECTED [=[
{
"@id" : "_:Build#CreationInfo",
"comment" : "This SBOM was generated from the CMakeLists.txt File",
"created" : "2026-02-17T10:34:34Z",
"createdBy" :
"@id": "_:Build#CreationInfo",
"comment": "This SBOM was generated from the CMakeLists.txt File",
"createdBy":
[
"https://gitlab.kitware.com/cmake/cmake"
],
"specVersion" : "3.0.1",
"type" : "CreationInfo"
"specVersion": "3.0.1",
"type": "CreationInfo"
}
]=])
set(SPDX_DOCUMENT_EXPECTED [=[
{
"name" : "application_targets",
"name" : "dog",
"profileConformance" :
[
"core",
"software"
],
"creationInfo" : "_:Build#CreationInfo",
"spdxId" : "urn:application_targets#SPDXDocument",
"spdxId" : "urn:dog#SPDXDocument",
"type" : "SpdxDocument"
}
]=])
set(APPLICATION_EXPECTED [=[
{
"spdxId" : "application#Package",
"name" : "application",
"software_primaryPurpose" : "application",
"spdxId" : "urn:canine#Package",
"name" : "canine",
"software_primaryPurpose" : "library",
"type" : "software_Package"
}
]=])
set(DEPENDENCY_EXPECTED [=[
{
"spdxId" : "bar:bar#Package",
"name" : "bar:bar",
"originatedBy" :
[
{
"name" : "bar",
"type" : "Organization"
}
],
"software_packageVersion" : "1.3.5",
"spdxId" : "urn:mammal#Package",
"name" : "mammal",
"type" : "software_Package"
}
]=])
@@ -56,13 +47,13 @@ set(DEPENDENCY_EXPECTED [=[
set(BUILD_LINKED_LIBRARIES_EXPECTED [=[
{
"creationInfo" : "_:Build#CreationInfo",
"description" : "Linked Libraries",
"from" : "urn:application#Package",
"description" : "Required Build-Time Libraries",
"from" : "urn:canine#Package",
"relationshipType" : "dependsOn",
"spdxId" : "urn:Static#Relationship",
"spdxId" : "urn:Shared#Relationship",
"to" :
[
"urn:bar:bar#Package"
"urn:mammal#Package"
],
"type" : "Relationship"
}
@@ -75,7 +66,7 @@ expect_object("${CREATION_INFO}" CREATION_INFO_EXPECTED)
string(JSON SPDX_DOCUMENT GET "${content}" "@graph" "1")
expect_object("${SPDX_DOCUMENT}" SPDX_DOCUMENT_EXPECTED)
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement" "0")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element" "0")
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element")
string(JSON LINKED_LIBRARIES GET "${content}" "@graph" "2")
expect_object("${LINKED_LIBRARIES}" BUILD_LINKED_LIBRARIES_EXPECTED)
@@ -46,9 +46,19 @@ set(FOO_SPDX_DOCUMENT [=[
set(FOO_LIBB [=[
{
"creationInfo" : "_:Build#CreationInfo",
"name" : "libd",
"name" : "libb",
"software_primaryPurpose" : "library",
"spdxId" : "urn:libd#Package",
"spdxId" : "urn:libb#Package",
"type" : "software_Package"
}
]=])
set(BAR_LIBB [=[
{
"creationInfo" : "_:Build#CreationInfo",
"name" : "libb",
"software_primaryPurpose" : "library",
"spdxId" : "urn:libb#Package",
"type" : "software_Package"
}
]=])
@@ -67,7 +77,7 @@ set(BAR_LIBD [=[
{
"spdxId" : "urn:libd#Package",
"name" : "libd",
"software_primaryPurpose" : "LIBRARY",
"software_primaryPurpose" : "library",
"type" : "software_Package"
}
]=])
@@ -127,13 +137,13 @@ expect_value("${FOO_CONTENT}" "https://spdx.org/rdf/3.0.1/spdx-context.jsonld" "
string(JSON FOO_CREATION_INFO GET "${FOO_CONTENT}" "@graph" "0")
string(JSON FOO_SPDX_DOCUMENT GET "${FOO_CONTENT}" "@graph" "1")
expect_object("${FOO_SPDX_DOCUMENT}" FOO_SPDX_DOCUMENT)
expect_object("${FOO_SPDX_DOCUMENT}" FOO_LIBB "rootElement" "0")
expect_object("${FOO_SPDX_DOCUMENT}" FOO_LIBB "rootElement")
expect_value("${BAR_CONTENT}" "https://spdx.org/rdf/3.0.1/spdx-context.jsonld" "@context")
string(JSON BAR_CREATION_INFO GET "${BAR_CONTENT}" "@graph" "0")
string(JSON BAR_SPDX_DOCUMENT GET "${BAR_CONTENT}" "@graph" "1")
expect_object("${BAR_SPDX_DOCUMENT}" BAR_SPDX_DOCUMENT)
expect_object("${BAR_SPDX_DOCUMENT}" BAR_LIBC "rootElement" "0")
expect_object("${BAR_SPDX_DOCUMENT}" BAR_LIBD "rootElement" "1")
expect_object("${BAR_SPDX_DOCUMENT}" BAR_DEPENDENCY_TEST "element" "0")
expect_object("${BAR_SPDX_DOCUMENT}" BAR_DEPENDENCY_FOO "element" "1")
expect_object("${BAR_SPDX_DOCUMENT}" BAR_LIBC "rootElement")
expect_object("${BAR_SPDX_DOCUMENT}" BAR_LIBD "rootElement")
expect_object("${BAR_SPDX_DOCUMENT}" BAR_DEPENDENCY_TEST "element")
expect_object("${BAR_SPDX_DOCUMENT}" BAR_DEPENDENCY_FOO "element")
@@ -15,39 +15,39 @@ set(CREATION_INFO_EXPECTED [=[
set(SPDX_DOCUMENT_EXPECTED [=[
{
"name" : "application_targets",
"name" : "shared_targets",
"profileConformance" :
[
"core",
"software"
],
"creationInfo" : "_:Build#CreationInfo",
"spdxId" : "urn:application_targets#SPDXDocument",
"spdxId" : "urn:shared_targets#SPDXDocument",
"type" : "SpdxDocument"
}
]=])
set(APPLICATION_EXPECTED [=[
{
"spdxId" : "application#Package",
"name" : "application",
"software_primaryPurpose" : "application",
"spdxId" : "urn:shared#Package",
"name" : "shared",
"software_primaryPurpose" : "library",
"type" : "software_Package"
}
]=])
set(DEPENDENCY_EXPECTED [=[
{
"spdxId" : "bar:bar#Package",
"name" : "bar:bar",
"spdxId" : "urn:foo:foo#Package",
"name" : "foo:foo",
"originatedBy" :
[
{
"name" : "bar",
"name" : "foo",
"type" : "Organization"
}
],
"software_packageVersion" : "1.3.5",
"software_packageVersion" : "1.2.3",
"type" : "software_Package"
}
]=])
@@ -56,12 +56,12 @@ set(BUILD_LINKED_LIBRARIES_EXPECTED [=[
{
"creationInfo" : "_:Build#CreationInfo",
"description" : "Linked Libraries",
"from" : "urn:application#Package",
"from" : "urn:shared#Package",
"relationshipType" : "dependsOn",
"spdxId" : "urn:Static#Relationship",
"to" :
[
"urn:bar:bar#Package"
"urn:foo:foo#Package"
],
"type" : "Relationship"
}
@@ -74,7 +74,7 @@ expect_object("${CREATION_INFO}" CREATION_INFO_EXPECTED)
string(JSON SPDX_DOCUMENT GET "${content}" "@graph" "1")
expect_object("${SPDX_DOCUMENT}" SPDX_DOCUMENT_EXPECTED)
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement" "0")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element" "0")
expect_object("${SPDX_DOCUMENT}" APPLICATION_EXPECTED "rootElement")
expect_object("${SPDX_DOCUMENT}" DEPENDENCY_EXPECTED "element")
string(JSON LINKED_LIBRARIES GET "${content}" "@graph" "2")
expect_object("${LINKED_LIBRARIES}" BUILD_LINKED_LIBRARIES_EXPECTED)