Skip to content

Commit

Permalink
Added new DL_PATHS option to catch_discover_tests() (#2467)
Browse files Browse the repository at this point in the history
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ý <martin.horenovsky@gmail.com>
  • Loading branch information
mheimlich and horenmar committed Jul 11, 2022
1 parent 5f9109a commit a63ad74
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
16 changes: 15 additions & 1 deletion extras/Catch.cmake
Expand Up @@ -116,6 +116,13 @@ same as the Catch name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
``--out dir/<test_name>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.

#]=======================================================================]

#------------------------------------------------------------------------------
Expand All @@ -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}
)

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions extras/CatchAddTests.cmake
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit a63ad74

Please sign in to comment.