GoogleTestAddTests.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. cmake_minimum_required(VERSION ${CMAKE_VERSION})
  4. # Overwrite possibly existing ${_CTEST_FILE} with empty file
  5. set(flush_tests_MODE WRITE)
  6. # Flushes script to ${_CTEST_FILE}
  7. macro(flush_script)
  8. file(${flush_tests_MODE} "${_CTEST_FILE}" "${script}")
  9. set(flush_tests_MODE APPEND)
  10. set(script "")
  11. endmacro()
  12. # Flushes tests_buffer to tests
  13. macro(flush_tests_buffer)
  14. list(APPEND tests "${tests_buffer}")
  15. set(tests_buffer "")
  16. endmacro()
  17. macro(add_command NAME TEST_NAME)
  18. set(_args "")
  19. foreach(_arg ${ARGN})
  20. if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
  21. string(APPEND _args " [==[${_arg}]==]")
  22. else()
  23. string(APPEND _args " ${_arg}")
  24. endif()
  25. endforeach()
  26. string(APPEND script "${NAME}(${TEST_NAME} ${_args})\n")
  27. string(LENGTH "${script}" _script_len)
  28. if(${_script_len} GREATER "50000")
  29. flush_script()
  30. endif()
  31. # Unsets macro local variables to prevent leakage outside of this macro.
  32. unset(_args)
  33. unset(_script_len)
  34. endmacro()
  35. function(generate_testname_guards OUTPUT OPEN_GUARD_VAR CLOSE_GUARD_VAR)
  36. set(open_guard "[=[")
  37. set(close_guard "]=]")
  38. set(counter 1)
  39. while("${OUTPUT}" MATCHES "${close_guard}")
  40. math(EXPR counter "${counter} + 1")
  41. string(REPEAT "=" ${counter} equals)
  42. set(open_guard "[${equals}[")
  43. set(close_guard "]${equals}]")
  44. endwhile()
  45. set(${OPEN_GUARD_VAR} "${open_guard}" PARENT_SCOPE)
  46. set(${CLOSE_GUARD_VAR} "${close_guard}" PARENT_SCOPE)
  47. endfunction()
  48. function(escape_square_brackets OUTPUT BRACKET PLACEHOLDER PLACEHOLDER_VAR OUTPUT_VAR)
  49. if("${OUTPUT}" MATCHES "\\${BRACKET}")
  50. set(placeholder "${PLACEHOLDER}")
  51. while("${OUTPUT}" MATCHES "${placeholder}")
  52. set(placeholder "${placeholder}_")
  53. endwhile()
  54. string(REPLACE "${BRACKET}" "${placeholder}" OUTPUT "${OUTPUT}")
  55. set(${PLACEHOLDER_VAR} "${placeholder}" PARENT_SCOPE)
  56. set(${OUTPUT_VAR} "${OUTPUT}" PARENT_SCOPE)
  57. endif()
  58. endfunction()
  59. function(gtest_discover_tests_impl)
  60. cmake_parse_arguments(
  61. ""
  62. ""
  63. "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"
  64. "TEST_EXTRA_ARGS;TEST_PROPERTIES;TEST_EXECUTOR"
  65. ${ARGN}
  66. )
  67. set(prefix "${_TEST_PREFIX}")
  68. set(suffix "${_TEST_SUFFIX}")
  69. set(extra_args ${_TEST_EXTRA_ARGS})
  70. set(properties ${_TEST_PROPERTIES})
  71. set(script)
  72. set(suite)
  73. set(tests)
  74. set(tests_buffer)
  75. if(_TEST_FILTER)
  76. set(filter "--gtest_filter=${_TEST_FILTER}")
  77. else()
  78. set(filter)
  79. endif()
  80. # Run test executable to get list of available tests
  81. if(NOT EXISTS "${_TEST_EXECUTABLE}")
  82. message(FATAL_ERROR
  83. "Specified test executable does not exist.\n"
  84. " Path: '${_TEST_EXECUTABLE}'"
  85. )
  86. endif()
  87. execute_process(
  88. COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" --gtest_list_tests ${filter}
  89. WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
  90. TIMEOUT ${_TEST_DISCOVERY_TIMEOUT}
  91. OUTPUT_VARIABLE output
  92. RESULT_VARIABLE result
  93. )
  94. if(NOT ${result} EQUAL 0)
  95. string(REPLACE "\n" "\n " output "${output}")
  96. if(_TEST_EXECUTOR)
  97. set(path "${_TEST_EXECUTOR} ${_TEST_EXECUTABLE}")
  98. else()
  99. set(path "${_TEST_EXECUTABLE}")
  100. endif()
  101. message(FATAL_ERROR
  102. "Error running test executable.\n"
  103. " Path: '${path}'\n"
  104. " Result: ${result}\n"
  105. " Output:\n"
  106. " ${output}\n"
  107. )
  108. endif()
  109. generate_testname_guards("${output}" open_guard close_guard)
  110. escape_square_brackets("${output}" "[" "__osb" open_sb output)
  111. escape_square_brackets("${output}" "]" "__csb" close_sb output)
  112. # Preserve semicolon in test-parameters
  113. string(REPLACE [[;]] [[\;]] output "${output}")
  114. string(REPLACE "\n" ";" output "${output}")
  115. # Parse output
  116. foreach(line ${output})
  117. # Skip header
  118. if(NOT line MATCHES "gtest_main\\.cc")
  119. # Do we have a module name or a test name?
  120. if(NOT line MATCHES "^ ")
  121. # Module; remove trailing '.' to get just the name...
  122. string(REGEX REPLACE "\\.( *#.*)?$" "" suite "${line}")
  123. if(line MATCHES "#" AND NOT _NO_PRETTY_TYPES)
  124. string(REGEX REPLACE "/[0-9]\\.+ +#.*= +" "/" pretty_suite "${line}")
  125. else()
  126. set(pretty_suite "${suite}")
  127. endif()
  128. string(REGEX REPLACE "^DISABLED_" "" pretty_suite "${pretty_suite}")
  129. else()
  130. # Test name; strip spaces and comments to get just the name...
  131. string(REGEX REPLACE " +" "" test "${line}")
  132. if(test MATCHES "#" AND NOT _NO_PRETTY_VALUES)
  133. string(REGEX REPLACE "/[0-9]+#GetParam..=" "/" pretty_test "${test}")
  134. else()
  135. string(REGEX REPLACE "#.*" "" pretty_test "${test}")
  136. endif()
  137. string(REGEX REPLACE "^DISABLED_" "" pretty_test "${pretty_test}")
  138. string(REGEX REPLACE "#.*" "" test "${test}")
  139. if(NOT "${_TEST_XML_OUTPUT_DIR}" STREQUAL "")
  140. set(TEST_XML_OUTPUT_PARAM "--gtest_output=xml:${_TEST_XML_OUTPUT_DIR}/${prefix}${suite}.${test}${suffix}.xml")
  141. else()
  142. unset(TEST_XML_OUTPUT_PARAM)
  143. endif()
  144. set(testname "${prefix}${pretty_suite}.${pretty_test}${suffix}")
  145. # sanitize test name for further processing downstream
  146. # unescape []
  147. if(open_sb)
  148. string(REPLACE "${open_sb}" "[" testname "${testname}")
  149. endif()
  150. if(close_sb)
  151. string(REPLACE "${close_sb}" "]" testname "${testname}")
  152. endif()
  153. # escape \
  154. string(REPLACE [[\]] [[\\]] testname "${testname}")
  155. # escape $
  156. string(REPLACE [[$]] [[\$]] testname "${testname}")
  157. set(guarded_testname "${open_guard}${testname}${close_guard}")
  158. # ...and add to script
  159. add_command(add_test
  160. "${guarded_testname}"
  161. ${_TEST_EXECUTOR}
  162. "${_TEST_EXECUTABLE}"
  163. "--gtest_filter=${suite}.${test}"
  164. "--gtest_also_run_disabled_tests"
  165. ${TEST_XML_OUTPUT_PARAM}
  166. ${extra_args}
  167. )
  168. if(suite MATCHES "^DISABLED_" OR test MATCHES "^DISABLED_")
  169. add_command(set_tests_properties
  170. "${guarded_testname}"
  171. PROPERTIES DISABLED TRUE
  172. )
  173. endif()
  174. add_command(set_tests_properties
  175. "${guarded_testname}"
  176. PROPERTIES
  177. WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
  178. SKIP_REGULAR_EXPRESSION "\\\\[ SKIPPED \\\\]"
  179. ${properties}
  180. )
  181. # possibly unbalanced square brackets render lists invalid so skip such tests in _TEST_LIST
  182. if(NOT "${testname}" MATCHES [=[(\[|\])]=])
  183. # escape ;
  184. string(REPLACE [[;]] [[\;]] testname "${testname}")
  185. list(APPEND tests_buffer "${testname}")
  186. list(LENGTH tests_buffer tests_buffer_length)
  187. if(${tests_buffer_length} GREATER "250")
  188. flush_tests_buffer()
  189. endif()
  190. endif()
  191. endif()
  192. endif()
  193. endforeach()
  194. # Create a list of all discovered tests, which users may use to e.g. set
  195. # properties on the tests
  196. flush_tests_buffer()
  197. add_command(set "" ${_TEST_LIST} ${tests})
  198. # Write CTest script
  199. flush_script()
  200. endfunction()
  201. if(CMAKE_SCRIPT_MODE_FILE)
  202. gtest_discover_tests_impl(
  203. NO_PRETTY_TYPES ${NO_PRETTY_TYPES}
  204. NO_PRETTY_VALUES ${NO_PRETTY_VALUES}
  205. TEST_EXECUTABLE ${TEST_EXECUTABLE}
  206. TEST_EXECUTOR ${TEST_EXECUTOR}
  207. TEST_WORKING_DIR ${TEST_WORKING_DIR}
  208. TEST_PREFIX ${TEST_PREFIX}
  209. TEST_SUFFIX ${TEST_SUFFIX}
  210. TEST_FILTER ${TEST_FILTER}
  211. TEST_LIST ${TEST_LIST}
  212. CTEST_FILE ${CTEST_FILE}
  213. TEST_DISCOVERY_TIMEOUT ${TEST_DISCOVERY_TIMEOUT}
  214. TEST_XML_OUTPUT_DIR ${TEST_XML_OUTPUT_DIR}
  215. TEST_EXTRA_ARGS ${TEST_EXTRA_ARGS}
  216. TEST_PROPERTIES ${TEST_PROPERTIES}
  217. )
  218. endif()