GoogleTestAddTests.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. string(STRIP "${line}" test)
  131. if(test MATCHES "#" AND NOT _NO_PRETTY_VALUES)
  132. string(REGEX REPLACE "/[0-9]+[ #]+GetParam\\(\\) = " "/" pretty_test "${test}")
  133. else()
  134. string(REGEX REPLACE " +#.*" "" pretty_test "${test}")
  135. endif()
  136. string(REGEX REPLACE "^DISABLED_" "" pretty_test "${pretty_test}")
  137. string(REGEX REPLACE " +#.*" "" test "${test}")
  138. if(NOT "${_TEST_XML_OUTPUT_DIR}" STREQUAL "")
  139. set(TEST_XML_OUTPUT_PARAM "--gtest_output=xml:${_TEST_XML_OUTPUT_DIR}/${prefix}${suite}.${test}${suffix}.xml")
  140. else()
  141. unset(TEST_XML_OUTPUT_PARAM)
  142. endif()
  143. set(testname "${prefix}${pretty_suite}.${pretty_test}${suffix}")
  144. # sanitize test name for further processing downstream
  145. # unescape []
  146. if(open_sb)
  147. string(REPLACE "${open_sb}" "[" testname "${testname}")
  148. endif()
  149. if(close_sb)
  150. string(REPLACE "${close_sb}" "]" testname "${testname}")
  151. endif()
  152. # escape \
  153. string(REPLACE [[\]] [[\\]] testname "${testname}")
  154. # escape $
  155. string(REPLACE [[$]] [[\$]] testname "${testname}")
  156. set(guarded_testname "${open_guard}${testname}${close_guard}")
  157. # ...and add to script
  158. add_command(add_test
  159. "${guarded_testname}"
  160. ${_TEST_EXECUTOR}
  161. "${_TEST_EXECUTABLE}"
  162. "--gtest_filter=${suite}.${test}"
  163. "--gtest_also_run_disabled_tests"
  164. ${TEST_XML_OUTPUT_PARAM}
  165. ${extra_args}
  166. )
  167. if(suite MATCHES "^DISABLED_" OR test MATCHES "^DISABLED_")
  168. add_command(set_tests_properties
  169. "${guarded_testname}"
  170. PROPERTIES DISABLED TRUE
  171. )
  172. endif()
  173. add_command(set_tests_properties
  174. "${guarded_testname}"
  175. PROPERTIES
  176. WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
  177. SKIP_REGULAR_EXPRESSION "\\\\[ SKIPPED \\\\]"
  178. ${properties}
  179. )
  180. # possibly unbalanced square brackets render lists invalid so skip such tests in _TEST_LIST
  181. if(NOT "${testname}" MATCHES [=[(\[|\])]=])
  182. # escape ;
  183. string(REPLACE [[;]] [[\;]] testname "${testname}")
  184. list(APPEND tests_buffer "${testname}")
  185. list(LENGTH tests_buffer tests_buffer_length)
  186. if(${tests_buffer_length} GREATER "250")
  187. flush_tests_buffer()
  188. endif()
  189. endif()
  190. endif()
  191. endif()
  192. endforeach()
  193. # Create a list of all discovered tests, which users may use to e.g. set
  194. # properties on the tests
  195. flush_tests_buffer()
  196. add_command(set "" ${_TEST_LIST} ${tests})
  197. # Write CTest script
  198. flush_script()
  199. endfunction()
  200. if(CMAKE_SCRIPT_MODE_FILE)
  201. gtest_discover_tests_impl(
  202. NO_PRETTY_TYPES ${NO_PRETTY_TYPES}
  203. NO_PRETTY_VALUES ${NO_PRETTY_VALUES}
  204. TEST_EXECUTABLE ${TEST_EXECUTABLE}
  205. TEST_EXECUTOR ${TEST_EXECUTOR}
  206. TEST_WORKING_DIR ${TEST_WORKING_DIR}
  207. TEST_PREFIX ${TEST_PREFIX}
  208. TEST_SUFFIX ${TEST_SUFFIX}
  209. TEST_FILTER ${TEST_FILTER}
  210. TEST_LIST ${TEST_LIST}
  211. CTEST_FILE ${CTEST_FILE}
  212. TEST_DISCOVERY_TIMEOUT ${TEST_DISCOVERY_TIMEOUT}
  213. TEST_XML_OUTPUT_DIR ${TEST_XML_OUTPUT_DIR}
  214. TEST_EXTRA_ARGS ${TEST_EXTRA_ARGS}
  215. TEST_PROPERTIES ${TEST_PROPERTIES}
  216. )
  217. endif()