GoogleTestAddTests.cmake 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. set(prefix "${TEST_PREFIX}")
  4. set(suffix "${TEST_SUFFIX}")
  5. set(extra_args ${TEST_EXTRA_ARGS})
  6. set(properties ${TEST_PROPERTIES})
  7. set(script)
  8. set(suite)
  9. set(tests)
  10. function(add_command NAME)
  11. set(_args "")
  12. foreach(_arg ${ARGN})
  13. if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
  14. set(_args "${_args} [==[${_arg}]==]")
  15. else()
  16. set(_args "${_args} ${_arg}")
  17. endif()
  18. endforeach()
  19. set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
  20. endfunction()
  21. # Run test executable to get list of available tests
  22. if(NOT EXISTS "${TEST_EXECUTABLE}")
  23. message(FATAL_ERROR
  24. "Specified test executable '${TEST_EXECUTABLE}' does not exist"
  25. )
  26. endif()
  27. execute_process(
  28. COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" --gtest_list_tests
  29. OUTPUT_VARIABLE output
  30. RESULT_VARIABLE result
  31. )
  32. if(NOT ${result} EQUAL 0)
  33. message(FATAL_ERROR
  34. "Error running test executable '${TEST_EXECUTABLE}':\n"
  35. " Result: ${result}\n"
  36. " Output: ${output}\n"
  37. )
  38. endif()
  39. string(REPLACE "\n" ";" output "${output}")
  40. # Parse output
  41. foreach(line ${output})
  42. # Skip header
  43. if(NOT line MATCHES "gtest_main\\.cc")
  44. # Do we have a module name or a test name?
  45. if(NOT line MATCHES "^ ")
  46. # Module; remove trailing '.' to get just the name...
  47. string(REGEX REPLACE "\\.( *#.*)?" "" suite "${line}")
  48. if(line MATCHES "#" AND NOT NO_PRETTY_TYPES)
  49. string(REGEX REPLACE "/[0-9]\\.+ +#.*= +" "/" pretty_suite "${line}")
  50. else()
  51. set(pretty_suite "${suite}")
  52. endif()
  53. string(REGEX REPLACE "^DISABLED_" "" pretty_suite "${pretty_suite}")
  54. else()
  55. # Test name; strip spaces and comments to get just the name...
  56. string(REGEX REPLACE " +" "" test "${line}")
  57. if(test MATCHES "#" AND NOT NO_PRETTY_VALUES)
  58. string(REGEX REPLACE "/[0-9]+#GetParam..=" "/" pretty_test "${test}")
  59. else()
  60. string(REGEX REPLACE "#.*" "" pretty_test "${test}")
  61. endif()
  62. string(REGEX REPLACE "^DISABLED_" "" pretty_test "${pretty_test}")
  63. string(REGEX REPLACE "#.*" "" test "${test}")
  64. # ...and add to script
  65. add_command(add_test
  66. "${prefix}${pretty_suite}.${pretty_test}${suffix}"
  67. ${TEST_EXECUTOR}
  68. "${TEST_EXECUTABLE}"
  69. "--gtest_filter=${suite}.${test}"
  70. "--gtest_also_run_disabled_tests"
  71. ${extra_args}
  72. )
  73. if(suite MATCHES "^DISABLED" OR test MATCHES "^DISABLED")
  74. add_command(set_tests_properties
  75. "${prefix}${pretty_suite}.${pretty_test}${suffix}"
  76. PROPERTIES DISABLED TRUE
  77. )
  78. endif()
  79. add_command(set_tests_properties
  80. "${prefix}${pretty_suite}.${pretty_test}${suffix}"
  81. PROPERTIES
  82. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  83. ${properties}
  84. )
  85. list(APPEND tests "${prefix}${pretty_suite}.${pretty_test}${suffix}")
  86. endif()
  87. endif()
  88. endforeach()
  89. # Create a list of all discovered tests, which users may use to e.g. set
  90. # properties on the tests
  91. add_command(set ${TEST_LIST} ${tests})
  92. # Write CTest script
  93. file(WRITE "${CTEST_FILE}" "${script}")