GoogleTestAddTests.cmake 5.8 KB

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