Cristian Morales Vega | 35b339f | 2019-05-19 20:12:41 +0100 | [diff] [blame] | 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 | set(prefix "${TEST_PREFIX}") |
| 5 | set(suffix "${TEST_SUFFIX}") |
| 6 | set(spec ${TEST_SPEC}) |
| 7 | set(extra_args ${TEST_EXTRA_ARGS}) |
| 8 | set(properties ${TEST_PROPERTIES}) |
Shiv Upadhyay | 680d044 | 2021-02-18 07:15:39 -0800 | [diff] [blame] | 9 | set(add_labels ${TEST_ADD_LABELS}) |
Dennis | 428b697 | 2020-09-06 17:51:35 +0200 | [diff] [blame] | 10 | set(junit_output_dir "${TEST_JUNIT_OUTPUT_DIR}") |
Cristian Morales Vega | 35b339f | 2019-05-19 20:12:41 +0100 | [diff] [blame] | 11 | set(script) |
| 12 | set(suite) |
| 13 | set(tests) |
| 14 | |
| 15 | function(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) |
| 25 | endfunction() |
| 26 | |
| 27 | # Run test executable to get list of available tests |
| 28 | if(NOT EXISTS "${TEST_EXECUTABLE}") |
| 29 | message(FATAL_ERROR |
| 30 | "Specified test executable '${TEST_EXECUTABLE}' does not exist" |
| 31 | ) |
| 32 | endif() |
| 33 | |
| 34 | if("${spec}" MATCHES .) |
| 35 | set(spec "--test-case=${spec}") |
| 36 | endif() |
| 37 | |
| 38 | execute_process( |
| 39 | COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-cases |
| 40 | OUTPUT_VARIABLE output |
| 41 | RESULT_VARIABLE result |
Philipp Bucher | 2744a17 | 2021-11-04 16:59:47 +0100 | [diff] [blame] | 42 | WORKING_DIRECTORY "${TEST_WORKING_DIR}" |
Cristian Morales Vega | 35b339f | 2019-05-19 20:12:41 +0100 | [diff] [blame] | 43 | ) |
| 44 | if(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 | ) |
| 50 | endif() |
| 51 | |
| 52 | string(REPLACE "\n" ";" output "${output}") |
| 53 | |
| 54 | # Parse output |
| 55 | foreach(line ${output}) |
| 56 | if("${line}" STREQUAL "===============================================================================" OR "${line}" MATCHES [==[^\[doctest\] ]==]) |
| 57 | continue() |
| 58 | endif() |
| 59 | set(test ${line}) |
Shiv Upadhyay | 680d044 | 2021-02-18 07:15:39 -0800 | [diff] [blame] | 60 | set(labels "") |
Jackson | 60de223 | 2021-11-04 11:35:58 -0400 | [diff] [blame] | 61 | if(${add_labels}) |
Shiv Upadhyay | 680d044 | 2021-02-18 07:15:39 -0800 | [diff] [blame] | 62 | # 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 Bucher | 2744a17 | 2021-11-04 16:59:47 +0100 | [diff] [blame] | 67 | WORKING_DIRECTORY "${TEST_WORKING_DIR}" |
Shiv Upadhyay | 680d044 | 2021-02-18 07:15:39 -0800 | [diff] [blame] | 68 | ) |
| 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 | |
Dennis | 428b697 | 2020-09-06 17:51:35 +0200 | [diff] [blame] | 86 | 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() |
warmsocks | 33cc7f9 | 2019-09-12 01:24:04 -0700 | [diff] [blame] | 93 | # use escape commas to handle properly test cases with commas inside the name |
Cristian Morales Vega | 35b339f | 2019-05-19 20:12:41 +0100 | [diff] [blame] | 94 | 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}" |
Dennis | 428b697 | 2020-09-06 17:51:35 +0200 | [diff] [blame] | 101 | "${TEST_JUNIT_OUTPUT_PARAM}" |
Cristian Morales Vega | 35b339f | 2019-05-19 20:12:41 +0100 | [diff] [blame] | 102 | ${extra_args} |
| 103 | ) |
| 104 | add_command(set_tests_properties |
| 105 | "${prefix}${test}${suffix}" |
| 106 | PROPERTIES |
| 107 | WORKING_DIRECTORY "${TEST_WORKING_DIR}" |
Shiv Upadhyay | 680d044 | 2021-02-18 07:15:39 -0800 | [diff] [blame] | 108 | LABELS ${labels} |
Cristian Morales Vega | 35b339f | 2019-05-19 20:12:41 +0100 | [diff] [blame] | 109 | ${properties} |
| 110 | ) |
Shiv Upadhyay | 680d044 | 2021-02-18 07:15:39 -0800 | [diff] [blame] | 111 | unset(labels) |
Cristian Morales Vega | 35b339f | 2019-05-19 20:12:41 +0100 | [diff] [blame] | 112 | list(APPEND tests "${prefix}${test}${suffix}") |
| 113 | endforeach() |
| 114 | |
| 115 | # Create a list of all discovered tests, which users may use to e.g. set |
| 116 | # properties on the tests |
| 117 | add_command(set ${TEST_LIST} ${tests}) |
| 118 | |
| 119 | # Write CTest script |
| 120 | file(WRITE "${CTEST_FILE}" "${script}") |