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}) |
| 9 | set(script) |
| 10 | set(suite) |
| 11 | set(tests) |
| 12 | |
| 13 | function(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) |
| 23 | endfunction() |
| 24 | |
| 25 | # Run test executable to get list of available tests |
| 26 | if(NOT EXISTS "${TEST_EXECUTABLE}") |
| 27 | message(FATAL_ERROR |
| 28 | "Specified test executable '${TEST_EXECUTABLE}' does not exist" |
| 29 | ) |
| 30 | endif() |
| 31 | |
| 32 | if("${spec}" MATCHES .) |
| 33 | set(spec "--test-case=${spec}") |
| 34 | endif() |
| 35 | |
| 36 | execute_process( |
| 37 | COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-cases |
| 38 | OUTPUT_VARIABLE output |
| 39 | RESULT_VARIABLE result |
| 40 | ) |
| 41 | if(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 | ) |
| 47 | endif() |
| 48 | |
| 49 | string(REPLACE "\n" ";" output "${output}") |
| 50 | |
| 51 | # Parse output |
| 52 | foreach(line ${output}) |
| 53 | if("${line}" STREQUAL "===============================================================================" OR "${line}" MATCHES [==[^\[doctest\] ]==]) |
| 54 | continue() |
| 55 | endif() |
| 56 | set(test ${line}) |
warmsocks | 33cc7f9 | 2019-09-12 01:24:04 -0700 | [diff] [blame] | 57 | # 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] | 58 | 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}") |
| 74 | endforeach() |
| 75 | |
| 76 | # Create a list of all discovered tests, which users may use to e.g. set |
| 77 | # properties on the tests |
| 78 | add_command(set ${TEST_LIST} ${tests}) |
| 79 | |
| 80 | # Write CTest script |
| 81 | file(WRITE "${CTEST_FILE}" "${script}") |