| 1 |
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
# file Copyright.txt or https://cmake.org/licensing for details. |
| 3 |
|
| 4 |
function(add_command NAME) |
| 5 |
set(_args "") |
| 6 |
# use ARGV* instead of ARGN, because ARGN splits arrays into multiple arguments |
| 7 |
math(EXPR _last_arg ${ARGC}-1) |
| 8 |
foreach(_n RANGE 1 ${_last_arg}) |
| 9 |
set(_arg "${ARGV${_n}}") |
| 10 |
if(_arg MATCHES "[^-./:a-zA-Z0-9_]") |
| 11 |
set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument |
| 12 |
else() |
| 13 |
set(_args "${_args} ${_arg}") |
| 14 |
endif() |
| 15 |
endforeach() |
| 16 |
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE) |
| 17 |
endfunction() |
| 18 |
|
| 19 |
function(catch_discover_tests_impl) |
| 20 |
|
| 21 |
cmake_parse_arguments( |
| 22 |
"" |
| 23 |
"" |
| 24 |
"TEST_EXECUTABLE;TEST_WORKING_DIR;TEST_DL_PATHS;TEST_OUTPUT_DIR;TEST_OUTPUT_PREFIX;TEST_OUTPUT_SUFFIX;TEST_PREFIX;TEST_REPORTER;TEST_SPEC;TEST_SUFFIX;TEST_LIST;CTEST_FILE" |
| 25 |
"TEST_EXTRA_ARGS;TEST_PROPERTIES;TEST_EXECUTOR" |
| 26 |
${ARGN} |
| 27 |
) |
| 28 |
|
| 29 |
set(prefix "${_TEST_PREFIX}") |
| 30 |
set(suffix "${_TEST_SUFFIX}") |
| 31 |
set(spec ${_TEST_SPEC}) |
| 32 |
set(extra_args ${_TEST_EXTRA_ARGS}) |
| 33 |
set(properties ${_TEST_PROPERTIES}) |
| 34 |
set(reporter ${_TEST_REPORTER}) |
| 35 |
set(output_dir ${_TEST_OUTPUT_DIR}) |
| 36 |
set(output_prefix ${_TEST_OUTPUT_PREFIX}) |
| 37 |
set(output_suffix ${_TEST_OUTPUT_SUFFIX}) |
| 38 |
set(dl_paths ${_TEST_DL_PATHS}) |
| 39 |
set(script) |
| 40 |
set(suite) |
| 41 |
set(tests) |
| 42 |
|
| 43 |
if(WIN32) |
| 44 |
set(dl_paths_variable_name PATH) |
| 45 |
elseif(APPLE) |
| 46 |
set(dl_paths_variable_name DYLD_LIBRARY_PATH) |
| 47 |
else() |
| 48 |
set(dl_paths_variable_name LD_LIBRARY_PATH) |
| 49 |
endif() |
| 50 |
|
| 51 |
# Run test executable to get list of available tests |
| 52 |
if(NOT EXISTS "${_TEST_EXECUTABLE}") |
| 53 |
message(FATAL_ERROR |
| 54 |
"Specified test executable '${_TEST_EXECUTABLE}' does not exist" |
| 55 |
) |
| 56 |
endif() |
| 57 |
|
| 58 |
if(dl_paths) |
| 59 |
cmake_path(CONVERT "${dl_paths}" TO_NATIVE_PATH_LIST paths) |
| 60 |
set(ENV{${dl_paths_variable_name}} "${paths}") |
| 61 |
endif() |
| 62 |
|
| 63 |
execute_process( |
| 64 |
COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" ${spec} --list-tests --verbosity quiet |
| 65 |
OUTPUT_VARIABLE output |
| 66 |
RESULT_VARIABLE result |
| 67 |
WORKING_DIRECTORY "${_TEST_WORKING_DIR}" |
| 68 |
) |
| 69 |
if(NOT ${result} EQUAL 0) |
| 70 |
message(FATAL_ERROR |
| 71 |
"Error running test executable '${_TEST_EXECUTABLE}':\n" |
| 72 |
" Result: ${result}\n" |
| 73 |
" Output: ${output}\n" |
| 74 |
) |
| 75 |
endif() |
| 76 |
|
| 77 |
# Make sure to escape ; (semicolons) in test names first, because |
| 78 |
# that'd break the foreach loop for "Parse output" later and create |
| 79 |
# wrongly splitted and thus failing test cases (false positives) |
| 80 |
string(REPLACE ";" "\;" output "${output}") |
| 81 |
string(REPLACE "\n" ";" output "${output}") |
| 82 |
|
| 83 |
# Prepare reporter |
| 84 |
if(reporter) |
| 85 |
set(reporter_arg "--reporter ${reporter}") |
| 86 |
|
| 87 |
# Run test executable to check whether reporter is available |
| 88 |
# note that the use of --list-reporters is not the important part, |
| 89 |
# we only want to check whether the execution succeeds with ${reporter_arg} |
| 90 |
execute_process( |
| 91 |
COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} ${reporter_arg} --list-reporters |
| 92 |
OUTPUT_VARIABLE reporter_check_output |
| 93 |
RESULT_VARIABLE reporter_check_result |
| 94 |
WORKING_DIRECTORY "${TEST_WORKING_DIR}" |
| 95 |
) |
| 96 |
if(${reporter_check_result} EQUAL 255) |
| 97 |
message(FATAL_ERROR |
| 98 |
"\"${reporter}\" is not a valid reporter!\n" |
| 99 |
) |
| 100 |
elseif(NOT ${reporter_check_result} EQUAL 0) |
| 101 |
message(FATAL_ERROR |
| 102 |
"Error running test executable '${TEST_EXECUTABLE}':\n" |
| 103 |
" Result: ${reporter_check_result}\n" |
| 104 |
" Output: ${reporter_check_output}\n" |
| 105 |
) |
| 106 |
endif() |
| 107 |
endif() |
| 108 |
|
| 109 |
# Prepare output dir |
| 110 |
if(output_dir AND NOT IS_ABSOLUTE ${output_dir}) |
| 111 |
set(output_dir "${_TEST_WORKING_DIR}/${output_dir}") |
| 112 |
if(NOT EXISTS ${output_dir}) |
| 113 |
file(MAKE_DIRECTORY ${output_dir}) |
| 114 |
endif() |
| 115 |
endif() |
| 116 |
|
| 117 |
if(dl_paths) |
| 118 |
foreach(path ${dl_paths}) |
| 119 |
cmake_path(NATIVE_PATH path native_path) |
| 120 |
list(APPEND environment_modifications "${dl_paths_variable_name}=path_list_prepend:${native_path}") |
| 121 |
endforeach() |
| 122 |
endif() |
| 123 |
|
| 124 |
# Parse output |
| 125 |
foreach(line ${output}) |
| 126 |
set(test "${line}") |
| 127 |
# Escape characters in test case names that would be parsed by Catch2 |
| 128 |
# Note that the \ escaping must happen FIRST! Do not change the order. |
| 129 |
set(test_name "${test}") |
| 130 |
foreach(char \\ , [ ]) |
| 131 |
string(REPLACE ${char} "\\${char}" test_name "${test_name}") |
| 132 |
endforeach(char) |
| 133 |
# ...add output dir |
| 134 |
if(output_dir) |
| 135 |
string(REGEX REPLACE "[^A-Za-z0-9_]" "_" test_name_clean "${test_name}") |
| 136 |
set(output_dir_arg "--out ${output_dir}/${output_prefix}${test_name_clean}${output_suffix}") |
| 137 |
endif() |
| 138 |
|
| 139 |
# ...and add to script |
| 140 |
add_command(add_test |
| 141 |
"${prefix}${test}${suffix}" |
| 142 |
${_TEST_EXECUTOR} |
| 143 |
"${_TEST_EXECUTABLE}" |
| 144 |
"${test_name}" |
| 145 |
${extra_args} |
| 146 |
"${reporter_arg}" |
| 147 |
"${output_dir_arg}" |
| 148 |
) |
| 149 |
add_command(set_tests_properties |
| 150 |
"${prefix}${test}${suffix}" |
| 151 |
PROPERTIES |
| 152 |
WORKING_DIRECTORY "${_TEST_WORKING_DIR}" |
| 153 |
${properties} |
| 154 |
) |
| 155 |
|
| 156 |
if(environment_modifications) |
| 157 |
add_command(set_tests_properties |
| 158 |
"${prefix}${test}${suffix}" |
| 159 |
PROPERTIES |
| 160 |
ENVIRONMENT_MODIFICATION "${environment_modifications}") |
| 161 |
endif() |
| 162 |
|
| 163 |
list(APPEND tests "${prefix}${test}${suffix}") |
| 164 |
endforeach() |
| 165 |
|
| 166 |
# Create a list of all discovered tests, which users may use to e.g. set |
| 167 |
# properties on the tests |
| 168 |
add_command(set ${_TEST_LIST} ${tests}) |
| 169 |
|
| 170 |
# Write CTest script |
| 171 |
file(WRITE "${_CTEST_FILE}" "${script}") |
| 172 |
endfunction() |
| 173 |
|
| 174 |
if(CMAKE_SCRIPT_MODE_FILE) |
| 175 |
catch_discover_tests_impl( |
| 176 |
TEST_EXECUTABLE ${TEST_EXECUTABLE} |
| 177 |
TEST_EXECUTOR ${TEST_EXECUTOR} |
| 178 |
TEST_WORKING_DIR ${TEST_WORKING_DIR} |
| 179 |
TEST_SPEC ${TEST_SPEC} |
| 180 |
TEST_EXTRA_ARGS ${TEST_EXTRA_ARGS} |
| 181 |
TEST_PROPERTIES ${TEST_PROPERTIES} |
| 182 |
TEST_PREFIX ${TEST_PREFIX} |
| 183 |
TEST_SUFFIX ${TEST_SUFFIX} |
| 184 |
TEST_LIST ${TEST_LIST} |
| 185 |
TEST_REPORTER ${TEST_REPORTER} |
| 186 |
TEST_OUTPUT_DIR ${TEST_OUTPUT_DIR} |
| 187 |
TEST_OUTPUT_PREFIX ${TEST_OUTPUT_PREFIX} |
| 188 |
TEST_OUTPUT_SUFFIX ${TEST_OUTPUT_SUFFIX} |
| 189 |
TEST_DL_PATHS ${TEST_DL_PATHS} |
| 190 |
CTEST_FILE ${CTEST_FILE} |
| 191 |
) |
| 192 |
endif() |