Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cmake labels (take-over #2690) #2832

Open
wants to merge 5 commits into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 29 additions & 10 deletions extras/CatchAddTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function(catch_discover_tests_impl)
endif()

execute_process(
COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" ${spec} --list-tests --verbosity quiet
COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" ${spec} --list-tests --reporter JSON
OUTPUT_VARIABLE output
RESULT_VARIABLE result
WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
Expand All @@ -74,12 +74,6 @@ function(catch_discover_tests_impl)
)
endif()

# Make sure to escape ; (semicolons) in test names first, because
# that'd break the foreach loop for "Parse output" later and create
# wrongly splitted and thus failing test cases (false positives)
string(REPLACE ";" "\;" output "${output}")
string(REPLACE "\n" ";" output "${output}")

# Prepare reporter
if(reporter)
set(reporter_arg "--reporter ${reporter}")
Expand Down Expand Up @@ -121,15 +115,24 @@ function(catch_discover_tests_impl)
endforeach()
endif()

string(JSON listings GET "${output}" "listings")
string(JSON tests GET "${listings}" "tests")
string(JSON tests_length LENGTH "${tests}")
# CMake foreach loop is inclusive
math(EXPR test_end "${tests_length} - 1")
# Accumulate test names
set(tests_list)
# Parse output
foreach(line ${output})
set(test "${line}")
foreach(index RANGE "${test_end}")
string(JSON test_spec GET "${tests}" "${index}")
string(JSON test GET "${test_spec}" "name")
# Escape characters in test case names that would be parsed by Catch2
# Note that the \ escaping must happen FIRST! Do not change the order.
set(test_name "${test}")
foreach(char \\ , [ ])
string(REPLACE ${char} "\\${char}" test_name "${test_name}")
endforeach(char)
list(APPEND tests_list "${test_name}")
# ...add output dir
if(output_dir)
string(REGEX REPLACE "[^A-Za-z0-9_]" "_" test_name_clean "${test_name}")
Expand Down Expand Up @@ -161,11 +164,27 @@ function(catch_discover_tests_impl)
endif()

list(APPEND tests "${prefix}${test}${suffix}")

string(JSON tags GET "${test_spec}" "tags")
string(JSON tags_length LENGTH "${tags}")

if("${tags_length}" GREATER 0)
math(EXPR tag_end "${tags_length} - 1")

foreach(tag_index RANGE "${tag_end}")
string(JSON tag GET "${tags}" "${tag_index}")
add_command(set_tests_properties
"${prefix}${test}${suffix}"
PROPERTIES
LABELS "${tag}"
)
endforeach()
endif()
endforeach()

# Create a list of all discovered tests, which users may use to e.g. set
# properties on the tests
add_command(set ${_TEST_LIST} ${tests})
add_command(set ${_TEST_LIST} ${tests_list})

# Write CTest script
file(WRITE "${_CTEST_FILE}" "${script}")
Expand Down