From a68acca6d2fd1495e57a3fa8a249c2172ca5be9e Mon Sep 17 00:00:00 2001 From: Sirraide Date: Sat, 11 Nov 2023 20:45:30 +0100 Subject: [PATCH] [CMake] Allow specifying test dependencies --- CMakeLists.txt | 2 +- cmake/FCHKDiscoverTests.cmake | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cc851c..20ea1fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.20) -project(fchk VERSION 2.1.0 LANGUAGES CXX) +project(fchk VERSION 2.1.1 LANGUAGES CXX) ## ============================================================================ ## Global CMake Variables. diff --git a/cmake/FCHKDiscoverTests.cmake b/cmake/FCHKDiscoverTests.cmake index 78218e1..30808c2 100644 --- a/cmake/FCHKDiscoverTests.cmake +++ b/cmake/FCHKDiscoverTests.cmake @@ -1,7 +1,9 @@ enable_testing() ## User or the projects CMake file is expected to provide this. -set(FCHK_EXE_PATH "fchk" CACHE FILEPATH "Path to the fchk executable" ) +if (NOT DEFINED FCHK_EXE_PATH) + set(FCHK_EXE_PATH "fchk" CACHE FILEPATH "Path to the fchk executable" ) +endif() ## Collect tests in one or more directories. ## @@ -13,6 +15,7 @@ set(FCHK_EXE_PATH "fchk" CACHE FILEPATH "Path to the fchk executable" ) ## WORKING_DIRECTORY: The working directory to use for the test. ## ARGS: Additional arguments to pass to fchk. ## RECURSIVE: If set, recurse into subdirectories. +## DEPENDS: Mark that the tests depend on one or more targets. ## ## Example: ## FCHKAddAllTestsInDir( @@ -26,7 +29,7 @@ function(FCHKAddAllTestsInDir) set(options RECURSIVE) set(oneValueArgs TEST_NAME_PREFIX PREFIX WORKING_DIRECTORY) - set(multiValueArgs IN PATTERN ARGS) + set(multiValueArgs IN PATTERN ARGS DEPENDS) cmake_parse_arguments(FCHKAddAllTestsInDir "${options}" "${oneValueArgs}" @@ -39,6 +42,26 @@ function(FCHKAddAllTestsInDir) set(fchk_prefix --prefix ${FCHKAddAllTestsInDir_PREFIX}) endif() + ## Ungodly jank to allow people to add dependencies on other targets to the tests. + if (FCHKAddAllTestsInDir_DEPENDS) + foreach (dependency ${FCHKAddAllTestsInDir_DEPENDS}) + if (NOT TEST "_fchk_build_deps-${dependency}") + add_test( + NAME "_fchk_build_deps-${dependency}" + COMMAND "${CMAKE_COMMAND}" + --build "${CMAKE_BINARY_DIR}" + --config "$" + --target "${dependency}" + COMMAND_EXPAND_LISTS + ) + + set_tests_properties("_fchk_build_deps-${dependency}" + PROPERTIES FIXTURES_SETUP _fchk_build_deps + ) + endif() + endforeach() + endif() + foreach (dir ${FCHKAddAllTestsInDir_IN}) foreach (pat ${FCHKAddAllTestsInDir_PATTERN}) file(GLOB_RECURSE tests "${dir}/${pat}") @@ -52,7 +75,13 @@ function(FCHKAddAllTestsInDir) ${test} WORKING_DIRECTORY ${FCHKAddAllTestsInDir_WORKING_DIRECTORY} ) + + if (FCHKAddAllTestsInDir_DEPENDS) + set_tests_properties("${FCHKAddAllTestsInDir_TEST_NAME_PREFIX}${test}" + PROPERTIES FIXTURES_REQUIRED _fchk_build_deps + ) + endif() endforeach() endforeach() endforeach() -endfunction() \ No newline at end of file +endfunction()