Skip to content

Commit

Permalink
[CMake] Allow specifying test dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirraide committed Nov 11, 2023
1 parent 971c651 commit a68acca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
35 changes: 32 additions & 3 deletions cmake/FCHKDiscoverTests.cmake
Original file line number Diff line number Diff line change
@@ -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.
##
Expand All @@ -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(
Expand All @@ -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}"
Expand All @@ -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 "$<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}")
Expand All @@ -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()
endfunction()

0 comments on commit a68acca

Please sign in to comment.