CatchAddTests.cmake 4.3 KB

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