From 4fadb4beb782adfeb5fea52f0bef64881f1c3e1c Mon Sep 17 00:00:00 2001 From: Martin Heimlich Date: Fri, 24 Jun 2022 11:16:29 +0200 Subject: [PATCH 1/3] Added new DL_PATHS option to the catch_discover_tests() ctest integration. This enables setting the required PATH/LD_LIBRARY_PATH environment variables both when retrieving the list of text cases and when executing the tests. --- 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..39c5cc5be4 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 themselfs. 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..4cbd6027f8 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_append:${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() From 38281069cf3499aea408c6995e400a8f699a1f18 Mon Sep 17 00:00:00 2001 From: Martin Heimlich Date: Mon, 27 Jun 2022 09:19:04 +0200 Subject: [PATCH 2/3] prepend to PATH in order to override any previously set paths. --- extras/CatchAddTests.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/CatchAddTests.cmake b/extras/CatchAddTests.cmake index 4cbd6027f8..5511ada4d7 100644 --- a/extras/CatchAddTests.cmake +++ b/extras/CatchAddTests.cmake @@ -101,7 +101,7 @@ 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_append:${native_path}") + list(APPEND environment_modifications "${dl_paths_variable_name}=path_list_prepend:${native_path}") endforeach() endif() From a3c04fd579460b92b39e254c79f8a3304818ee56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Mon, 11 Jul 2022 18:31:07 +0200 Subject: [PATCH 3/3] Update extras/Catch.cmake --- extras/Catch.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/Catch.cmake b/extras/Catch.cmake index 39c5cc5be4..bc553591b2 100644 --- a/extras/Catch.cmake +++ b/extras/Catch.cmake @@ -120,7 +120,7 @@ same as the Catch name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``. 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 themselfs. This requires + test executable and when the tests are executed themselves. This requires cmake/ctest >= 3.22. #]=======================================================================]