|
| 1 | +# This file is part of Fortuno. |
| 2 | +# Licensed under the BSD-2-Clause Plus Patent license. |
| 3 | +# SPDX-License-Identifier: BSD-2-Clause-Patent |
| 4 | + |
| 5 | +function(Fortuno_add_test) |
| 6 | + #[===[.md |
| 7 | + # Fortuno_add_test |
| 8 | +
|
| 9 | + Internal helper for adding functional tests testing whole CMake projects. |
| 10 | +
|
| 11 | + ## Synopsis |
| 12 | + ```cmake |
| 13 | + Template_add_test(<name> |
| 14 | + [TEST_NAME <test_name>] |
| 15 | + [TARGET <target>] |
| 16 | + ) |
| 17 | + ``` |
| 18 | +
|
| 19 | + ## Options |
| 20 | +
|
| 21 | + `<name>` |
| 22 | + : Path to the CMake project to be executed relative to `${CMAKE_CURRENT_SOURCE_DIR}` |
| 23 | +
|
| 24 | + `TEST_NAME` [Default: `<name>`] |
| 25 | + : Name for the test to be used as the ctest name |
| 26 | + ]===] |
| 27 | + |
| 28 | + set(ARGS_Options) |
| 29 | + set(ARGS_OneValue |
| 30 | + TEST_NAME |
| 31 | + ) |
| 32 | + set(ARGS_MultiValue) |
| 33 | + cmake_parse_arguments(PARSE_ARGV 1 ARGS "${ARGS_Options}" "${ARGS_OneValue}" "${ARGS_MultiValue}") |
| 34 | + |
| 35 | + # Check required/optional arguments |
| 36 | + if (ARGC LESS 1) |
| 37 | + message(FATAL_ERROR "Missing test name") |
| 38 | + endif () |
| 39 | + if (NOT DEFINED ARGS_TEST_NAME) |
| 40 | + set(ARGS_TEST_NAME ${test}) |
| 41 | + endif () |
| 42 | + |
| 43 | + set(configure_args |
| 44 | + -DCMAKE_Fortran_COMPILER=${CMAKE_Fortran_COMPILER} |
| 45 | + ) |
| 46 | + if (Fortuno_IS_TOP_LEVEL) |
| 47 | + list(APPEND configure_args |
| 48 | + # Generated Config file point to binary targets until it is installed |
| 49 | + -DTemplate_ROOT=${Fortuno_BINARY_DIR} |
| 50 | + -DFETCHCONTENT_SOURCE_DIR_FORTUNO=${Template_SOURCE_DIR} |
| 51 | + ) |
| 52 | + endif () |
| 53 | + |
| 54 | + add_test(NAME ${ARGS_TEST_NAME} |
| 55 | + COMMAND ${CMAKE_CTEST_COMMAND} --build-and-test ${CMAKE_CURRENT_SOURCE_DIR}/${test} |
| 56 | + ${CMAKE_CURRENT_BINARY_DIR}/${test} |
| 57 | + # Use the same build environment as the current runner |
| 58 | + --build-generator "${CMAKE_GENERATOR}" |
| 59 | + --build-options ${configure_args} |
| 60 | + --test-command ${CMAKE_CTEST_COMMAND} |
| 61 | + --test-dir ${CMAKE_CURRENT_BINARY_DIR}/${test} |
| 62 | + --output-on-failure |
| 63 | + ) |
| 64 | +endfunction() |
0 commit comments