1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 cmake_minimum_required(VERSION ${CMAKE_VERSION})
6 # Overwrite possibly existing ${_CTEST_FILE} with empty file
7 set(flush_tests_MODE WRITE)
9 # Flushes script to ${_CTEST_FILE}
11 file(${flush_tests_MODE} "${_CTEST_FILE}" "${script}")
12 set(flush_tests_MODE APPEND PARENT_SCOPE)
17 # Flushes tests_buffer to tests
18 macro(flush_tests_buffer)
19 list(APPEND tests "${tests_buffer}")
23 function(add_command NAME TEST_NAME)
26 if(arg MATCHES "[^-./:a-zA-Z0-9_]")
27 string(APPEND args " [==[${arg}]==]")
29 string(APPEND args " ${arg}")
32 string(APPEND script "${NAME}(${TEST_NAME} ${args})\n")
33 string(LENGTH "${script}" script_len)
34 if(${script_len} GREATER "50000")
37 set(script "${script}" PARENT_SCOPE)
40 function(generate_testname_guards OUTPUT OPEN_GUARD_VAR CLOSE_GUARD_VAR)
42 set(close_guard "]=]")
44 while("${OUTPUT}" MATCHES "${close_guard}")
45 math(EXPR counter "${counter} + 1")
46 string(REPEAT "=" ${counter} equals)
47 set(open_guard "[${equals}[")
48 set(close_guard "]${equals}]")
50 set(${OPEN_GUARD_VAR} "${open_guard}" PARENT_SCOPE)
51 set(${CLOSE_GUARD_VAR} "${close_guard}" PARENT_SCOPE)
54 function(escape_square_brackets OUTPUT BRACKET PLACEHOLDER PLACEHOLDER_VAR OUTPUT_VAR)
55 if("${OUTPUT}" MATCHES "\\${BRACKET}")
56 set(placeholder "${PLACEHOLDER}")
57 while("${OUTPUT}" MATCHES "${placeholder}")
58 set(placeholder "${placeholder}_")
60 string(REPLACE "${BRACKET}" "${placeholder}" OUTPUT "${OUTPUT}")
61 set(${PLACEHOLDER_VAR} "${placeholder}" PARENT_SCOPE)
62 set(${OUTPUT_VAR} "${OUTPUT}" PARENT_SCOPE)
66 function(gtest_discover_tests_impl)
68 cmake_parse_arguments(
71 "NO_PRETTY_TYPES;NO_PRETTY_VALUES;TEST_EXECUTABLE;TEST_WORKING_DIR;TEST_PREFIX;TEST_SUFFIX;TEST_LIST;CTEST_FILE;TEST_DISCOVERY_TIMEOUT;TEST_XML_OUTPUT_DIR;TEST_FILTER"
72 "TEST_EXTRA_ARGS;TEST_PROPERTIES;TEST_EXECUTOR"
76 set(prefix "${_TEST_PREFIX}")
77 set(suffix "${_TEST_SUFFIX}")
78 set(extra_args ${_TEST_EXTRA_ARGS})
79 set(properties ${_TEST_PROPERTIES})
86 set(filter "--gtest_filter=${_TEST_FILTER}")
91 # Run test executable to get list of available tests
92 if(NOT EXISTS "${_TEST_EXECUTABLE}")
94 "Specified test executable does not exist.\n"
95 " Path: '${_TEST_EXECUTABLE}'"
99 COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" --gtest_list_tests ${filter}
100 WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
101 TIMEOUT ${_TEST_DISCOVERY_TIMEOUT}
102 OUTPUT_VARIABLE output
103 RESULT_VARIABLE result
105 if(NOT ${result} EQUAL 0)
106 string(REPLACE "\n" "\n " output "${output}")
108 set(path "${_TEST_EXECUTOR} ${_TEST_EXECUTABLE}")
110 set(path "${_TEST_EXECUTABLE}")
113 "Error running test executable.\n"
115 " Working directory: '${_TEST_WORKING_DIR}'\n"
116 " Result: ${result}\n"
122 generate_testname_guards("${output}" open_guard close_guard)
123 escape_square_brackets("${output}" "[" "__osb" open_sb output)
124 escape_square_brackets("${output}" "]" "__csb" close_sb output)
125 # Preserve semicolon in test-parameters
126 string(REPLACE [[;]] [[\;]] output "${output}")
127 string(REPLACE "\n" ";" output "${output}")
130 foreach(line ${output})
132 if(NOT line MATCHES "gtest_main\\.cc")
133 # Do we have a module name or a test name?
134 if(NOT line MATCHES "^ ")
135 # Module; remove trailing '.' to get just the name...
136 string(REGEX REPLACE "\\.( *#.*)?$" "" suite "${line}")
138 string(REGEX REPLACE "/[0-9].*" "" pretty_suite "${line}")
139 if(NOT _NO_PRETTY_TYPES)
140 string(REGEX REPLACE ".*/[0-9]+[ .#]+TypeParam = (.*)" "\\1" type_parameter "${line}")
142 string(REGEX REPLACE ".*/([0-9]+)[ .#]+TypeParam = .*" "\\1" type_parameter "${line}")
144 set(test_name_template "@prefix@@pretty_suite@.@pretty_test@<@type_parameter@>@suffix@")
146 set(pretty_suite "${suite}")
147 set(test_name_template "@prefix@@pretty_suite@.@pretty_test@@suffix@")
149 string(REGEX REPLACE "^DISABLED_" "" pretty_suite "${pretty_suite}")
151 string(STRIP "${line}" test)
152 if(test MATCHES "#" AND NOT _NO_PRETTY_VALUES)
153 string(REGEX REPLACE "/[0-9]+[ #]+GetParam\\(\\) = " "/" pretty_test "${test}")
155 string(REGEX REPLACE " +#.*" "" pretty_test "${test}")
157 string(REGEX REPLACE "^DISABLED_" "" pretty_test "${pretty_test}")
158 string(REGEX REPLACE " +#.*" "" test "${test}")
159 if(NOT "${_TEST_XML_OUTPUT_DIR}" STREQUAL "")
160 set(TEST_XML_OUTPUT_PARAM "--gtest_output=xml:${_TEST_XML_OUTPUT_DIR}/${prefix}${suite}.${test}${suffix}.xml")
162 unset(TEST_XML_OUTPUT_PARAM)
165 string(CONFIGURE "${test_name_template}" testname)
168 string(REPLACE "${open_sb}" "[" testname "${testname}")
171 string(REPLACE "${close_sb}" "]" testname "${testname}")
173 set(guarded_testname "${open_guard}${testname}${close_guard}")
177 "${guarded_testname}"
179 "${_TEST_EXECUTABLE}"
180 "--gtest_filter=${suite}.${test}"
181 "--gtest_also_run_disabled_tests"
182 ${TEST_XML_OUTPUT_PARAM}
185 if(suite MATCHES "^DISABLED_" OR test MATCHES "^DISABLED_")
186 add_command(set_tests_properties
187 "${guarded_testname}"
188 PROPERTIES DISABLED TRUE
192 add_command(set_tests_properties
193 "${guarded_testname}"
195 WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
196 SKIP_REGULAR_EXPRESSION "\\[ SKIPPED \\]"
200 # possibly unbalanced square brackets render lists invalid so skip such tests in ${_TEST_LIST}
201 if(NOT "${testname}" MATCHES [=[(\[|\])]=])
203 string(REPLACE [[;]] [[\\;]] testname "${testname}")
204 list(APPEND tests_buffer "${testname}")
205 list(LENGTH tests_buffer tests_buffer_length)
206 if(${tests_buffer_length} GREATER "250")
215 # Create a list of all discovered tests, which users may use to e.g. set
216 # properties on the tests
218 add_command(set "" ${_TEST_LIST} "${tests}")
225 if(CMAKE_SCRIPT_MODE_FILE)
226 gtest_discover_tests_impl(
227 NO_PRETTY_TYPES ${NO_PRETTY_TYPES}
228 NO_PRETTY_VALUES ${NO_PRETTY_VALUES}
229 TEST_EXECUTABLE ${TEST_EXECUTABLE}
230 TEST_EXECUTOR ${TEST_EXECUTOR}
231 TEST_WORKING_DIR ${TEST_WORKING_DIR}
232 TEST_PREFIX ${TEST_PREFIX}
233 TEST_SUFFIX ${TEST_SUFFIX}
234 TEST_FILTER ${TEST_FILTER}
235 TEST_LIST ${TEST_LIST}
236 CTEST_FILE ${CTEST_FILE}
237 TEST_DISCOVERY_TIMEOUT ${TEST_DISCOVERY_TIMEOUT}
238 TEST_XML_OUTPUT_DIR ${TEST_XML_OUTPUT_DIR}
239 TEST_EXTRA_ARGS ${TEST_EXTRA_ARGS}
240 TEST_PROPERTIES ${TEST_PROPERTIES}