Skip to content

Commit 8ef56c6

Browse files
tweskamarcosbento
authored andcommitted
Add TEST_REQUIRES keyword in ecbuild_add_test
1 parent 83b5038 commit 8ef56c6

File tree

8 files changed

+131
-1
lines changed

8 files changed

+131
-1
lines changed

cmake/ecbuild_add_test.cmake

+14-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
# [ GENERATED <file1> [<file2> ...] ]
3535
# [ DEPENDS <target1> [<target2> ...] ]
3636
# [ TEST_DEPENDS <target1> [<target2> ...] ]
37+
# [ TEST_REQUIRES <target1> [<target2> ...] ]
3738
# [ CONDITION <condition> ]
3839
# [ PROPERTIES <prop1> <val1> [<prop2> <val2> ...] ]
3940
# [ TEST_PROPERTIES <prop1> <val1> [<prop2> <val2> ...] ]
@@ -128,6 +129,9 @@
128129
# TEST_DEPENDS : optional
129130
# list of tests to be run before this one
130131
#
132+
# TEST_REQUIRES : optional
133+
# list of tests that will automatically run before this one
134+
#
131135
# CONDITION : optional
132136
# conditional expression which must evaluate to true for this target to be
133137
# built (must be valid in a CMake ``if`` statement)
@@ -187,7 +191,7 @@ function( ecbuild_add_test )
187191

