Merge topic 'cpack-innosetup-linux'
[kiteware-cmake.git] / Modules / GoogleTestAddTests.cmake
blobeea267d650970e27be16db60f710d4ffd0b13a64
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}
10 macro(flush_script)
11   file(${flush_tests_MODE} "${_CTEST_FILE}" "${script}")
12   set(flush_tests_MODE APPEND PARENT_SCOPE)
14   set(script "")
15 endmacro()
17 # Flushes tests_buffer to tests
18 macro(flush_tests_buffer)
19   list(APPEND tests "${tests_buffer}")
20   set(tests_buffer "")
21 endmacro()
23 function(add_command NAME TEST_NAME)
24   set(args "")
25   foreach(arg ${ARGN})
26     if(arg MATCHES "[^-./:a-zA-Z0-9_]")
27       string(APPEND args " [==[${arg}]==]")
28     else()
29       string(APPEND args " ${arg}")
30     endif()
31   endforeach()
32   string(APPEND script "${NAME}(${TEST_NAME} ${args})\n")
33   string(LENGTH "${script}" script_len)
34   if(${script_len} GREATER "50000")
35     flush_script()
36   endif()
37   set(script "${script}" PARENT_SCOPE)
38 endfunction()
40 function(generate_testname_guards OUTPUT OPEN_GUARD_VAR CLOSE_GUARD_VAR)
41   set(open_guard "[=[")
42   set(close_guard "]=]")
43   set(counter 1)
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}]")
49   endwhile()
50   set(${OPEN_GUARD_VAR} "${open_guard}" PARENT_SCOPE)
51   set(${CLOSE_GUARD_VAR} "${close_guard}" PARENT_SCOPE)
52 endfunction()
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}_")
59     endwhile()
60     string(REPLACE "${BRACKET}" "${placeholder}" OUTPUT "${OUTPUT}")
61     set(${PLACEHOLDER_VAR} "${placeholder}" PARENT_SCOPE)
62     set(${OUTPUT_VAR} "${OUTPUT}" PARENT_SCOPE)
63   endif()
64 endfunction()
66 function(gtest_discover_tests_impl)
68   cmake_parse_arguments(
69     ""
70     ""
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"
73     ${ARGN}
74   )
76   set(prefix "${_TEST_PREFIX}")
77   set(suffix "${_TEST_SUFFIX}")
78   set(extra_args ${_TEST_EXTRA_ARGS})
79   set(properties ${_TEST_PROPERTIES})
80   set(script)
81   set(suite)
82   set(tests)
83   set(tests_buffer)
85   if(_TEST_FILTER)
86     set(filter "--gtest_filter=${_TEST_FILTER}")
87   else()
88     set(filter)
89   endif()
91   # Run test executable to get list of available tests
92   if(NOT EXISTS "${_TEST_EXECUTABLE}")
93     message(FATAL_ERROR
94       "Specified test executable does not exist.\n"
95       "  Path: '${_TEST_EXECUTABLE}'"
96     )
97   endif()
98   execute_process(
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
104   )
105   if(NOT ${result} EQUAL 0)
106     string(REPLACE "\n" "\n    " output "${output}")
107     if(_TEST_EXECUTOR)
108       set(path "${_TEST_EXECUTOR} ${_TEST_EXECUTABLE}")
109     else()
110       set(path "${_TEST_EXECUTABLE}")
111     endif()
112     message(FATAL_ERROR
113       "Error running test executable.\n"
114       "  Path: '${path}'\n"
115       "  Working directory: '${_TEST_WORKING_DIR}'\n"
116       "  Result: ${result}\n"
117       "  Output:\n"
118       "    ${output}\n"
119     )
120   endif()
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}")
129   # Parse output
130   foreach(line ${output})
131     # Skip header
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}")
137         if(line MATCHES "#")
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}")
141           else()
142             string(REGEX REPLACE ".*/([0-9]+)[ .#]+TypeParam = .*" "\\1" type_parameter "${line}")
143           endif()
144           set(test_name_template "@prefix@@pretty_suite@.@pretty_test@<@type_parameter@>@suffix@")
145         else()
146           set(pretty_suite "${suite}")
147           set(test_name_template "@prefix@@pretty_suite@.@pretty_test@@suffix@")
148         endif()
149         string(REGEX REPLACE "^DISABLED_" "" pretty_suite "${pretty_suite}")
150       else()
151         string(STRIP "${line}" test)
152         if(test MATCHES "#" AND NOT _NO_PRETTY_VALUES)
153           string(REGEX REPLACE "/[0-9]+[ #]+GetParam\\(\\) = " "/" pretty_test "${test}")
154         else()
155           string(REGEX REPLACE " +#.*" "" pretty_test "${test}")
156         endif()
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")
161         else()
162           unset(TEST_XML_OUTPUT_PARAM)
163         endif()
165         string(CONFIGURE "${test_name_template}" testname)
166         # unescape []
167         if(open_sb)
168           string(REPLACE "${open_sb}" "[" testname "${testname}")
169         endif()
170         if(close_sb)
171           string(REPLACE "${close_sb}" "]" testname "${testname}")
172         endif()
173         set(guarded_testname "${open_guard}${testname}${close_guard}")
175         # add to script
176         add_command(add_test
177           "${guarded_testname}"
178           ${_TEST_EXECUTOR}
179           "${_TEST_EXECUTABLE}"
180           "--gtest_filter=${suite}.${test}"
181           "--gtest_also_run_disabled_tests"
182           ${TEST_XML_OUTPUT_PARAM}
183           ${extra_args}
184         )
185         if(suite MATCHES "^DISABLED_" OR test MATCHES "^DISABLED_")
186           add_command(set_tests_properties
187             "${guarded_testname}"
188             PROPERTIES DISABLED TRUE
189           )
190         endif()
192         add_command(set_tests_properties
193           "${guarded_testname}"
194           PROPERTIES
195           WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
196           SKIP_REGULAR_EXPRESSION "\\[  SKIPPED \\]"
197           ${properties}
198         )
200         # possibly unbalanced square brackets render lists invalid so skip such tests in ${_TEST_LIST}
201         if(NOT "${testname}" MATCHES [=[(\[|\])]=])
202           # escape ;
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")
207             flush_tests_buffer()
208           endif()
209         endif()
210       endif()
211     endif()
212   endforeach()
215   # Create a list of all discovered tests, which users may use to e.g. set
216   # properties on the tests
217   flush_tests_buffer()
218   add_command(set "" ${_TEST_LIST} "${tests}")
220   # Write CTest script
221   flush_script()
223 endfunction()
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}
241   )
242 endif()