CatchAddTests.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(reporter ${TEST_REPORTER})
  9. set(output_dir ${TEST_OUTPUT_DIR})
  10. set(output_prefix ${TEST_OUTPUT_PREFIX})
  11. set(output_suffix ${TEST_OUTPUT_SUFFIX})
  12. set(script)
  13. set(suite)
  14. set(tests)
  15. function(add_command NAME)
  16. set(_args "")
  17. # use ARGV* instead of ARGN, because ARGN splits arrays into multiple arguments
  18. math(EXPR _last_arg ${ARGC}-1)
  19. foreach(_n RANGE 1 ${_last_arg})
  20. set(_arg "${ARGV${_n}}")
  21. if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
  22. set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
  23. else()
  24. set(_args "${_args} ${_arg}")
  25. endif()
  26. endforeach()
  27. set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
  28. endfunction()
  29. # Run test executable to get list of available tests
  30. if(NOT EXISTS "${TEST_EXECUTABLE}")
  31. message(FATAL_ERROR
  32. "Specified test executable '${TEST_EXECUTABLE}' does not exist"
  33. )
  34. endif()
  35. execute_process(
  36. COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-names-only
  37. OUTPUT_VARIABLE output
  38. RESULT_VARIABLE result
  39. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  40. )
  41. # Catch --list-test-names-only reports the number of tests, so 0 is... surprising
  42. if(${result} EQUAL 0)
  43. message(WARNING
  44. "Test executable '${TEST_EXECUTABLE}' contains no tests!\n"
  45. )
  46. elseif(${result} LESS 0)
  47. message(FATAL_ERROR
  48. "Error running test executable '${TEST_EXECUTABLE}':\n"
  49. " Result: ${result}\n"
  50. " Output: ${output}\n"
  51. )
  52. endif()
  53. string(REPLACE "\n" ";" output "${output}")
  54. # Run test executable to get list of available reporters
  55. execute_process(
  56. COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-reporters
  57. OUTPUT_VARIABLE reporters_output
  58. RESULT_VARIABLE reporters_result
  59. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  60. )
  61. if(${reporters_result} EQUAL 0)
  62. message(WARNING
  63. "Test executable '${TEST_EXECUTABLE}' contains no reporters!\n"
  64. )
  65. elseif(${reporters_result} LESS 0)
  66. message(FATAL_ERROR
  67. "Error running test executable '${TEST_EXECUTABLE}':\n"
  68. " Result: ${reporters_result}\n"
  69. " Output: ${reporters_output}\n"
  70. )
  71. endif()
  72. string(FIND "${reporters_output}" "${reporter}" reporter_is_valid)
  73. if(reporter AND ${reporter_is_valid} EQUAL -1)
  74. message(FATAL_ERROR
  75. "\"${reporter}\" is not a valid reporter!\n"
  76. )
  77. endif()
  78. # Prepare reporter
  79. if(reporter)
  80. set(reporter_arg "--reporter ${reporter}")
  81. endif()
  82. # Prepare output dir
  83. if(output_dir AND NOT IS_ABSOLUTE ${output_dir})
  84. set(output_dir "${TEST_WORKING_DIR}/${output_dir}")
  85. if(NOT EXISTS ${output_dir})
  86. file(MAKE_DIRECTORY ${output_dir})
  87. endif()
  88. endif()
  89. # Parse output
  90. foreach(line ${output})
  91. set(test ${line})
  92. # Escape characters in test case names that would be parsed by Catch2
  93. set(test_name ${test})
  94. foreach(char , [ ])
  95. string(REPLACE ${char} "\\${char}" test_name ${test_name})
  96. endforeach(char)
  97. # ...add output dir
  98. if(output_dir)
  99. string(REGEX REPLACE "[^A-Za-z0-9_]" "_" test_name_clean ${test_name})
  100. set(output_dir_arg "--out ${output_dir}/${output_prefix}${test_name_clean}${output_suffix}")
  101. endif()
  102. # ...and add to script
  103. add_command(add_test
  104. "${prefix}${test}${suffix}"
  105. ${TEST_EXECUTOR}
  106. "${TEST_EXECUTABLE}"
  107. "${test_name}"
  108. ${extra_args}
  109. "${reporter_arg}"
  110. "${output_dir_arg}"
  111. )
  112. add_command(set_tests_properties
  113. "${prefix}${test}${suffix}"
  114. PROPERTIES
  115. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  116. ${properties}
  117. )
  118. list(APPEND tests "${prefix}${test}${suffix}")
  119. endforeach()
  120. # Create a list of all discovered tests, which users may use to e.g. set
  121. # properties on the tests
  122. add_command(set ${TEST_LIST} ${tests})
  123. # Write CTest script
  124. file(WRITE "${CTEST_FILE}" "${script}")