2
0

doctestAddTests.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(spec ${TEST_SPEC})
  6. set(extra_args ${TEST_EXTRA_ARGS})
  7. set(properties ${TEST_PROPERTIES})
  8. set(add_labels ${TEST_ADD_LABELS})
  9. set(junit_output_dir "${TEST_JUNIT_OUTPUT_DIR}")
  10. set(script)
  11. set(suite)
  12. set(tests)
  13. function(add_command NAME)
  14. set(_args "")
  15. foreach(_arg ${ARGN})
  16. if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
  17. set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
  18. else()
  19. set(_args "${_args} ${_arg}")
  20. endif()
  21. endforeach()
  22. set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
  23. endfunction()
  24. # Run test executable to get list of available tests
  25. if(NOT EXISTS "${TEST_EXECUTABLE}")
  26. message(FATAL_ERROR
  27. "Specified test executable '${TEST_EXECUTABLE}' does not exist"
  28. )
  29. endif()
  30. if("${spec}" MATCHES .)
  31. set(spec "--test-case=${spec}")
  32. endif()
  33. execute_process(
  34. COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-cases
  35. OUTPUT_VARIABLE output
  36. RESULT_VARIABLE result
  37. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  38. )
  39. if(NOT ${result} EQUAL 0)
  40. message(FATAL_ERROR
  41. "Error running test executable '${TEST_EXECUTABLE}':\n"
  42. " Result: ${result}\n"
  43. " Output: ${output}\n"
  44. )
  45. endif()
  46. string(REPLACE "\n" ";" output "${output}")
  47. # Parse output
  48. foreach(line ${output})
  49. if("${line}" STREQUAL "===============================================================================" OR "${line}" MATCHES [==[^\[doctest\] ]==])
  50. continue()
  51. endif()
  52. set(test ${line})
  53. set(labels "")
  54. if(${add_labels})
  55. # get test suite that test belongs to
  56. execute_process(
  57. COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" --test-case=${test} --list-test-suites
  58. OUTPUT_VARIABLE labeloutput
  59. RESULT_VARIABLE labelresult
  60. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  61. )
  62. if(NOT ${labelresult} EQUAL 0)
  63. message(FATAL_ERROR
  64. "Error running test executable '${TEST_EXECUTABLE}':\n"
  65. " Result: ${labelresult}\n"
  66. " Output: ${labeloutput}\n"
  67. )
  68. endif()
  69. string(REPLACE "\n" ";" labeloutput "${labeloutput}")
  70. foreach(labelline ${labeloutput})
  71. if("${labelline}" STREQUAL "===============================================================================" OR "${labelline}" MATCHES [==[^\[doctest\] ]==])
  72. continue()
  73. endif()
  74. list(APPEND labels ${labelline})
  75. endforeach()
  76. endif()
  77. if(NOT "${junit_output_dir}" STREQUAL "")
  78. # turn testname into a valid filename by replacing all special characters with "-"
  79. string(REGEX REPLACE "[/\\:\"|<>]" "-" test_filename "${test}")
  80. set(TEST_JUNIT_OUTPUT_PARAM "--reporters=junit" "--out=${junit_output_dir}/${prefix}${test_filename}${suffix}.xml")
  81. else()
  82. unset(TEST_JUNIT_OUTPUT_PARAM)
  83. endif()
  84. # use escape commas to handle properly test cases with commas inside the name
  85. string(REPLACE "," "\\," test_name ${test})
  86. # ...and add to script
  87. add_command(add_test
  88. "${prefix}${test}${suffix}"
  89. ${TEST_EXECUTOR}
  90. "${TEST_EXECUTABLE}"
  91. "--test-case=${test_name}"
  92. "${TEST_JUNIT_OUTPUT_PARAM}"
  93. ${extra_args}
  94. )
  95. add_command(set_tests_properties
  96. "${prefix}${test}${suffix}"
  97. PROPERTIES
  98. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  99. ${properties}
  100. LABELS ${labels}
  101. )
  102. unset(labels)
  103. list(APPEND tests "${prefix}${test}${suffix}")
  104. endforeach()
  105. # Create a list of all discovered tests, which users may use to e.g. set
  106. # properties on the tests
  107. add_command(set ${TEST_LIST} ${tests})
  108. # Write CTest script
  109. file(WRITE "${CTEST_FILE}" "${script}")