blob: 96d42bcb77f7b9f468f1f0a0999c2ef3176b449f [file] [log] [blame]
Cristian Morales Vega35b339f2019-05-19 20:12:41 +01001# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4set(prefix "${TEST_PREFIX}")
5set(suffix "${TEST_SUFFIX}")
6set(spec ${TEST_SPEC})
7set(extra_args ${TEST_EXTRA_ARGS})
8set(properties ${TEST_PROPERTIES})
Shiv Upadhyay680d0442021-02-18 07:15:39 -08009set(add_labels ${TEST_ADD_LABELS})
Dennis428b6972020-09-06 17:51:35 +020010set(junit_output_dir "${TEST_JUNIT_OUTPUT_DIR}")
Cristian Morales Vega35b339f2019-05-19 20:12:41 +010011set(script)
12set(suite)
13set(tests)
14
15function(add_command NAME)
16 set(_args "")
17 foreach(_arg ${ARGN})
18 if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
19 set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
20 else()
21 set(_args "${_args} ${_arg}")
22 endif()
23 endforeach()
24 set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
25endfunction()
26
27# Run test executable to get list of available tests
28if(NOT EXISTS "${TEST_EXECUTABLE}")
29 message(FATAL_ERROR
30 "Specified test executable '${TEST_EXECUTABLE}' does not exist"
31 )
32endif()
33
34if("${spec}" MATCHES .)
35 set(spec "--test-case=${spec}")
36endif()
37
38execute_process(
39 COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-cases
40 OUTPUT_VARIABLE output
41 RESULT_VARIABLE result
Philipp Bucher2744a172021-11-04 16:59:47 +010042 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
Cristian Morales Vega35b339f2019-05-19 20:12:41 +010043)
44if(NOT ${result} EQUAL 0)
45 message(FATAL_ERROR
46 "Error running test executable '${TEST_EXECUTABLE}':\n"
47 " Result: ${result}\n"
48 " Output: ${output}\n"
49 )
50endif()
51
52string(REPLACE "\n" ";" output "${output}")
53
54# Parse output
55foreach(line ${output})
56 if("${line}" STREQUAL "===============================================================================" OR "${line}" MATCHES [==[^\[doctest\] ]==])
57 continue()
58 endif()
59 set(test ${line})
Shiv Upadhyay680d0442021-02-18 07:15:39 -080060 set(labels "")
Jackson60de2232021-11-04 11:35:58 -040061 if(${add_labels})
Shiv Upadhyay680d0442021-02-18 07:15:39 -080062 # get test suite that test belongs to
63 execute_process(
64 COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" --test-case=${test} --list-test-suites
65 OUTPUT_VARIABLE labeloutput
66 RESULT_VARIABLE labelresult
Philipp Bucher2744a172021-11-04 16:59:47 +010067 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
Shiv Upadhyay680d0442021-02-18 07:15:39 -080068 )
69 if(NOT ${labelresult} EQUAL 0)
70 message(FATAL_ERROR
71 "Error running test executable '${TEST_EXECUTABLE}':\n"
72 " Result: ${labelresult}\n"
73 " Output: ${labeloutput}\n"
74 )
75 endif()
76
77 string(REPLACE "\n" ";" labeloutput "${labeloutput}")
78 foreach(labelline ${labeloutput})
79 if("${labelline}" STREQUAL "===============================================================================" OR "${labelline}" MATCHES [==[^\[doctest\] ]==])
80 continue()
81 endif()
82 list(APPEND labels ${labelline})
83 endforeach()
84 endif()
85
Dennis428b6972020-09-06 17:51:35 +020086 if(NOT "${junit_output_dir}" STREQUAL "")
87 # turn testname into a valid filename by replacing all special characters with "-"
88 string(REGEX REPLACE "[/\\:\"|<>]" "-" test_filename "${test}")
89 set(TEST_JUNIT_OUTPUT_PARAM "--reporters=junit" "--out=${junit_output_dir}/${prefix}${test_filename}${suffix}.xml")
90 else()
91 unset(TEST_JUNIT_OUTPUT_PARAM)
92 endif()
warmsocks33cc7f92019-09-12 01:24:04 -070093 # use escape commas to handle properly test cases with commas inside the name
Cristian Morales Vega35b339f2019-05-19 20:12:41 +010094 string(REPLACE "," "\\," test_name ${test})
95 # ...and add to script
96 add_command(add_test
97 "${prefix}${test}${suffix}"
98 ${TEST_EXECUTOR}
99 "${TEST_EXECUTABLE}"
100 "--test-case=${test_name}"
Dennis428b6972020-09-06 17:51:35 +0200101 "${TEST_JUNIT_OUTPUT_PARAM}"
Cristian Morales Vega35b339f2019-05-19 20:12:41 +0100102 ${extra_args}
103 )
104 add_command(set_tests_properties
105 "${prefix}${test}${suffix}"
106 PROPERTIES
107 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
Shiv Upadhyay680d0442021-02-18 07:15:39 -0800108 LABELS ${labels}
Cristian Morales Vega35b339f2019-05-19 20:12:41 +0100109 ${properties}
110 )
Shiv Upadhyay680d0442021-02-18 07:15:39 -0800111 unset(labels)
Cristian Morales Vega35b339f2019-05-19 20:12:41 +0100112 list(APPEND tests "${prefix}${test}${suffix}")
113endforeach()
114
115# Create a list of all discovered tests, which users may use to e.g. set
116# properties on the tests
117add_command(set ${TEST_LIST} ${tests})
118
119# Write CTest script
120file(WRITE "${CTEST_FILE}" "${script}")