blob: 98ee4a2c5a3246b5788aa8e74a6a360fbae19813 [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})
9set(script)
10set(suite)
11set(tests)
12
13function(add_command NAME)
14 set(_args "")
15 foreach(_arg ${ARGN})
16 if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
17 set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
18 else()
19 set(_args "${_args} ${_arg}")
20 endif()
21 endforeach()
22 set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
23endfunction()
24
25# Run test executable to get list of available tests
26if(NOT EXISTS "${TEST_EXECUTABLE}")
27 message(FATAL_ERROR
28 "Specified test executable '${TEST_EXECUTABLE}' does not exist"
29 )
30endif()
31
32if("${spec}" MATCHES .)
33 set(spec "--test-case=${spec}")
34endif()
35
36execute_process(
37 COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-cases
38 OUTPUT_VARIABLE output
39 RESULT_VARIABLE result
40)
41if(NOT ${result} EQUAL 0)
42 message(FATAL_ERROR
43 "Error running test executable '${TEST_EXECUTABLE}':\n"
44 " Result: ${result}\n"
45 " Output: ${output}\n"
46 )
47endif()
48
49string(REPLACE "\n" ";" output "${output}")
50
51# Parse output
52foreach(line ${output})
53 if("${line}" STREQUAL "===============================================================================" OR "${line}" MATCHES [==[^\[doctest\] ]==])
54 continue()
55 endif()
56 set(test ${line})
warmsocks33cc7f92019-09-12 01:24:04 -070057 # use escape commas to handle properly test cases with commas inside the name
Cristian Morales Vega35b339f2019-05-19 20:12:41 +010058 string(REPLACE "," "\\," test_name ${test})
59 # ...and add to script
60 add_command(add_test
61 "${prefix}${test}${suffix}"
62 ${TEST_EXECUTOR}
63 "${TEST_EXECUTABLE}"
64 "--test-case=${test_name}"
65 ${extra_args}
66 )
67 add_command(set_tests_properties
68 "${prefix}${test}${suffix}"
69 PROPERTIES
70 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
71 ${properties}
72 )
73 list(APPEND tests "${prefix}${test}${suffix}")
74endforeach()
75
76# Create a list of all discovered tests, which users may use to e.g. set
77# properties on the tests
78add_command(set ${TEST_LIST} ${tests})
79
80# Write CTest script
81file(WRITE "${CTEST_FILE}" "${script}")