Imported Upstream version 3.18.2
[platform/upstream/cmake.git] / Modules / GoogleTestAddTests.cmake
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 cmake_minimum_required(VERSION ${CMAKE_VERSION})
5
6 # Overwrite possibly existing ${_CTEST_FILE} with empty file
7 set(flush_tests_MODE WRITE)
8
9 # Flushes script to ${_CTEST_FILE}
10 macro(flush_script)
11   file(${flush_tests_MODE} "${_CTEST_FILE}" "${script}")
12   set(flush_tests_MODE APPEND)
13
14   set(script "")
15 endmacro()
16
17 # Flushes tests_buffer to tests
18 macro(flush_tests_buffer)
19   list(APPEND tests "${tests_buffer}")
20   set(tests_buffer "")
21 endmacro()
22
23 macro(add_command 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}(${_args})\n")
33   string(LENGTH "${script}" _script_len)
34   if(${_script_len} GREATER "50000")
35     flush_script()
36   endif()
37   # Unsets macro local variables to prevent leakage outside of this macro.
38   unset(_args)
39   unset(_script_len)
40 endmacro()
41
42 function(gtest_discover_tests_impl)
43
44   cmake_parse_arguments(
45     ""
46     ""
47     "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"
48     "TEST_EXTRA_ARGS;TEST_PROPERTIES;TEST_EXECUTOR"
49     ${ARGN}
50   )
51
52   set(prefix "${_TEST_PREFIX}")
53   set(suffix "${_TEST_SUFFIX}")
54   set(extra_args ${_TEST_EXTRA_ARGS})
55   set(properties ${_TEST_PROPERTIES})
56   set(script)
57   set(suite)
58   set(tests)
59   set(tests_buffer)
60
61   # Run test executable to get list of available tests
62   if(NOT EXISTS "${_TEST_EXECUTABLE}")
63     message(FATAL_ERROR
64       "Specified test executable does not exist.\n"
65       "  Path: '${_TEST_EXECUTABLE}'"
66     )
67   endif()
68   execute_process(
69     COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" --gtest_list_tests
70     WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
71     TIMEOUT ${_TEST_DISCOVERY_TIMEOUT}
72     OUTPUT_VARIABLE output
73     RESULT_VARIABLE result
74   )
75   if(NOT ${result} EQUAL 0)
76     string(REPLACE "\n" "\n    " output "${output}")
77     message(FATAL_ERROR
78       "Error running test executable.\n"
79       "  Path: '${_TEST_EXECUTABLE}'\n"
80       "  Result: ${result}\n"
81       "  Output:\n"
82       "    ${output}\n"
83     )
84   endif()
85
86   # Preserve semicolon in test-parameters
87   string(REPLACE [[;]] [[\;]] output "${output}")
88   string(REPLACE "\n" ";" output "${output}")
89
90   # Parse output
91   foreach(line ${output})
92     # Skip header
93     if(NOT line MATCHES "gtest_main\\.cc")
94       # Do we have a module name or a test name?
95       if(NOT line MATCHES "^  ")
96         # Module; remove trailing '.' to get just the name...
97         string(REGEX REPLACE "\\.( *#.*)?" "" suite "${line}")
98         if(line MATCHES "#" AND NOT _NO_PRETTY_TYPES)
99           string(REGEX REPLACE "/[0-9]\\.+ +#.*= +" "/" pretty_suite "${line}")
100         else()
101           set(pretty_suite "${suite}")
102         endif()
103         string(REGEX REPLACE "^DISABLED_" "" pretty_suite "${pretty_suite}")
104       else()
105         # Test name; strip spaces and comments to get just the name...
106         string(REGEX REPLACE " +" "" test "${line}")
107         if(test MATCHES "#" AND NOT _NO_PRETTY_VALUES)
108           string(REGEX REPLACE "/[0-9]+#GetParam..=" "/" pretty_test "${test}")
109         else()
110           string(REGEX REPLACE "#.*" "" pretty_test "${test}")
111         endif()
112         string(REGEX REPLACE "^DISABLED_" "" pretty_test "${pretty_test}")
113         string(REGEX REPLACE "#.*" "" test "${test}")
114         if(NOT "${_TEST_XML_OUTPUT_DIR}" STREQUAL "")
115           set(TEST_XML_OUTPUT_PARAM "--gtest_output=xml:${_TEST_XML_OUTPUT_DIR}/${prefix}${suite}.${test}${suffix}.xml")
116         else()
117           unset(TEST_XML_OUTPUT_PARAM)
118         endif()
119
120         # sanitize test name for further processing downstream
121         set(testname "${prefix}${pretty_suite}.${pretty_test}${suffix}")
122         # escape \
123         string(REPLACE [[\]] [[\\]] testname "${testname}")
124         # escape ;
125         string(REPLACE [[;]] [[\;]] testname "${testname}")
126         # escape $
127         string(REPLACE [[$]] [[\$]] testname "${testname}")
128
129         # ...and add to script
130         add_command(add_test
131           "${testname}"
132           ${_TEST_EXECUTOR}
133           "${_TEST_EXECUTABLE}"
134           "--gtest_filter=${suite}.${test}"
135           "--gtest_also_run_disabled_tests"
136           ${TEST_XML_OUTPUT_PARAM}
137           ${extra_args}
138         )
139         if(suite MATCHES "^DISABLED" OR test MATCHES "^DISABLED")
140           add_command(set_tests_properties
141             "${testname}"
142             PROPERTIES DISABLED TRUE
143           )
144         endif()
145         add_command(set_tests_properties
146           "${testname}"
147           PROPERTIES
148           WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
149           SKIP_REGULAR_EXPRESSION "\\\\[  SKIPPED \\\\]"
150           ${properties}
151         )
152         list(APPEND tests_buffer "${testname}")
153         list(LENGTH tests_buffer tests_buffer_length)
154         if(${tests_buffer_length} GREATER "250")
155           flush_tests_buffer()
156         endif()
157       endif()
158     endif()
159   endforeach()
160
161
162   # Create a list of all discovered tests, which users may use to e.g. set
163   # properties on the tests
164   flush_tests_buffer()
165   add_command(set ${_TEST_LIST} ${tests})
166
167   # Write CTest script
168   flush_script()
169
170 endfunction()
171
172 if(CMAKE_SCRIPT_MODE_FILE)
173   gtest_discover_tests_impl(
174     NO_PRETTY_TYPES ${NO_PRETTY_TYPES}
175     NO_PRETTY_VALUES ${NO_PRETTY_VALUES}
176     TEST_EXECUTABLE ${TEST_EXECUTABLE}
177     TEST_EXECUTOR ${TEST_EXECUTOR}
178     TEST_WORKING_DIR ${TEST_WORKING_DIR}
179     TEST_PREFIX ${TEST_PREFIX}
180     TEST_SUFFIX ${TEST_SUFFIX}
181     TEST_LIST ${TEST_LIST}
182     CTEST_FILE ${CTEST_FILE}
183     TEST_DISCOVERY_TIMEOUT ${TEST_DISCOVERY_TIMEOUT}
184     TEST_XML_OUTPUT_DIR ${TEST_XML_OUTPUT_DIR}
185     TEST_EXTRA_ARGS ${TEST_EXTRA_ARGS}
186     TEST_PROPERTIES ${TEST_PROPERTIES}
187   )
188 endif()