doctestAddTests.cmake (3688B)
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(add_labels ${TEST_ADD_LABELS}) 10 set(junit_output_dir "${TEST_JUNIT_OUTPUT_DIR}") 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 42 WORKING_DIRECTORY "${TEST_WORKING_DIR}" 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}) 60 set(labels "") 61 if(${add_labels}) 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 67 WORKING_DIRECTORY "${TEST_WORKING_DIR}" 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 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() 93 # use escape commas to handle properly test cases with commas inside the name 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}" 101 "${TEST_JUNIT_OUTPUT_PARAM}" 102 ${extra_args} 103 ) 104 add_command(set_tests_properties 105 "${prefix}${test}${suffix}" 106 PROPERTIES 107 WORKING_DIRECTORY "${TEST_WORKING_DIR}" 108 ${properties} 109 LABELS ${labels} 110 ) 111 unset(labels) 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}")