From a63ad7455471dfa67275b866774f5fd0feb1cb81 Mon Sep 17 00:00:00 2001 From: mheimlich Date: Mon, 11 Jul 2022 18:31:32 +0200 Subject: [PATCH] Added new DL_PATHS option to catch_discover_tests() (#2467) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This enables setting the required PATH/LD_LIBRARY_PATH environment variables both when retrieving the list of text cases and when executing the tests. Co-authored-by: Martin Hořeňovský --- extras/Catch.cmake | 16 +++++++++++++++- extras/CatchAddTests.cmake | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/extras/Catch.cmake b/extras/Catch.cmake index a388516203..bc553591b2 100644 --- a/extras/Catch.cmake +++ b/extras/Catch.cmake @@ -116,6 +116,13 @@ same as the Catch name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``. ``--out dir/suffix``. This can be used to add a file extension to the output e.g. ".xml". + ``DL_PATHS path...`` + Specifies paths that need to be set for the dynamic linker to find shared + libraries/DLLs when running the test executable (PATH/LD_LIBRARY_PATH respectively). + These paths will both be set when retrieving the list of test cases from the + test executable and when the tests are executed themselves. This requires + cmake/ctest >= 3.22. + #]=======================================================================] #------------------------------------------------------------------------------ @@ -124,7 +131,7 @@ function(catch_discover_tests TARGET) "" "" "TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;REPORTER;OUTPUT_DIR;OUTPUT_PREFIX;OUTPUT_SUFFIX" - "TEST_SPEC;EXTRA_ARGS;PROPERTIES" + "TEST_SPEC;EXTRA_ARGS;PROPERTIES;DL_PATHS" ${ARGN} ) @@ -135,6 +142,12 @@ function(catch_discover_tests TARGET) set(_TEST_LIST ${TARGET}_TESTS) endif() + if (_DL_PATHS) + if(${CMAKE_VERSION} VERSION_LESS "3.22.0") + message(FATAL_ERROR "The DL_PATHS option requires at least cmake 3.22") + endif() + endif() + ## Generate a unique name based on the extra arguments string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS} ${_REPORTER} ${_OUTPUT_DIR} ${_OUTPUT_PREFIX} ${_OUTPUT_SUFFIX}") string(SUBSTRING ${args_hash} 0 7 args_hash) @@ -164,6 +177,7 @@ function(catch_discover_tests TARGET) -D "TEST_OUTPUT_DIR=${_OUTPUT_DIR}" -D "TEST_OUTPUT_PREFIX=${_OUTPUT_PREFIX}" -D "TEST_OUTPUT_SUFFIX=${_OUTPUT_SUFFIX}" + -D "TEST_DL_PATHS=${_DL_PATHS}" -D "CTEST_FILE=${ctest_tests_file}" -P "${_CATCH_DISCOVER_TESTS_SCRIPT}" VERBATIM diff --git a/extras/CatchAddTests.cmake b/extras/CatchAddTests.cmake index 9210f5a4df..5511ada4d7 100644 --- a/extras/CatchAddTests.cmake +++ b/extras/CatchAddTests.cmake @@ -10,10 +10,17 @@ set(reporter ${TEST_REPORTER}) set(output_dir ${TEST_OUTPUT_DIR}) set(output_prefix ${TEST_OUTPUT_PREFIX}) set(output_suffix ${TEST_OUTPUT_SUFFIX}) +set(dl_paths ${TEST_DL_PATHS}) set(script) set(suite) set(tests) +if(WIN32) + set(dl_paths_variable_name PATH) +else() + set(dl_paths_variable_name LD_LIBRARY_PATH) +endif() + function(add_command NAME) set(_args "") # use ARGV* instead of ARGN, because ARGN splits arrays into multiple arguments @@ -35,6 +42,12 @@ if(NOT EXISTS "${TEST_EXECUTABLE}") "Specified test executable '${TEST_EXECUTABLE}' does not exist" ) endif() + +if(dl_paths) + cmake_path(CONVERT "${dl_paths}" TO_NATIVE_PATH_LIST paths) + set(ENV{${dl_paths_variable_name}} "${paths}") +endif() + execute_process( COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-tests --verbosity quiet OUTPUT_VARIABLE output @@ -85,6 +98,13 @@ if(output_dir AND NOT IS_ABSOLUTE ${output_dir}) endif() endif() +if(dl_paths) + foreach(path ${dl_paths}) + cmake_path(NATIVE_PATH path native_path) + list(APPEND environment_modifications "${dl_paths_variable_name}=path_list_prepend:${native_path}") + endforeach() +endif() + # Parse output foreach(line ${output}) set(test ${line}) @@ -115,6 +135,14 @@ foreach(line ${output}) WORKING_DIRECTORY "${TEST_WORKING_DIR}" ${properties} ) + + if(environment_modifications) + add_command(set_tests_properties + "${prefix}${test}${suffix}" + PROPERTIES + ENVIRONMENT_MODIFICATION "${environment_modifications}") + endif() + list(APPEND tests "${prefix}${test}${suffix}") endforeach()