GoogleTestAddTests.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. cmake_minimum_required(VERSION 3.30)
  4. cmake_policy(SET CMP0174 NEW) # TODO: Remove this when we can update the above to 3.31
  5. function(add_command name test_name)
  6. set(args "")
  7. foreach(arg ${ARGN})
  8. if(arg MATCHES "[^-./:a-zA-Z0-9_]")
  9. string(APPEND args " [==[${arg}]==]")
  10. else()
  11. string(APPEND args " ${arg}")
  12. endif()
  13. endforeach()
  14. string(APPEND script "${name}(${test_name} ${args})\n")
  15. set(script "${script}" PARENT_SCOPE)
  16. endfunction()
  17. function(generate_testname_guards output open_guard_var close_guard_var)
  18. set(open_guard "[=[")
  19. set(close_guard "]=]")
  20. set(counter 1)
  21. while("${output}" MATCHES "${close_guard}")
  22. math(EXPR counter "${counter} + 1")
  23. string(REPEAT "=" ${counter} equals)
  24. set(open_guard "[${equals}[")
  25. set(close_guard "]${equals}]")
  26. endwhile()
  27. set(${open_guard_var} "${open_guard}" PARENT_SCOPE)
  28. set(${close_guard_var} "${close_guard}" PARENT_SCOPE)
  29. endfunction()
  30. function(escape_square_brackets output bracket placeholder placeholder_var output_var)
  31. if("${output}" MATCHES "\\${bracket}")
  32. set(placeholder "${placeholder}")
  33. while("${output}" MATCHES "${placeholder}")
  34. set(placeholder "${placeholder}_")
  35. endwhile()
  36. string(REPLACE "${bracket}" "${placeholder}" output "${output}")
  37. set(${placeholder_var} "${placeholder}" PARENT_SCOPE)
  38. set(${output_var} "${output}" PARENT_SCOPE)
  39. endif()
  40. endfunction()
  41. macro(write_test_to_file)
  42. # Store the gtest test name before messing with these strings
  43. set(gtest_name ${current_test_suite}.${current_test_name})
  44. set(pretty_test_suite ${current_test_suite})
  45. set(pretty_test_name ${current_test_name})
  46. # Handle disabled tests
  47. set(maybe_DISABLED "")
  48. if(pretty_test_suite MATCHES "^DISABLED_" OR pretty_test_name MATCHES "^DISABLED_")
  49. set(maybe_DISABLED DISABLED YES)
  50. string(REGEX REPLACE "^DISABLED_" "" pretty_test_suite "${pretty_test_suite}")
  51. string(REGEX REPLACE "^DISABLED_" "" pretty_test_name "${pretty_test_name}")
  52. endif()
  53. if (NOT current_test_value_param STREQUAL "" AND NOT arg_NO_PRETTY_VALUES)
  54. # Remove value param name, if any, from test name
  55. string(REGEX REPLACE "^(.+)/.+$" "\\1" pretty_test_name "${pretty_test_name}")
  56. set(pretty_test_name "${pretty_test_name}/${current_test_value_param}")
  57. endif()
  58. if(NOT current_test_type_param STREQUAL "")
  59. # Parse type param name from suite name
  60. if(pretty_test_suite MATCHES "^(.+)/(.+)$")
  61. set(pretty_test_suite "${CMAKE_MATCH_1}")
  62. set(current_type_param_name "${CMAKE_MATCH_2}")
  63. else()
  64. set(current_type_param_name "")
  65. endif()
  66. if (NOT arg_NO_PRETTY_TYPES)
  67. string(APPEND pretty_test_name "<${current_test_type_param}>")
  68. elseif(NOT current_type_param_name STREQUAL "")
  69. string(APPEND pretty_test_name "<${current_type_param_name}>")
  70. endif()
  71. endif()
  72. set(test_name_template "@prefix@@pretty_test_suite@.@pretty_test_name@@suffix@")
  73. string(CONFIGURE "${test_name_template}" testname)
  74. if(NOT "${arg_TEST_XML_OUTPUT_DIR}" STREQUAL "")
  75. set(TEST_XML_OUTPUT_PARAM "--gtest_output=xml:${arg_TEST_XML_OUTPUT_DIR}/${prefix}${gtest_name}${suffix}.xml")
  76. else()
  77. set(TEST_XML_OUTPUT_PARAM "")
  78. endif()
  79. # unescape []
  80. if(open_sb)
  81. string(REPLACE "${open_sb}" "[" testname "${testname}")
  82. endif()
  83. if(close_sb)
  84. string(REPLACE "${close_sb}" "]" testname "${testname}")
  85. endif()
  86. set(guarded_testname "${open_guard}${testname}${close_guard}")
  87. # Add to script. Do not use add_command() here because it messes up the
  88. # handling of empty values when forwarding arguments, and we need to
  89. # preserve those carefully for arg_TEST_EXECUTOR and arg_EXTRA_ARGS.
  90. string(APPEND script "add_test(${guarded_testname} ${launcherArgs}")
  91. foreach(arg IN ITEMS
  92. "${arg_TEST_EXECUTABLE}"
  93. "--gtest_filter=${gtest_name}"
  94. "--gtest_also_run_disabled_tests"
  95. ${TEST_XML_OUTPUT_PARAM}
  96. )
  97. if(arg MATCHES "[^-./:a-zA-Z0-9_]")
  98. string(APPEND script " [==[${arg}]==]")
  99. else()
  100. string(APPEND script " ${arg}")
  101. endif()
  102. endforeach()
  103. if(arg_TEST_EXTRA_ARGS)
  104. list(JOIN arg_TEST_EXTRA_ARGS "]==] [==[" extra_args)
  105. string(APPEND script " [==[${extra_args}]==]")
  106. endif()
  107. string(APPEND script ")\n")
  108. set(maybe_LOCATION "")
  109. if(NOT current_test_file STREQUAL "" AND NOT current_test_line STREQUAL "")
  110. set(maybe_LOCATION DEF_SOURCE_LINE "${current_test_file}:${current_test_line}")
  111. endif()
  112. add_command(set_tests_properties
  113. "${guarded_testname}"
  114. PROPERTIES
  115. ${maybe_DISABLED}
  116. ${maybe_LOCATION}
  117. WORKING_DIRECTORY "${arg_TEST_WORKING_DIR}"
  118. SKIP_REGULAR_EXPRESSION "\\[ SKIPPED \\]"
  119. ${arg_TEST_PROPERTIES}
  120. )
  121. # possibly unbalanced square brackets render lists invalid so skip such
  122. # tests in ${arg_TEST_LIST}
  123. if(NOT "${testname}" MATCHES [=[(\[|\])]=])
  124. # escape ;
  125. string(REPLACE [[;]] [[\\;]] testname "${testname}")
  126. list(APPEND tests_buffer "${testname}")
  127. list(LENGTH tests_buffer tests_buffer_length)
  128. if(tests_buffer_length GREATER "250")
  129. # Chunk updates to the final "tests" variable, keeping the
  130. # "tests_buffer" variable that we append each test to relatively
  131. # small. This mitigates worsening performance impacts for the
  132. # corner case of having many thousands of tests.
  133. list(APPEND tests "${tests_buffer}")
  134. set(tests_buffer "")
  135. endif()
  136. endif()
  137. # If we've built up a sizable script so far, write it out as a chunk now
  138. # so we don't accumulate a massive string to write at the end
  139. string(LENGTH "${script}" script_len)
  140. if(${script_len} GREATER "50000")
  141. file(APPEND "${arg_CTEST_FILE}" "${script}")
  142. set(script "")
  143. endif()
  144. endmacro()
  145. macro(parse_tests_from_output)
  146. generate_testname_guards("${output}" open_guard close_guard)
  147. escape_square_brackets("${output}" "[" "__osb" open_sb output)
  148. escape_square_brackets("${output}" "]" "__csb" close_sb output)
  149. # Preserve semicolon in test-parameters
  150. string(REPLACE [[;]] [[\;]] output "${output}")
  151. string(REPLACE "\n" ";" output "${output}")
  152. # Command line output doesn't contain information about the file and line number of the tests
  153. set(current_test_file "")
  154. set(current_test_line "")
  155. # Parse output
  156. foreach(line ${output})
  157. # Skip header
  158. if(line MATCHES "gtest_main\\.cc")
  159. continue()
  160. endif()
  161. if(line STREQUAL "")
  162. continue()
  163. endif()
  164. # Do we have a module name or a test name?
  165. if(NOT line MATCHES "^ ")
  166. set(current_test_type_param "")
  167. # Module; remove trailing '.' to get just the name...
  168. string(REGEX REPLACE "\\.( *#.*)?$" "" current_test_suite "${line}")
  169. if(line MATCHES "# *TypeParam = (.*)$")
  170. set(current_test_type_param "${CMAKE_MATCH_1}")
  171. endif()
  172. else()
  173. string(STRIP "${line}" test)
  174. string(REGEX REPLACE " ( *#.*)?$" "" current_test_name "${test}")
  175. set(current_test_value_param "")
  176. if(line MATCHES "# *GetParam\\(\\) = (.*)$")
  177. set(current_test_value_param "${CMAKE_MATCH_1}")
  178. endif()
  179. write_test_to_file()
  180. endif()
  181. endforeach()
  182. endmacro()
  183. macro(get_json_member_with_default json_variable member_name out_variable)
  184. string(JSON ${out_variable}
  185. ERROR_VARIABLE error_param
  186. GET "${${json_variable}}" "${member_name}"
  187. )
  188. if(error_param)
  189. # Member not present
  190. set(${out_variable} "")
  191. endif()
  192. endmacro()
  193. macro(parse_tests_from_json json_file)
  194. if(NOT EXISTS "${json_file}")
  195. message(FATAL_ERROR "Missing expected JSON file with test list: ${json_file}")
  196. endif()
  197. file(READ "${json_file}" test_json)
  198. string(JSON test_suites_json GET "${test_json}" "testsuites")
  199. # Return if there are no testsuites
  200. string(JSON len_test_suites LENGTH "${test_suites_json}")
  201. if(len_test_suites GREATER 0)
  202. set(open_sb)
  203. set(close_sb)
  204. math(EXPR upper_limit_test_suite_range "${len_test_suites} - 1")
  205. foreach(index_test_suite RANGE ${upper_limit_test_suite_range})
  206. string(JSON test_suite_json GET "${test_suites_json}" ${index_test_suite})
  207. # "suite" is expected to be set in write_test_to_file(). When parsing the
  208. # plain text output, "suite" is expected to be the original suite name
  209. # before accounting for pretty names. This may be used to construct the
  210. # name of XML output results files.
  211. string(JSON current_test_suite GET "${test_suite_json}" "name")
  212. string(JSON tests_json GET "${test_suite_json}" "testsuite")
  213. # Skip test suites without tests
  214. string(JSON len_tests LENGTH "${tests_json}")
  215. if(len_tests LESS_EQUAL 0)
  216. continue()
  217. endif()
  218. math(EXPR upper_limit_test_range "${len_tests} - 1")
  219. foreach(index_test RANGE ${upper_limit_test_range})
  220. string(JSON test_json GET "${tests_json}" ${index_test})
  221. string(JSON len_test_parameters LENGTH "${test_json}")
  222. if(len_test_parameters LESS_EQUAL 0)
  223. continue()
  224. endif()
  225. get_json_member_with_default(test_json "name" current_test_name)
  226. get_json_member_with_default(test_json "file" current_test_file)
  227. get_json_member_with_default(test_json "line" current_test_line)
  228. get_json_member_with_default(test_json "value_param" current_test_value_param)
  229. get_json_member_with_default(test_json "type_param" current_test_type_param)
  230. generate_testname_guards(
  231. "${current_test_suite}${current_test_name}${current_test_value_param}${current_test_type_param}"
  232. open_guard close_guard
  233. )
  234. write_test_to_file()
  235. endforeach()
  236. endforeach()
  237. endif()
  238. endmacro()
  239. function(gtest_discover_tests_impl)
  240. set(options "")
  241. set(oneValueArgs
  242. NO_PRETTY_TYPES # These two take a value, unlike gtest_discover_tests()
  243. NO_PRETTY_VALUES #
  244. TEST_EXECUTABLE
  245. TEST_WORKING_DIR
  246. TEST_PREFIX
  247. TEST_SUFFIX
  248. TEST_LIST
  249. CTEST_FILE
  250. TEST_DISCOVERY_TIMEOUT
  251. TEST_XML_OUTPUT_DIR
  252. # The following are all multi-value arguments in gtest_discover_tests(),
  253. # but they are each given to us as a single argument. We parse them that
  254. # way to avoid problems with preserving empty list values and escaping.
  255. TEST_FILTER
  256. TEST_EXTRA_ARGS
  257. TEST_DISCOVERY_EXTRA_ARGS
  258. TEST_PROPERTIES
  259. TEST_EXECUTOR
  260. )
  261. set(multiValueArgs "")
  262. cmake_parse_arguments(PARSE_ARGV 0 arg
  263. "${options}" "${oneValueArgs}" "${multiValueArgs}"
  264. )
  265. set(prefix "${arg_TEST_PREFIX}")
  266. set(suffix "${arg_TEST_SUFFIX}")
  267. set(script)
  268. set(tests)
  269. set(tests_buffer "")
  270. # If a file at ${arg_CTEST_FILE} already exists, we overwrite it.
  271. file(REMOVE "${arg_CTEST_FILE}")
  272. set(filter)
  273. if(arg_TEST_FILTER)
  274. set(filter "--gtest_filter=${arg_TEST_FILTER}")
  275. endif()
  276. # CMP0178 has already been handled in gtest_discover_tests(), so we only need
  277. # to implement NEW behavior here. This means preserving empty arguments for
  278. # TEST_EXECUTOR. For OLD or WARN, gtest_discover_tests() already removed any
  279. # empty arguments.
  280. set(launcherArgs "")
  281. if(NOT "${arg_TEST_EXECUTOR}" STREQUAL "")
  282. list(JOIN arg_TEST_EXECUTOR "]==] [==[" launcherArgs)
  283. set(launcherArgs "[==[${launcherArgs}]==]")
  284. endif()
  285. # Run test executable to get list of available tests
  286. if(NOT EXISTS "${arg_TEST_EXECUTABLE}")
  287. message(FATAL_ERROR
  288. "Specified test executable does not exist.\n"
  289. " Path: '${arg_TEST_EXECUTABLE}'"
  290. )
  291. endif()
  292. set(discovery_extra_args "")
  293. if(NOT "${arg_TEST_DISCOVERY_EXTRA_ARGS}" STREQUAL "")
  294. list(JOIN arg_TEST_DISCOVERY_EXTRA_ARGS "]==] [==[" discovery_extra_args)
  295. set(discovery_extra_args "[==[${discovery_extra_args}]==]")
  296. endif()
  297. set(json_file "${arg_TEST_WORKING_DIR}/cmake_test_discovery.json")
  298. # Remove json file to make sure we don't pick up an outdated one
  299. file(REMOVE "${json_file}")
  300. cmake_language(EVAL CODE
  301. "execute_process(
  302. COMMAND ${launcherArgs} [==[${arg_TEST_EXECUTABLE}]==]
  303. --gtest_list_tests
  304. [==[--gtest_output=json:${json_file}]==]
  305. ${filter}
  306. ${discovery_extra_args}
  307. WORKING_DIRECTORY [==[${arg_TEST_WORKING_DIR}]==]
  308. TIMEOUT ${arg_TEST_DISCOVERY_TIMEOUT}
  309. OUTPUT_VARIABLE output
  310. RESULT_VARIABLE result
  311. )"
  312. )
  313. if(NOT ${result} EQUAL 0)
  314. string(REPLACE "\n" "\n " output "${output}")
  315. if(arg_TEST_EXECUTOR)
  316. set(path "${arg_TEST_EXECUTOR} ${arg_TEST_EXECUTABLE}")
  317. else()
  318. set(path "${arg_TEST_EXECUTABLE}")
  319. endif()
  320. message(FATAL_ERROR
  321. "Error running test executable.\n"
  322. " Path: '${path}'\n"
  323. " Working directory: '${arg_TEST_WORKING_DIR}'\n"
  324. " Result: ${result}\n"
  325. " Output:\n"
  326. " ${output}\n"
  327. )
  328. endif()
  329. if(EXISTS "${json_file}")
  330. parse_tests_from_json("${json_file}")
  331. else()
  332. # gtest < 1.8.1, and all gtest compiled with GTEST_HAS_FILE_SYSTEM=0, don't
  333. # recognize the --gtest_output=json option, and issue a warning or error on
  334. # stdout about it being unrecognized, but still return an exit code 0 for
  335. # success. All versions report the test list on stdout whether
  336. # --gtest_output=json is recognized or not.
  337. # NOTE: Because we are calling a macro, we don't want to pass "output" as
  338. # an argument because it messes up the contents passed through due to the
  339. # different escaping, etc. that gets applied. We rely on it picking up the
  340. # "output" variable we have already set here.
  341. parse_tests_from_output()
  342. endif()
  343. if(NOT tests_buffer STREQUAL "")
  344. list(APPEND tests "${tests_buffer}")
  345. endif()
  346. # Create a list of all discovered tests, which users may use to e.g. set
  347. # properties on the tests
  348. add_command(set "" ${arg_TEST_LIST} "${tests}")
  349. # Write remaining content to the CTest script
  350. file(APPEND "${arg_CTEST_FILE}" "${script}")
  351. endfunction()
  352. if(CMAKE_SCRIPT_MODE_FILE)
  353. gtest_discover_tests_impl(
  354. NO_PRETTY_TYPES ${NO_PRETTY_TYPES}
  355. NO_PRETTY_VALUES ${NO_PRETTY_VALUES}
  356. TEST_EXECUTABLE ${TEST_EXECUTABLE}
  357. TEST_EXECUTOR "${TEST_EXECUTOR}"
  358. TEST_WORKING_DIR ${TEST_WORKING_DIR}
  359. TEST_PREFIX ${TEST_PREFIX}
  360. TEST_SUFFIX ${TEST_SUFFIX}
  361. TEST_FILTER ${TEST_FILTER}
  362. TEST_LIST ${TEST_LIST}
  363. CTEST_FILE ${CTEST_FILE}
  364. TEST_DISCOVERY_TIMEOUT ${TEST_DISCOVERY_TIMEOUT}
  365. TEST_XML_OUTPUT_DIR ${TEST_XML_OUTPUT_DIR}
  366. TEST_EXTRA_ARGS "${TEST_EXTRA_ARGS}"
  367. TEST_DISCOVERY_EXTRA_ARGS "${TEST_DISCOVERY_EXTRA_ARGS}"
  368. TEST_PROPERTIES "${TEST_PROPERTIES}"
  369. )
  370. endif()