188192
set( options NO_AS_NEEDED )
189193
set( single_value_args TARGET ENABLED COMMAND TYPE LINKER_LANGUAGE MPI OMP WORKING_DIRECTORY )
190-
set( multi_value_args SOURCES OBJECTS LIBS INCLUDES TEST_DEPENDS DEPENDS LABELS ARGS
194+
set( multi_value_args SOURCES OBJECTS LIBS INCLUDES TEST_DEPENDS DEPENDS TEST_REQUIRES LABELS ARGS
191195
PERSISTENT DEFINITIONS RESOURCES TEST_DATA CFLAGS
192196
CXXFLAGS FFLAGS GENERATED CONDITION TEST_PROPERTIES PROPERTIES ENVIRONMENT )
193197

@@ -475,6 +479,15 @@ function( ecbuild_add_test )
475479
set_tests_properties( ${_PAR_TARGET} PROPERTIES ${_PAR_TEST_PROPERTIES} )
476480
endif()
477481

482+
# Set the fictures properties if test requires another test to run before
483+
if ( DEFINED _PAR_TEST_REQUIRES )
484+
ecbuild_debug("ecbuild_add_test(${_PAR_TARGET}): set test requirements to ${_PAR_TEST_REQUIRES}")
485+
foreach(_requirement ${_PAR_TEST_REQUIRES} )
486+
set_tests_properties( ${_requirement} PROPERTIES FIXTURES_SETUP ${_requirement} )
487+
endforeach()
488+
set_tests_properties( ${_PAR_TARGET} PROPERTIES FIXTURES_REQUIRED "${_PAR_TEST_REQUIRES}" )
489+
endif()
490+
478491
# get test data
479492

480493
if( _PAR_TEST_DATA )

tests/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ add_subdirectory( project_import )
1313
add_subdirectory( project_summary )
1414
add_subdirectory( ecbuild_override_compiler_flags )
1515
add_subdirectory( test_properties )
16+
add_subdirectory( test_requires_simple )
17+
add_subdirectory( test_requires_complex )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
ecbuild_add_test(
3+
TARGET test_ecbuild_test_requires_complex
4+
TYPE SCRIPT
5+
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build-and-run.sh
6+
ENVIRONMENT CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
HERE=${CMAKE_CURRENT_BINARY_DIR:-"$( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd -P )"}
6+
SOURCE=${CMAKE_CURRENT_SOURCE_DIR:-$HERE}
7+
8+
# Add ecbuild to path
9+
export PATH=$SOURCE/../../bin:$PATH
10+
echo $PATH
11+
echo $SOURCE
12+
13+
# Build the project
14+
ecbuild $SOURCE/test_project -B $HERE/build
15+
16+
# Run only one specific test (which should invoke the dependency)
17+
(cd $HERE/build; ctest -R write_world) # Avoid using --test-dir option in ctest
18+
19+
# Check if the output is as expected
20+
echo -n "World!" | diff - $HERE/build/world.txt
21+
22+
# Run only one specific test (which should invoke the dependencies)
23+
(cd $HERE/build; ctest -R combine_hello_world) # Avoid using --test-dir option in ctest
24+
25+
# Check if the output is as expected
26+
echo -n "Hello, World!" | diff - $HERE/build/helloworld.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
2+
3+
find_package( ecbuild REQUIRED )
4+
project( test_test_requires_complex VERSION 0.1.0 LANGUAGES NONE )
5+
6+
ecbuild_add_test(
7+
TARGET clean_output
8+
COMMAND ${CMAKE_COMMAND}
9+
ARGS -E remove ${CMAKE_CURRENT_BINARY_DIR}/hello.txt ${CMAKE_CURRENT_BINARY_DIR}/world.txt ${CMAKE_CURRENT_BINARY_DIR}/helloworld.txt
10+
)
11+
12+
ecbuild_add_test(
13+
TARGET write_hello
14+
TEST_REQUIRES clean_output
15+
COMMAND bash
16+
ARGS -c "echo -n 'Hello, ' >> ${CMAKE_CURRENT_BINARY_DIR}/hello.txt"
17+
)
18+
19+
ecbuild_add_test(
20+
TARGET write_world
21+
TEST_REQUIRES clean_output
22+
COMMAND bash
23+
ARGS -c "echo -n 'World!' >> ${CMAKE_CURRENT_BINARY_DIR}/world.txt"
24+
)
25+
26+
ecbuild_add_test(
27+
TARGET combine_hello_world
28+
TEST_REQUIRES write_hello write_world
29+
COMMAND bash
30+
ARGS -c "cat ${CMAKE_CURRENT_BINARY_DIR}/hello.txt ${CMAKE_CURRENT_BINARY_DIR}/world.txt >> ${CMAKE_CURRENT_BINARY_DIR}/helloworld.txt"
31+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
ecbuild_add_test(
3+
TARGET test_ecbuild_test_requires_simple
4+
TYPE SCRIPT
5+
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build-and-run.sh
6+
ENVIRONMENT CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
HERE=${CMAKE_CURRENT_BINARY_DIR:-"$( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd -P )"}
6+
SOURCE=${CMAKE_CURRENT_SOURCE_DIR:-$HERE}
7+
8+
# Add ecbuild to path
9+
export PATH=$SOURCE/../../bin:$PATH
10+
echo $PATH
11+
echo $SOURCE
12+
13+
# Build the project
14+
ecbuild $SOURCE/test_project -B $HERE/build
15+
16+
# Run only one specific test (which should invoke the dependencies)
17+
(cd $HERE/build; ctest -R write_world_after_hello) # Avoid using --test-dir option in ctest
18+
19+
# Check if the output is as expected
20+
echo -n "Hello, World!" | diff - $HERE/build/output.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
2+
3+
find_package( ecbuild REQUIRED )
4+
project( test_test_requires_simple VERSION 0.1.0 LANGUAGES NONE )
5+
6+
ecbuild_add_test(
7+
TARGET clean_output
8+
COMMAND ${CMAKE_COMMAND}
9+
ARGS -E remove ${CMAKE_CURRENT_BINARY_DIR}/output.txt
10+
)
11+
12+
ecbuild_add_test(
13+
TARGET write_hello
14+
TEST_REQUIRES clean_output
15+
COMMAND bash
16+
ARGS -c "echo -n 'Hello, ' >> ${CMAKE_CURRENT_BINARY_DIR}/output.txt"
17+
)
18+
19+
ecbuild_add_test(
20+
TARGET write_world_after_hello
21+
TEST_REQUIRES write_hello
22+
COMMAND bash
23+
ARGS -c "echo -n 'World!' >> ${CMAKE_CURRENT_BINARY_DIR}/output.txt"
24+
)

0 commit comments

Comments
 (0)