Merge branch 'release-4.0'
[kiteware-cmake.git] / Modules / GoogleTestAddTests.cmake
blobafed307f496cfb725b920fbed9ff31f81df1c375
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 3.30)
5 cmake_policy(SET CMP0174 NEW)   # TODO: Remove this when we can update the above to 3.31
7 function(add_command name test_name)
8   set(args "")
9   foreach(arg ${ARGN})
10     if(arg MATCHES "[^-./:a-zA-Z0-9_]")
11       string(APPEND args " [==[${arg}]==]")
12     else()
13       string(APPEND args " ${arg}")
14     endif()
15   endforeach()
16   string(APPEND script "${name}(${test_name} ${args})\n")
17   set(script "${script}" PARENT_SCOPE)
18 endfunction()
20 function(generate_testname_guards output open_guard_var close_guard_var)
21   set(open_guard "[=[")
22   set(close_guard "]=]")
23   set(counter 1)
24   while("${output}" MATCHES "${close_guard}")
25     math(EXPR counter "${counter} + 1")
26     string(REPEAT "=" ${counter} equals)
27     set(open_guard "[${equals}[")
28     set(close_guard "]${equals}]")
29   endwhile()
30   set(${open_guard_var} "${open_guard}" PARENT_SCOPE)
31   set(${close_guard_var} "${close_guard}" PARENT_SCOPE)
32 endfunction()
34 function(escape_square_brackets output bracket placeholder placeholder_var output_var)
35   if("${output}" MATCHES "\\${bracket}")
36     set(placeholder "${placeholder}")
37     while("${output}" MATCHES "${placeholder}")
38         set(placeholder "${placeholder}_")
39     endwhile()
40     string(REPLACE "${bracket}" "${placeholder}" output "${output}")
41     set(${placeholder_var} "${placeholder}" PARENT_SCOPE)
42     set(${output_var} "${output}" PARENT_SCOPE)
43   endif()
44 endfunction()
46 function(gtest_discover_tests_impl)
48   set(options "")
49   set(oneValueArgs
50     NO_PRETTY_TYPES   # These two take a value, unlike gtest_discover_tests()
51     NO_PRETTY_VALUES  #
52     TEST_EXECUTABLE
53     TEST_WORKING_DIR
54     TEST_PREFIX
55     TEST_SUFFIX
56     TEST_LIST
57     CTEST_FILE
58     TEST_DISCOVERY_TIMEOUT
59     TEST_XML_OUTPUT_DIR
60     # The following are all multi-value arguments in gtest_discover_tests(),
61     # but they are each given to us as a single argument. We parse them that
62     # way to avoid problems with preserving empty list values and escaping.
63     TEST_FILTER
64     TEST_EXTRA_ARGS
65     TEST_DISCOVERY_EXTRA_ARGS
66     TEST_PROPERTIES
67     TEST_EXECUTOR
68   )
69   set(multiValueArgs "")
70   cmake_parse_arguments(PARSE_ARGV 0 arg
71     "${options}" "${oneValueArgs}" "${multiValueArgs}"
72   )
74   set(prefix "${arg_TEST_PREFIX}")
75   set(suffix "${arg_TEST_SUFFIX}")
76   set(script)
77   set(suite)
78   set(tests)
79   set(tests_buffer "")
81   # If a file at ${arg_CTEST_FILE} already exists, we overwrite it.
82   # For performance reasons, we write to this file in chunks, and this variable
83   # is updated to APPEND after the first write.
84   set(file_write_mode WRITE)
86   if(arg_TEST_FILTER)
87     set(filter "--gtest_filter=${arg_TEST_FILTER}")
88   else()
89     set(filter)
90   endif()
92   # CMP0178 has already been handled in gtest_discover_tests(), so we only need
93   # to implement NEW behavior here. This means preserving empty arguments for
94   # TEST_EXECUTOR. For OLD or WARN, gtest_discover_tests() already removed any
95   # empty arguments.
96   set(launcherArgs "")
97   if(NOT "${arg_TEST_EXECUTOR}" STREQUAL "")
98     list(JOIN arg_TEST_EXECUTOR "]==] [==[" launcherArgs)
99     set(launcherArgs "[==[${launcherArgs}]==]")
100   endif()
102   # Run test executable to get list of available tests
103   if(NOT EXISTS "${arg_TEST_EXECUTABLE}")
104     message(FATAL_ERROR
105       "Specified test executable does not exist.\n"
106       "  Path: '${arg_TEST_EXECUTABLE}'"
107     )
108   endif()
110   set(discovery_extra_args "")
111   if(NOT "${arg_TEST_DISCOVERY_EXTRA_ARGS}" STREQUAL "")
112     list(JOIN arg_TEST_DISCOVERY_EXTRA_ARGS "]==] [==[" discovery_extra_args)
113     set(discovery_extra_args "[==[${discovery_extra_args}]==]")
114   endif()
116   cmake_language(EVAL CODE
117     "execute_process(
118       COMMAND ${launcherArgs} [==[${arg_TEST_EXECUTABLE}]==] --gtest_list_tests ${filter} ${discovery_extra_args}
119       WORKING_DIRECTORY [==[${arg_TEST_WORKING_DIR}]==]
120       TIMEOUT ${arg_TEST_DISCOVERY_TIMEOUT}
121       OUTPUT_VARIABLE output
122       RESULT_VARIABLE result
123     )"
124   )
125   if(NOT ${result} EQUAL 0)
126     string(REPLACE "\n" "\n    " output "${output}")
127     if(arg_TEST_EXECUTOR)
128       set(path "${arg_TEST_EXECUTOR} ${arg_TEST_EXECUTABLE}")
129     else()
130       set(path "${arg_TEST_EXECUTABLE}")
131     endif()
132     message(FATAL_ERROR
133       "Error running test executable.\n"
134       "  Path: '${path}'\n"
135       "  Working directory: '${arg_TEST_WORKING_DIR}'\n"
136       "  Result: ${result}\n"
137       "  Output:\n"
138       "    ${output}\n"
139     )
140   endif()
142   generate_testname_guards("${output}" open_guard close_guard)
143   escape_square_brackets("${output}" "[" "__osb" open_sb output)
144   escape_square_brackets("${output}" "]" "__csb" close_sb output)
145   # Preserve semicolon in test-parameters
146   string(REPLACE [[;]] [[\;]] output "${output}")
147   string(REPLACE "\n" ";" output "${output}")
149   # Parse output
150   foreach(line ${output})
151     # Skip header
152     if(NOT line MATCHES "gtest_main\\.cc")
153       # Do we have a module name or a test name?
154       if(NOT line MATCHES "^  ")
155         # Module; remove trailing '.' to get just the name...
156         string(REGEX REPLACE "\\.( *#.*)?$" "" suite "${line}")
157         if(line MATCHES "#")
158           string(REGEX REPLACE "/[0-9].*" "" pretty_suite "${line}")
159           if(NOT arg_NO_PRETTY_TYPES)
160             string(REGEX REPLACE ".*/[0-9]+[ .#]+TypeParam = (.*)" "\\1" type_parameter "${line}")
161           else()
162             string(REGEX REPLACE ".*/([0-9]+)[ .#]+TypeParam = .*" "\\1" type_parameter "${line}")
163           endif()
164           set(test_name_template "@prefix@@pretty_suite@.@pretty_test@<@type_parameter@>@suffix@")
165         else()
166           set(pretty_suite "${suite}")
167           set(test_name_template "@prefix@@pretty_suite@.@pretty_test@@suffix@")
168         endif()
169         string(REGEX REPLACE "^DISABLED_" "" pretty_suite "${pretty_suite}")
170       else()
171         string(STRIP "${line}" test)
172         if(test MATCHES "#" AND NOT arg_NO_PRETTY_VALUES)
173           string(REGEX REPLACE "/[0-9]+[ #]+GetParam\\(\\) = " "/" pretty_test "${test}")
174         else()
175           string(REGEX REPLACE " +#.*" "" pretty_test "${test}")
176         endif()
177         string(REGEX REPLACE "^DISABLED_" "" pretty_test "${pretty_test}")
178         string(REGEX REPLACE " +#.*" "" test "${test}")
179         if(NOT "${arg_TEST_XML_OUTPUT_DIR}" STREQUAL "")
180           set(TEST_XML_OUTPUT_PARAM "--gtest_output=xml:${arg_TEST_XML_OUTPUT_DIR}/${prefix}${suite}.${test}${suffix}.xml")
181         else()
182           unset(TEST_XML_OUTPUT_PARAM)
183         endif()
185         string(CONFIGURE "${test_name_template}" testname)
186         # unescape []
187         if(open_sb)
188           string(REPLACE "${open_sb}" "[" testname "${testname}")
189         endif()
190         if(close_sb)
191           string(REPLACE "${close_sb}" "]" testname "${testname}")
192         endif()
193         set(guarded_testname "${open_guard}${testname}${close_guard}")
195         # Add to script. Do not use add_command() here because it messes up the
196         # handling of empty values when forwarding arguments, and we need to
197         # preserve those carefully for arg_TEST_EXECUTOR and arg_EXTRA_ARGS.
198         string(APPEND script "add_test(${guarded_testname} ${launcherArgs}")
199         foreach(arg IN ITEMS
200           "${arg_TEST_EXECUTABLE}"
201           "--gtest_filter=${suite}.${test}"
202           "--gtest_also_run_disabled_tests"
203           ${TEST_XML_OUTPUT_PARAM}
204           )
205           if(arg MATCHES "[^-./:a-zA-Z0-9_]")
206             string(APPEND script " [==[${arg}]==]")
207           else()
208             string(APPEND script " ${arg}")
209           endif()
210         endforeach()
211         if(arg_TEST_EXTRA_ARGS)
212           list(JOIN arg_TEST_EXTRA_ARGS "]==] [==[" extra_args)
213           string(APPEND script " [==[${extra_args}]==]")
214         endif()
215         string(APPEND script ")\n")
217         set(maybe_disabled "")
218         if(suite MATCHES "^DISABLED_" OR test MATCHES "^DISABLED_")
219           set(maybe_disabled DISABLED TRUE)
220         endif()
222         add_command(set_tests_properties
223           "${guarded_testname}"
224           PROPERTIES
225           ${maybe_disabled}
226           WORKING_DIRECTORY "${arg_TEST_WORKING_DIR}"
227           SKIP_REGULAR_EXPRESSION "\\[  SKIPPED \\]"
228           ${arg_TEST_PROPERTIES}
229         )
231         # possibly unbalanced square brackets render lists invalid so skip such
232         # tests in ${arg_TEST_LIST}
233         if(NOT "${testname}" MATCHES [=[(\[|\])]=])
234           # escape ;
235           string(REPLACE [[;]] [[\\;]] testname "${testname}")
236           list(APPEND tests_buffer "${testname}")
237           list(LENGTH tests_buffer tests_buffer_length)
238           if(tests_buffer_length GREATER "250")
239             # Chunk updates to the final "tests" variable, keeping the
240             # "tests_buffer" variable that we append each test to relatively
241             # small. This mitigates worsening performance impacts for the
242             # corner case of having many thousands of tests.
243             list(APPEND tests "${tests_buffer}")
244             set(tests_buffer "")
245           endif()
246         endif()
247       endif()
249       # If we've built up a sizable script so far, write it out as a chunk now
250       # so we don't accumulate a massive string to write at the end
251       string(LENGTH "${script}" script_len)
252       if(${script_len} GREATER "50000")
253         file(${file_write_mode} "${arg_CTEST_FILE}" "${script}")
254         set(file_write_mode APPEND)
255         set(script "")
256       endif()
258     endif()
259   endforeach()
261   if(NOT tests_buffer STREQUAL "")
262     list(APPEND tests "${tests_buffer}")
263   endif()
265   # Create a list of all discovered tests, which users may use to e.g. set
266   # properties on the tests
267   add_command(set "" ${arg_TEST_LIST} "${tests}")
269   # Write remaining content to the CTest script
270   file(${file_write_mode} "${arg_CTEST_FILE}" "${script}")
272 endfunction()
274 if(CMAKE_SCRIPT_MODE_FILE)
275   gtest_discover_tests_impl(
276     NO_PRETTY_TYPES ${NO_PRETTY_TYPES}
277     NO_PRETTY_VALUES ${NO_PRETTY_VALUES}
278     TEST_EXECUTABLE ${TEST_EXECUTABLE}
279     TEST_EXECUTOR "${TEST_EXECUTOR}"
280     TEST_WORKING_DIR ${TEST_WORKING_DIR}
281     TEST_PREFIX ${TEST_PREFIX}
282     TEST_SUFFIX ${TEST_SUFFIX}
283     TEST_FILTER ${TEST_FILTER}
284     TEST_LIST ${TEST_LIST}
285     CTEST_FILE ${CTEST_FILE}
286     TEST_DISCOVERY_TIMEOUT ${TEST_DISCOVERY_TIMEOUT}
287     TEST_XML_OUTPUT_DIR ${TEST_XML_OUTPUT_DIR}
288     TEST_EXTRA_ARGS "${TEST_EXTRA_ARGS}"
289     TEST_DISCOVERY_EXTRA_ARGS "${TEST_DISCOVERY_EXTRA_ARGS}"
290     TEST_PROPERTIES "${TEST_PROPERTIES}"
291   )
292 endif()