Debian Codegraph @

podofo/test/common/cmake/CatchAddTests.cmake

raw
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(reporter ${TEST_REPORTER})
10 set(output_dir ${TEST_OUTPUT_DIR})
11 set(output_prefix ${TEST_OUTPUT_PREFIX})
12 set(output_suffix ${TEST_OUTPUT_SUFFIX})
13 set(script)
14 set(suite)
15 set(tests)
16
17 function(add_command NAME)
18 set(_args "")
19 # use ARGV* instead of ARGN, because ARGN splits arrays into multiple arguments
20 math(EXPR _last_arg ${ARGC}-1)
21 foreach(_n RANGE 1 ${_last_arg})
22 set(_arg "${ARGV${_n}}")
23 if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
24 set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
25 else()
26 set(_args "${_args} ${_arg}")
27 endif()
28 endforeach()
29 set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
30 endfunction()
31
32 # Run test executable to get list of available tests
33 if(NOT EXISTS "${TEST_EXECUTABLE}")
34 message(FATAL_ERROR
35 "Specified test executable '${TEST_EXECUTABLE}' does not exist"
36 )
37 endif()
38 execute_process(
39 COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-names-only
40 OUTPUT_VARIABLE output
41 RESULT_VARIABLE result
42 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
43 )
44 # Catch --list-test-names-only reports the number of tests, so 0 is... surprising
45 if(${result} EQUAL 0)
46 message(WARNING
47 "Test executable '${TEST_EXECUTABLE}' contains no tests!\n"
48 )
49 elseif(${result} LESS 0)
50 message(FATAL_ERROR
51 "Error running test executable '${TEST_EXECUTABLE}':\n"
52 " Result: ${result}\n"
53 " Output: ${output}\n"
54 )
55 endif()
56
57 string(REPLACE "\n" ";" output "${output}")
58
59 # Run test executable to get list of available reporters
60 execute_process(
61 COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-reporters
62 OUTPUT_VARIABLE reporters_output
63 RESULT_VARIABLE reporters_result
64 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
65 )
66 if(${reporters_result} EQUAL 0)
67 message(WARNING
68 "Test executable '${TEST_EXECUTABLE}' contains no reporters!\n"
69 )
70 elseif(${reporters_result} LESS 0)
71 message(FATAL_ERROR
72 "Error running test executable '${TEST_EXECUTABLE}':\n"
73 " Result: ${reporters_result}\n"
74 " Output: ${reporters_output}\n"
75 )
76 endif()
77 string(FIND "${reporters_output}" "${reporter}" reporter_is_valid)
78 if(reporter AND ${reporter_is_valid} EQUAL -1)
79 message(FATAL_ERROR
80 "\"${reporter}\" is not a valid reporter!\n"
81 )
82 endif()
83
84 # Prepare reporter
85 if(reporter)
86 set(reporter_arg "--reporter ${reporter}")
87 endif()
88
89 # Prepare output dir
90 if(output_dir AND NOT IS_ABSOLUTE ${output_dir})
91 set(output_dir "${TEST_WORKING_DIR}/${output_dir}")
92 if(NOT EXISTS ${output_dir})
93 file(MAKE_DIRECTORY ${output_dir})
94 endif()
95 endif()
96
97 # Parse output
98 foreach(line ${output})
99 set(test ${line})
100 # Escape characters in test case names that would be parsed by Catch2
101 set(test_name ${test})
102 foreach(char , [ ])
103 string(REPLACE ${char} "\\${char}" test_name ${test_name})
104 endforeach(char)
105 # ...add output dir
106 if(output_dir)
107 string(REGEX REPLACE "[^A-Za-z0-9_]" "_" test_name_clean ${test_name})
108 set(output_dir_arg "--out ${output_dir}/${output_prefix}${test_name_clean}${output_suffix}")
109 endif()
110
111 # ...and add to script
112 add_command(add_test
113 "${prefix}${test}${suffix}"
114 ${TEST_EXECUTOR}
115 "${TEST_EXECUTABLE}"
116 "${test_name}"
117 ${extra_args}
118 "${reporter_arg}"
119 "${output_dir_arg}"
120 )
121 add_command(set_tests_properties
122 "${prefix}${test}${suffix}"
123 PROPERTIES
124 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
125 ${properties}
126 )
127 list(APPEND tests "${prefix}${test}${suffix}")
128 endforeach()
129
130 # Create a list of all discovered tests, which users may use to e.g. set
131 # properties on the tests
132 add_command(set ${TEST_LIST} ${tests})
133
134 # Write CTest script
135 file(WRITE "${CTEST_FILE}" "${script}")