GoogleTestAddTests.cmake 5.9 KB

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