Tests/RunCMake/Instrumentation: Factor out common JSON logic

This commit is contained in:
Tyler Yankee
2025-07-28 11:29:36 -04:00
parent 4d21b9e6d8
commit 27bc7d5782
7 changed files with 149 additions and 142 deletions

View File

@@ -1,4 +1,4 @@
include(${CMAKE_CURRENT_LIST_DIR}/verify-snippet.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/json.cmake)
if (NOT IS_DIRECTORY "${v1}/data/content")
add_error("Custom content directory does not exist.")

View File

@@ -1,4 +1,4 @@
include(${CMAKE_CURRENT_LIST_DIR}/check-data-dir.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/json.cmake)
if (NOT IS_DIRECTORY "${v1}/data/content")
add_error("Custom content directory does not exist.")
@@ -10,27 +10,17 @@ if (NOT ${num} EQUAL 2)
add_error("Found ${num} custom content files, expected 2.")
endif()
macro(assert_key contents key expected)
string(JSON value ERROR_VARIABLE errors GET "${contents}" ${key})
if (errors)
add_error("Did not find expected key \"${key}\" in custom content.")
endif()
if (NOT ${value} MATCHES ${expected})
add_error("Unexpected data in custom content file:\nGot ${value}, Expected ${expected}.")
endif()
endmacro()
# Check contents of configureContent files
set(firstFile "")
foreach(content_file IN LISTS content_files)
read_json("${content_file}" contents)
assert_key("${contents}" myString "string")
assert_key("${contents}" myBool "OFF")
assert_key("${contents}" myInt "1")
assert_key("${contents}" myFloat "2.5")
assert_key("${contents}" myTrue "ON")
assert_key("${contents}" myList "[ \"a\", \"b\", \"c\" ]")
assert_key("${contents}" myObject "{.*\"key\".*:.*\"value\".*}")
json_assert_key("${content_file}" "${contents}" myString "string")
json_assert_key("${content_file}" "${contents}" myBool "OFF")
json_assert_key("${content_file}" "${contents}" myInt "1")
json_assert_key("${content_file}" "${contents}" myFloat "2.5")
json_assert_key("${content_file}" "${contents}" myTrue "ON")
json_assert_key("${content_file}" "${contents}" myList "[ \"a\", \"b\", \"c\" ]")
json_assert_key("${content_file}" "${contents}" myObject "{.*\"key\".*:.*\"value\".*}")
if (NOT firstFile)
set(firstFile "${content_file}")
endif()

View File

@@ -25,14 +25,14 @@ foreach(snippet IN LISTS snippets)
if (NOT target MATCHES NOTFOUND)
set(targets "main;lib;customTarget;TARGET_NAME")
if (NOT ${target} IN_LIST targets)
snippet_error("${snippet}" "Unexpected target: ${target}")
json_error("${snippet}" "Unexpected target: ${target}")
endif()
endif()
# Verify output
string(JSON result GET "${contents}" result)
if (NOT ${result} EQUAL 0)
snippet_error("${snippet}" "Compile command had non-0 result")
json_error("${snippet}" "Compile command had non-0 result")
endif()
# Verify contents of compile-* Snippets
@@ -41,10 +41,10 @@ foreach(snippet IN LISTS snippets)
string(JSON source GET "${contents}" source)
string(JSON language GET "${contents}" language)
if (NOT language MATCHES "C\\+\\+")
snippet_error("${snippet}" "Expected C++ compile language")
json_error("${snippet}" "Expected C++ compile language")
endif()
if (NOT source MATCHES "${target}.cxx$")
snippet_error("${snippet}" "Unexpected source file")
json_error("${snippet}" "Unexpected source file")
endif()
endif()
@@ -55,30 +55,30 @@ foreach(snippet IN LISTS snippets)
string(JSON targetLabels GET "${contents}" targetLabels)
if (target MATCHES "main")
if (NOT targetType MATCHES "EXECUTABLE")
snippet_error("${snippet}" "Expected EXECUTABLE, target type was ${targetType}")
json_error("${snippet}" "Expected EXECUTABLE, target type was ${targetType}")
endif()
string(JSON nlabels LENGTH "${targetLabels}")
if (NOT nlabels STREQUAL 2)
snippet_error("${snippet}" "Missing Target Labels for: ${target}")
json_error("${snippet}" "Missing Target Labels for: ${target}")
else()
string(JSON label1 GET "${contents}" targetLabels 0)
string(JSON label2 GET "${contents}" targetLabels 1)
if (NOT label1 MATCHES "label1" OR NOT label2 MATCHES "label2")
snippet_error("${snippet}" "Missing Target Labels for: ${target}")
json_error("${snippet}" "Missing Target Labels for: ${target}")
endif()
endif()
endif()
if (target MATCHES "lib")
if (NOT targetType MATCHES "STATIC_LIBRARY")
snippet_error("${snippet}" "Expected STATIC_LIBRARY, target type was ${targetType}")
json_error("${snippet}" "Expected STATIC_LIBRARY, target type was ${targetType}")
endif()
string(JSON nlabels LENGTH "${targetLabels}")
if (NOT nlabels STREQUAL 1)
snippet_error("${snippet}" "Missing Target Labels for: ${target}")
json_error("${snippet}" "Missing Target Labels for: ${target}")
else()
string(JSON label ERROR_VARIABLE noLabels GET "${contents}" targetLabels 0)
if (NOT label MATCHES "label3")
snippet_error("${snippet}" "Missing Target Labels for: ${target}")
json_error("${snippet}" "Missing Target Labels for: ${target}")
endif()
endif()
endif()
@@ -88,7 +88,7 @@ foreach(snippet IN LISTS snippets)
if (filename MATCHES "^custom-")
string(JSON outputs GET "${contents}" outputs)
if (NOT output1 MATCHES "output1" OR NOT output2 MATCHES "output2")
snippet_error("${snippet}" "Custom command missing outputs")
json_error("${snippet}" "Custom command missing outputs")
endif()
endif()
@@ -96,7 +96,7 @@ foreach(snippet IN LISTS snippets)
if (filename MATCHES "^test-")
string(JSON testName GET "${contents}" testName)
if (NOT testName STREQUAL "test")
snippet_error("${snippet}" "Unexpected testName: ${testName}")
json_error("${snippet}" "Unexpected testName: ${testName}")
endif()
endif()
@@ -104,7 +104,7 @@ foreach(snippet IN LISTS snippets)
if (filename MATCHES "^test|^compile|^link")
string(JSON config GET "${contents}" config)
if (NOT config STREQUAL "Debug")
snippet_error(${snippet} "Unexpected config: ${config}")
json_error(${snippet} "Unexpected config: ${config}")
endif()
endif()
@@ -112,7 +112,7 @@ foreach(snippet IN LISTS snippets)
if (filename MATCHES "^cmakeBuild|^ctest")
string(JSON command GET "${contents}" command)
if (NOT command MATCHES "Debug")
snippet_error(${snippet} "Command value missing passed arguments")
json_error(${snippet} "Command value missing passed arguments")
endif()
endif()

