GoogleTestAddTests.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 does not exist.\n"
  25. " Path: '${TEST_EXECUTABLE}'"
  26. )
  27. endif()
  28. execute_process(
  29. COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" --gtest_list_tests
  30. TIMEOUT ${TEST_DISCOVERY_TIMEOUT}
  31. OUTPUT_VARIABLE output
  32. RESULT_VARIABLE result
  33. )
  34. if(NOT ${result} EQUAL 0)
  35. string(REPLACE "\n" "\n " output "${output}")
  36. message(FATAL_ERROR
  37. "Error running test executable.\n"
  38. " Path: '${TEST_EXECUTABLE}'\n"
  39. " Result: ${result}\n"
  40. " Output:\n"
  41. " ${output}\n"
  42. )
  43. endif()
  44. string(REPLACE "\n" ";" output "${output}")
  45. # Parse output
  46. foreach(line ${output})
  47. # Skip header
  48. if(NOT line MATCHES "gtest_main\\.cc")
  49. # Do we have a module name or a test name?
  50. if(NOT line MATCHES "^ ")
  51. # Module; remove trailing '.' to get just the name...
  52. string(REGEX REPLACE "\\.( *#.*)?" "" suite "${line}")
  53. if(line MATCHES "#" AND NOT NO_PRETTY_TYPES)
  54. string(REGEX REPLACE "/[0-9]\\.+ +#.*= +" "/" pretty_suite "${line}")
  55. else()
  56. set(pretty_suite "${suite}")
  57. endif()
  58. string(REGEX REPLACE "^DISABLED_" "" pretty_suite "${pretty_suite}")
  59. else()
  60. # Test name; strip spaces and comments to get just the name...
  61. string(REGEX REPLACE " +" "" test "${line}")
  62. if(test MATCHES "#" AND NOT NO_PRETTY_VALUES)
  63. string(REGEX REPLACE "/[0-9]+#GetParam..=" "/" pretty_test "${test}")
  64. else()
  65. string(REGEX REPLACE "#.*" "" pretty_test "${test}")
  66. endif()
  67. string(REGEX REPLACE "^DISABLED_" "" pretty_test "${pretty_test}")
  68. string(REGEX REPLACE "#.*" "" test "${test}")
  69. # ...and add to script
  70. add_command(add_test
  71. "${prefix}${pretty_suite}.${pretty_test}${suffix}"
  72. ${TEST_EXECUTOR}
  73. "${TEST_EXECUTABLE}"
  74. "--gtest_filter=${suite}.${test}"
  75. "--gtest_also_run_disabled_tests"
  76. ${extra_args}
  77. )
  78. if(suite MATCHES "^DISABLED" OR test MATCHES "^DISABLED")
  79. add_command(set_tests_properties
  80. "${prefix}${pretty_suite}.${pretty_test}${suffix}"
  81. PROPERTIES DISABLED TRUE
  82. )
  83. endif()
  84. add_command(set_tests_properties
  85. "${prefix}${pretty_suite}.${pretty_test}${suffix}"
  86. PROPERTIES
  87. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  88. ${properties}
  89. )
  90. list(APPEND tests "${prefix}${pretty_suite}.${pretty_test}${suffix}")
  91. endif()
  92. endif()
  93. endforeach()
  94. # Create a list of all discovered tests, which users may use to e.g. set
  95. # properties on the tests
  96. add_command(set ${TEST_LIST} ${tests})
  97. # Write CTest script
  98. file(WRITE "${CTEST_FILE}" "${script}")