Skip to content

Commit

Permalink
Add TEST_REQUIRES keyword in ecbuild_add_test
Browse files Browse the repository at this point in the history
  • Loading branch information
tweska authored and marcosbento committed Jan 23, 2025
1 parent 83b5038 commit 8ef56c6
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cmake/ecbuild_add_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# [ GENERATED <file1> [<file2> ...] ]
# [ DEPENDS <target1> [<target2> ...] ]
# [ TEST_DEPENDS <target1> [<target2> ...] ]
# [ TEST_REQUIRES <target1> [<target2> ...] ]
# [ CONDITION <condition> ]
# [ PROPERTIES <prop1> <val1> [<prop2> <val2> ...] ]
# [ TEST_PROPERTIES <prop1> <val1> [<prop2> <val2> ...] ]
Expand Down Expand Up @@ -128,6 +129,9 @@
# TEST_DEPENDS : optional
# list of tests to be run before this one
#
# TEST_REQUIRES : optional
# list of tests that will automatically run before this one
#
# CONDITION : optional
# conditional expression which must evaluate to true for this target to be
# built (must be valid in a CMake ``if`` statement)
Expand Down Expand Up @@ -187,7 +191,7 @@ function( ecbuild_add_test )

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

Expand Down Expand Up @@ -475,6 +479,15 @@ function( ecbuild_add_test )
set_tests_properties( ${_PAR_TARGET} PROPERTIES ${_PAR_TEST_PROPERTIES} )
endif()

# Set the fictures properties if test requires another test to run before
if ( DEFINED _PAR_TEST_REQUIRES )
ecbuild_debug("ecbuild_add_test(${_PAR_TARGET}): set test requirements to ${_PAR_TEST_REQUIRES}")
foreach(_requirement ${_PAR_TEST_REQUIRES} )
set_tests_properties( ${_requirement} PROPERTIES FIXTURES_SETUP ${_requirement} )
endforeach()
set_tests_properties( ${_PAR_TARGET} PROPERTIES FIXTURES_REQUIRED "${_PAR_TEST_REQUIRES}" )
endif()

# get test data

if( _PAR_TEST_DATA )
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ add_subdirectory( project_import )
add_subdirectory( project_summary )
add_subdirectory( ecbuild_override_compiler_flags )
add_subdirectory( test_properties )
add_subdirectory( test_requires_simple )
add_subdirectory( test_requires_complex )
7 changes: 7 additions & 0 deletions tests/test_requires_complex/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

ecbuild_add_test(
TARGET test_ecbuild_test_requires_complex
TYPE SCRIPT
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build-and-run.sh
ENVIRONMENT CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
)
26 changes: 26 additions & 0 deletions tests/test_requires_complex/build-and-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -e

HERE=${CMAKE_CURRENT_BINARY_DIR:-"$( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd -P )"}
SOURCE=${CMAKE_CURRENT_SOURCE_DIR:-$HERE}

# Add ecbuild to path
export PATH=$SOURCE/../../bin:$PATH
echo $PATH
echo $SOURCE

# Build the project
ecbuild $SOURCE/test_project -B $HERE/build

# Run only one specific test (which should invoke the dependency)
(cd $HERE/build; ctest -R write_world) # Avoid using --test-dir option in ctest

# Check if the output is as expected
echo -n "World!" | diff - $HERE/build/world.txt

# Run only one specific test (which should invoke the dependencies)
(cd $HERE/build; ctest -R combine_hello_world) # Avoid using --test-dir option in ctest

# Check if the output is as expected
echo -n "Hello, World!" | diff - $HERE/build/helloworld.txt
31 changes: 31 additions & 0 deletions tests/test_requires_complex/test_project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)

find_package( ecbuild REQUIRED )
project( test_test_requires_complex VERSION 0.1.0 LANGUAGES NONE )

ecbuild_add_test(
TARGET clean_output
COMMAND ${CMAKE_COMMAND}
ARGS -E remove ${CMAKE_CURRENT_BINARY_DIR}/hello.txt ${CMAKE_CURRENT_BINARY_DIR}/world.txt ${CMAKE_CURRENT_BINARY_DIR}/helloworld.txt
)

ecbuild_add_test(
TARGET write_hello
TEST_REQUIRES clean_output
COMMAND bash
ARGS -c "echo -n 'Hello, ' >> ${CMAKE_CURRENT_BINARY_DIR}/hello.txt"
)

ecbuild_add_test(
TARGET write_world
TEST_REQUIRES clean_output
COMMAND bash
ARGS -c "echo -n 'World!' >> ${CMAKE_CURRENT_BINARY_DIR}/world.txt"
)

ecbuild_add_test(
TARGET combine_hello_world
TEST_REQUIRES write_hello write_world
COMMAND bash
ARGS -c "cat ${CMAKE_CURRENT_BINARY_DIR}/hello.txt ${CMAKE_CURRENT_BINARY_DIR}/world.txt >> ${CMAKE_CURRENT_BINARY_DIR}/helloworld.txt"
)
7 changes: 7 additions & 0 deletions tests/test_requires_simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

ecbuild_add_test(
TARGET test_ecbuild_test_requires_simple
TYPE SCRIPT
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build-and-run.sh
ENVIRONMENT CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
)
20 changes: 20 additions & 0 deletions tests/test_requires_simple/build-and-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

set -e

HERE=${CMAKE_CURRENT_BINARY_DIR:-"$( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd -P )"}
SOURCE=${CMAKE_CURRENT_SOURCE_DIR:-$HERE}

# Add ecbuild to path
export PATH=$SOURCE/../../bin:$PATH
echo $PATH
echo $SOURCE

# Build the project
ecbuild $SOURCE/test_project -B $HERE/build

# Run only one specific test (which should invoke the dependencies)
(cd $HERE/build; ctest -R write_world_after_hello) # Avoid using --test-dir option in ctest

# Check if the output is as expected
echo -n "Hello, World!" | diff - $HERE/build/output.txt
24 changes: 24 additions & 0 deletions tests/test_requires_simple/test_project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)

find_package( ecbuild REQUIRED )
project( test_test_requires_simple VERSION 0.1.0 LANGUAGES NONE )

ecbuild_add_test(
TARGET clean_output
COMMAND ${CMAKE_COMMAND}
ARGS -E remove ${CMAKE_CURRENT_BINARY_DIR}/output.txt
)

ecbuild_add_test(
TARGET write_hello
TEST_REQUIRES clean_output
COMMAND bash
ARGS -c "echo -n 'Hello, ' >> ${CMAKE_CURRENT_BINARY_DIR}/output.txt"
)

ecbuild_add_test(
TARGET write_world_after_hello
TEST_REQUIRES write_hello
COMMAND bash
ARGS -c "echo -n 'World!' >> ${CMAKE_CURRENT_BINARY_DIR}/output.txt"
)

0 comments on commit 8ef56c6

Please sign in to comment.