1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 set(prefix "${TEST_PREFIX}")
5 set(suffix "${TEST_SUFFIX}")
7 set(extra_args ${TEST_EXTRA_ARGS})
8 set(properties ${TEST_PROPERTIES})
9 set(reporter ${TEST_REPORTER})
10 set(output_dir ${TEST_OUTPUT_DIR})
11 set(output_prefix ${TEST_OUTPUT_PREFIX})
12 set(output_suffix ${TEST_OUTPUT_SUFFIX})
17 function(add_command NAME)
19 # use ARGV* instead of ARGN, because ARGN splits arrays into multiple arguments
20 math(EXPR _last_arg ${ARGC}-1)
21 foreach(_n RANGE 1 ${_last_arg})
22 set(_arg "${ARGV${_n}}")
23 if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
24 set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
26 set(_args "${_args} ${_arg}")
29 set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
32 # Run test executable to get list of available tests
33 if(NOT EXISTS "${TEST_EXECUTABLE}")
35 "Specified test executable '${TEST_EXECUTABLE}' does not exist"
39 COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-names-only
40 OUTPUT_VARIABLE output
41 RESULT_VARIABLE result
42 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
44 # Catch --list-test-names-only reports the number of tests, so 0 is... surprising
47 "Test executable '${TEST_EXECUTABLE}' contains no tests!\n"
49 elseif(${result} LESS 0)
51 "Error running test executable '${TEST_EXECUTABLE}':\n"
52 " Result: ${result}\n"
53 " Output: ${output}\n"
57 string(REPLACE "\n" ";" output "${output}")
59 # Run test executable to get list of available reporters
61 COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-reporters
62 OUTPUT_VARIABLE reporters_output
63 RESULT_VARIABLE reporters_result
64 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
66 if(${reporters_result} EQUAL 0)
68 "Test executable '${TEST_EXECUTABLE}' contains no reporters!\n"
70 elseif(${reporters_result} LESS 0)
72 "Error running test executable '${TEST_EXECUTABLE}':\n"
73 " Result: ${reporters_result}\n"
74 " Output: ${reporters_output}\n"
77 string(FIND "${reporters_output}" "${reporter}" reporter_is_valid)
78 if(reporter AND ${reporter_is_valid} EQUAL -1)
80 "\"${reporter}\" is not a valid reporter!\n"
86 set(reporter_arg "--reporter ${reporter}")
90 if(output_dir AND NOT IS_ABSOLUTE ${output_dir})
91 set(output_dir "${TEST_WORKING_DIR}/${output_dir}")
92 if(NOT EXISTS ${output_dir})
93 file(MAKE_DIRECTORY ${output_dir})
98 foreach(line ${output})
100 # Escape characters in test case names that would be parsed by Catch2
101 set(test_name ${test})
103 string(REPLACE ${char} "\\${char}" test_name ${test_name})
107 string(REGEX REPLACE "[^A-Za-z0-9_]" "_" test_name_clean ${test_name})
108 set(output_dir_arg "--out ${output_dir}/${output_prefix}${test_name_clean}${output_suffix}")
111 # ...and add to script
113 "${prefix}${test}${suffix}"
121 add_command(set_tests_properties
122 "${prefix}${test}${suffix}"
124 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
127 list(APPEND tests "${prefix}${test}${suffix}")
130 # Create a list of all discovered tests, which users may use to e.g. set
131 # properties on the tests
132 add_command(set ${TEST_LIST} ${tests})
135 file(WRITE "${CTEST_FILE}" "${script}")