View File

@@ -1,18 +1,7 @@
include(${CMAKE_CURRENT_LIST_DIR}/json.cmake)
function(check_generated_json n)
set(expected_file "${RunCMake_TEST_BINARY_DIR}/query/query-${n}.json")
set(generated_file "${v1}/query/generated/query-${n}.json")
read_json("${expected_file}" expected)
read_json("${generated_file}" generated)
string(JSON equal EQUAL ${expected} ${generated})
if (NOT equal)
set(RunCMake_TEST_FAILED
"Generated JSON ${generated}\nNot equal to expected ${expected}"
)
endif()
return(PROPAGATE RunCMake_TEST_FAILED)
endfunction()
foreach(n IN LISTS generated_queries)
check_generated_json(${n})
set(expected_file "${RunCMake_TEST_BINARY_DIR}/query/query-${n}.json")
set(generated_file "${v1}/query/generated/query-${n}.json")
json_equals(${expected_file} ${generated_file})
endforeach()

View File

@@ -20,22 +20,10 @@ function(add_error error)
return(PROPAGATE ERROR_MESSAGE)
endfunction()
function(has_key_index key json)
cmake_parse_arguments(ARG "UNEXPECTED" "" "" ${ARGN})
unset(missingKey)
string(JSON ${key} ERROR_VARIABLE missingKey GET "${json}" ${key})
if (NOT ARG_UNEXPECTED AND NOT missingKey MATCHES NOTFOUND)
add_error("\nKey \"${key}\" not in index:\n${json}")
elseif(ARG_UNEXPECTED AND missingKey MATCHES NOTFOUND)
add_error("\nUnexpected key \"${key}\" in index:\n${json}")
endif()
return(PROPAGATE ERROR_MESSAGE ${key})
endfunction()
has_key_index(version "${contents}")
has_key_index(buildDir "${contents}")
has_key_index(dataDir "${contents}")
has_key_index(snippets "${contents}")
json_has_key("${index}" "${contents}" version)
json_has_key("${index}" "${contents}" buildDir)
json_has_key("${index}" "${contents}" dataDir)
json_has_key("${index}" "${contents}" snippets)
if (NOT version EQUAL 1)
add_error("Version must be 1, got: ${version}")
@@ -52,26 +40,26 @@ foreach(i RANGE ${length})
verify_snippet(${dataDir}/${filename} "${snippet_contents}")
endforeach()
has_key_index(staticSystemInformation "${contents}" ${hasStaticInfo})
json_has_key("${index}" "${contents}" staticSystemInformation ${hasStaticInfo})
if (NOT hasStaticInfo STREQUAL UNEXPECTED)
has_key_index(OSName "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(OSPlatform "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(OSRelease "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(OSVersion "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(familyId "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(hostname "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(is64Bits "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(modelId "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(numberOfLogicalCPU "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(numberOfPhysicalCPU "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(processorAPICID "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(processorCacheSize "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(processorClockFrequency "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(processorName "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(totalPhysicalMemory "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(totalVirtualMemory "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(vendorID "${staticSystemInformation}" ${hasStaticInfo})
has_key_index(vendorString "${staticSystemInformation}" ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" OSName ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" OSPlatform ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" OSRelease ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" OSVersion ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" familyId ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" hostname ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" is64Bits ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" modelId ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" numberOfLogicalCPU ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" numberOfPhysicalCPU ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" processorAPICID ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" processorCacheSize ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" processorClockFrequency ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" processorName ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" totalPhysicalMemory ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" totalVirtualMemory ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" vendorID ${hasStaticInfo})
json_has_key("${index}" "${staticSystemInformation}" vendorString ${hasStaticInfo})
endif()
get_filename_component(dataDir ${index} DIRECTORY)

View File

@@ -1,4 +1,69 @@
# Read the JSON `filename` into `outvar`.
function(read_json filename outvar)
file(READ "${filename}" ${outvar})
return(PROPAGATE ${outvar})
endfunction()
# Utility for error messages.
function(add_error error)
string(APPEND RunCMake_TEST_FAILED " ${error}\n")
string(APPEND ERROR_MESSAGE " ${error}\n")
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
# Utility for JSON-specific error messages.
function(json_error file error)
add_error("Error in JSON file ${file}:\n${error}")
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
# Check if the JSON string `json` has `key` when it's not
# UNEXPECTED. If so, return it in `key`.
function(json_has_key file json key)
cmake_parse_arguments(ARG "UNEXPECTED" "" "" ${ARGN})
unset(missingKey)
string(JSON ${key} ERROR_VARIABLE missingKey GET "${json}" ${key})
if (NOT ARG_UNEXPECTED AND NOT missingKey MATCHES NOTFOUND)
json_error("${file}" "Missing key \'${key}\':\n${json}")
elseif (ARG_UNEXPECTED AND missingKey MATCHES NOTFOUND)
json_error("${file}" "\nUnexpected key \'${key}\':\n${json}")
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE ${key})
endfunction()
# Check if the JSON string `json` does not have `key`.
function(json_missing_key file json key)
string(JSON data ERROR_VARIABLE missingKey GET "${json}" ${key})
if (missingKey MATCHES NOTFOUND)
json_error("${file}" "Has unexpected ${key}.")
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
# Check if the JSON string `json` has `key` and its value matches `expected`.
function(json_assert_key file json key expected)
string(JSON data ERROR_VARIABLE missingKey GET "${json}" ${key})
if (NOT missingKey MATCHES NOTFOUND)
json_error("${file}" "Missing ${key}.")
endif()
if (NOT ${data} MATCHES ${expected})
json_error(
"${file}"
"Unexpected data in custom content file:\nGot ${data}, Expected ${expected}."
)
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
# Check if the two given JSON files are equal.
function(json_equals expected_file actual_file)
read_json("${expected_file}" expected_contents)
read_json("${actual_file}" actual_contents)
string(JSON equal EQUAL ${expected_contents} ${actual_contents})
if (NOT equal)
add_error(
"JSON ${expected_file} does not equal ${actual_file}."
)
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()

View File

@@ -1,78 +1,53 @@
# Performs generic (non-project specific) validation of v1 Snippet File Contents
function(add_error error)
string(APPEND RunCMake_TEST_FAILED " ${error}\n")
string(APPEND ERROR_MESSAGE " ${error}\n")
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(snippet_error snippet error)
add_error("Error in snippet file ${snippet}:\n${error}")
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(has_key snippet json key)
string(JSON data ERROR_VARIABLE missingKey GET "${json}" ${key})
if (NOT missingKey MATCHES NOTFOUND)
snippet_error("${snippet}" "Missing ${key}")
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(has_not_key snippet json key)
string(JSON data ERROR_VARIABLE missingKey GET "${json}" ${key})
if (missingKey MATCHES NOTFOUND)
snippet_error("${snippet}" "Has unexpected ${key}")
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
include(${CMAKE_CURRENT_LIST_DIR}/json.cmake)
function(snippet_has_fields snippet contents)
get_filename_component(filename "${snippet}" NAME)
has_key("${snippet}" "${contents}" role)
has_key("${snippet}" "${contents}" result)
has_key("${snippet}" "${contents}" workingDir)
json_has_key("${snippet}" "${contents}" role)
json_has_key("${snippet}" "${contents}" result)
json_has_key("${snippet}" "${contents}" workingDir)
if (NOT filename MATCHES "^build-*")
has_key("${snippet}" "${contents}" command)
json_has_key("${snippet}" "${contents}" command)
endif()
if (filename MATCHES "^link-*")
has_key("${snippet}" "${contents}" target)
has_key("${snippet}" "${contents}" outputs)
has_key("${snippet}" "${contents}" outputSizes)
has_key("${snippet}" "${contents}" targetType)
has_key("${snippet}" "${contents}" targetLabels)
has_key("${snippet}" "${contents}" config)
json_has_key("${snippet}" "${contents}" target)
json_has_key("${snippet}" "${contents}" outputs)
json_has_key("${snippet}" "${contents}" outputSizes)
json_has_key("${snippet}" "${contents}" targetType)
json_has_key("${snippet}" "${contents}" targetLabels)
json_has_key("${snippet}" "${contents}" config)
elseif (filename MATCHES "^compile-*")
has_key("${snippet}" "${contents}" target)
has_key("${snippet}" "${contents}" outputs)
has_key("${snippet}" "${contents}" outputSizes)
has_key("${snippet}" "${contents}" source)
has_key("${snippet}" "${contents}" language)
has_key("${snippet}" "${contents}" config)
json_has_key("${snippet}" "${contents}" target)
json_has_key("${snippet}" "${contents}" outputs)
json_has_key("${snippet}" "${contents}" outputSizes)
json_has_key("${snippet}" "${contents}" source)
json_has_key("${snippet}" "${contents}" language)
json_has_key("${snippet}" "${contents}" config)
elseif (filename MATCHES "^custom-*")
has_key("${snippet}" "${contents}" outputs)
has_key("${snippet}" "${contents}" outputSizes)
json_has_key("${snippet}" "${contents}" outputs)
json_has_key("${snippet}" "${contents}" outputSizes)
elseif (filename MATCHES "^test-*")
has_key("${snippet}" "${contents}" testName)
has_key("${snippet}" "${contents}" config)
json_has_key("${snippet}" "${contents}" testName)
json_has_key("${snippet}" "${contents}" config)
endif()
if(ARGS_DYNAMIC_QUERY)
has_key("${snippet}" "${contents}" dynamicSystemInformation)
json_has_key("${snippet}" "${contents}" dynamicSystemInformation)
string(JSON dynamicSystemInfo ERROR_VARIABLE noInfo GET "${contents}" dynamicSystemInformation)
if (noInfo MATCHES NOTFOUND)
has_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
has_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
has_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
has_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
json_has_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
json_has_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
json_has_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
json_has_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
endif()
else()
has_not_key("${snippet}" "${contents}" dynamicSystemInformation)
json_missing_key("${snippet}" "${contents}" dynamicSystemInformation)
string(JSON dynamicSystemInfo ERROR_VARIABLE noInfo GET "${contents}" dynamicSystemInformation)
if (noInfo MATCHES NOTFOUND)
has_not_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
has_not_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
has_not_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
has_not_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
json_missing_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
json_missing_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
json_missing_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
json_missing_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
endif()
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
@@ -85,7 +60,7 @@ function(snippet_valid_timing contents)
snippet_error("${snippet}" "Negative time start: ${start}")
endif()
if (duration LESS 0)
snippet_error("${snippet}" "Negative duration: ${end}")
json_error("${snippet}" "Negative duration: ${end}")
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
@@ -95,12 +70,12 @@ function(verify_snippet snippet contents)
snippet_valid_timing("${contents}")
string(JSON version GET "${contents}" version)
if (NOT version EQUAL 1)
snippet_error("${snippet}" "Version must be 1, got: ${version}")
json_error("${snippet}" "Version must be 1, got: ${version}")
endif()
string(JSON role GET "${contents}" role)
get_filename_component(filename "${snippet}" NAME)
if (NOT filename MATCHES "^${role}-")
snippet_error("${snippet}" "Role \"${role}\" doesn't match snippet filename")
json_error("${snippet}" "Role \"${role}\" doesn't match snippet filename")
endif()
string(JSON outputs ERROR_VARIABLE noOutputs GET "${contents}" outputs)
if (NOT outputs MATCHES NOTFOUND)
@@ -108,7 +83,7 @@ function(verify_snippet snippet contents)
list(LENGTH outputs outputsLen)
list(LENGTH outputSizes outputSizesLen)
if (outputSizes MATCHES NOTFOUND OR NOT outputsLen EQUAL outputSizesLen)
snippet_error("${snippet}" "outputs and outputSizes do not match")
json_error("${snippet}" "outputs and outputSizes do not match")
endif()
endif()
return(PROPAGATE ERROR_MESSAGE RunCMake_TEST_FAILED role)