Skip to content

Commit e5e125e

Browse files
committed
[WIP] Migrate examples to standalone tests
1 parent 0c14523 commit e5e125e

File tree

14 files changed

+114
-31
lines changed

14 files changed

+114
-31
lines changed

cmake/FortunoTestHelpers.cmake

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 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()

config.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ option(FORTUNO_WITH_COARRAY "Fortuno: whether library with coarray interface sho
1515

1616
option(FORTUNO_WITH_TESTS "Fortuno: whether to build test suite" ${PROJECT_IS_TOP_LEVEL})
1717

18-
option(FORTUNO_WITH_EXAMPLES "Fortuno: whether to build example apps" ${PROJECT_IS_TOP_LEVEL})
18+
if (FORTUNO_WITH_EXAMPLES)
19+
message(WARNING
20+
"FORTUNO_WITH_EXAMPLES is deprecated and has no effect. "
21+
"This is now part of the tests"
22+
)
23+
endif ()
1924

2025
option(FORTUNO_INSTALL "Fortuno: Install project" ${PROJECT_IS_TOP_LEVEL})
2126

example/coarray/CMakeLists.txt

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,37 @@
22
# Licensed under the BSD-2-Clause Plus Patent license.
33
# SPDX-License-Identifier: BSD-2-Clause-Patent
44

5-
list(APPEND CMAKE_MESSAGE_CONTEXT Coarray)
6-
7-
add_library(fortuno_example_coarray_mylib)
8-
set_target_properties(
9-
fortuno_example_coarray_mylib PROPERTIES
10-
OUTPUT_NAME mylib
5+
project(coarray-example
6+
LANGUAGES Fortran
117
)
8+
9+
find_package(Fortuno CONFIG REQUIRED)
10+
11+
# TODO: decouple this from config.cmake setup
12+
include(../../config.cmake)
13+
14+
add_library(mylib)
1215
target_sources(
13-
fortuno_example_coarray_mylib PRIVATE
16+
mylib PRIVATE
1417
mylib.f90
1518
)
16-
target_compile_options(fortuno_example_coarray_mylib PRIVATE ${FORTUNO_FFLAGS_COARRAY})
17-
target_link_options(fortuno_example_coarray_mylib PRIVATE ${FORTUNO_LDFLAGS_COARRAY})
19+
target_compile_options(mylib PRIVATE ${FORTUNO_FFLAGS_COARRAY})
20+
target_link_options(mylib PRIVATE ${FORTUNO_LDFLAGS_COARRAY})
1821

19-
add_executable(fortuno_example_coarray_testapp)
20-
set_target_properties(
21-
fortuno_example_coarray_testapp PROPERTIES
22-
OUTPUT_NAME testapp
23-
)
22+
add_executable(testapp)
2423
target_sources(
25-
fortuno_example_coarray_testapp PRIVATE
24+
testapp PRIVATE
2625
test_simple.f90
2726
testapp.f90
2827
)
2928
target_link_libraries(
30-
fortuno_example_coarray_testapp PRIVATE
31-
fortuno_example_coarray_mylib Fortuno::fortuno_coarray
29+
testapp PRIVATE
30+
mylib Fortuno::fortuno_coarray
31+
)
32+
target_compile_options(testapp PRIVATE ${FORTUNO_FFLAGS_COARRAY})
33+
target_link_options(testapp PRIVATE ${FORTUNO_LDFLAGS_COARRAY})
34+
35+
enable_testing()
36+
add_test(NAME fortuno_test
37+
COMMAND testapp
3238
)
33-
target_compile_options(fortuno_example_coarray_testapp PRIVATE ${FORTUNO_FFLAGS_COARRAY})
34-
target_link_options(fortuno_example_coarray_testapp PRIVATE ${FORTUNO_LDFLAGS_COARRAY})

test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
list(APPEND CMAKE_MESSAGE_CONTEXT Test)
66

7+
include(FortunoTestHelpers)
8+
79
add_subdirectory(unit)
10+
add_subdirectory(example)

example/CMakeLists.txt renamed to test/example/CMakeLists.txt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@
22
# Licensed under the BSD-2-Clause Plus Patent license.
33
# SPDX-License-Identifier: BSD-2-Clause-Patent
44

5-
list(APPEND CMAKE_MESSAGE_CONTEXT Example)
6-
75
if (FORTUNO_WITH_SERIAL)
8-
add_subdirectory(serial)
6+
Fortuno_add_test(serial)
97
if (FORTUNO_WITH_FPP)
10-
add_subdirectory(serial-fpp)
8+
Fortuno_add_test(serial-fpp)
119
endif ()
1210
if (FYPP)
13-
add_subdirectory(serial-fypp)
11+
Fortuno_add_test(serial-fypp)
1412
endif ()
1513
endif ()
1614

1715
if (FORTUNO_WITH_MPI)
18-
add_subdirectory(mpi)
16+
Fortuno_add_test(mpi)
1917
if (FORTUNO_WITH_FPP)
20-
add_subdirectory(mpi-fpp)
18+
Fortuno_add_test(mpi-fpp)
2119
endif ()
2220
if (FYPP)
23-
add_subdirectory(mpi-fypp)
21+
Fortuno_add_test(mpi-fypp)
2422
endif ()
2523
endif ()
2624

2725
if (FORTUNO_WITH_COARRAY)
28-
add_subdirectory(coarray)
26+
Fortuno_add_test(coarray)
2927
if (FORTUNO_WITH_FPP)
30-
add_subdirectory(coarray-fpp)
28+
Fortuno_add_test(coarray-fpp)
3129
endif ()
3230
if (FYPP)
33-
add_subdirectory(coarray-fypp)
31+
Fortuno_add_test(coarray-fypp)
3432
endif ()
3533
endif ()

test/example/coarray

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../example/coarray

test/example/coarray-fpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../example/coarray-fpp

test/example/coarray-fypp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../example/coarray-fypp

test/example/mpi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../example/mpi

test/example/mpi-fpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../example/mpi-fpp

0 commit comments

Comments
 (0)