GoogleTestAddTests.cmake 3.3 KB

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