From ca1d6fb89fc9862efabf7afb58271566f69f5067 Mon Sep 17 00:00:00 2001 From: Lukas Riedel Date: Tue, 4 Sep 2018 14:52:28 +0200 Subject: [PATCH] Improve CMake testing functions * 'dorie_add_metaini_test': * Make handle metaini input files or regular files * Always configure metaini files and place them into the build dir * Error out on the case UNIT_TEST and TARGET * Add CMake docstrings * Remove '.mini' files from gitignore list --- .gitignore | 1 - cmake/modules/DorieTesting.cmake | 123 +++++++++++++++++++++++++++---- test/CMakeLists.txt | 34 ++++----- 3 files changed, 124 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 6f76be80..9a78d809 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ build-cmake/ # Exclude generated files -test/*.mini python/parfield/wrapper/pf_from_file.py python/testtools/wrapper/test_dorie.py python/testtools/wrapper/test_dorie_pfg.py diff --git a/cmake/modules/DorieTesting.cmake b/cmake/modules/DorieTesting.cmake index c0288a2e..d845c9a9 100644 --- a/cmake/modules/DorieTesting.cmake +++ b/cmake/modules/DorieTesting.cmake @@ -10,7 +10,14 @@ add_custom_target(system_tests COMMAND ctest --output-on-failure --exclude-regex ^ut.+$ ) -# Add appropriate links for generating code coverage information to targets +# +# .. cmake_function:: add_coverage_links +# +# This function adds the appropriate compiler and linker flags for creating +# a coverage report from the resulting object files of the specified targets. +# Signature: +# `add_coverage_links(...)` +# function(add_coverage_links) foreach(target ${ARGV}) target_compile_options(${target} PRIVATE --coverage) @@ -18,7 +25,29 @@ function(add_coverage_links) endforeach() endfunction() -# Add a unit test. This forwards all arguments to dune_add_test +# +# .. cmake_function:: dorie_add_unit_test +# +# .. cmake_param:: NAME +# :single: +# :required: +# +# The name of the resulting test executable and target. +# +# .. cmake_param:: TARGET +# :single: +# +# The target this test applies to. This is only required if no SOURCES +# are specified. +# +# This function serves as wrapper around the function `dune_add_test` which +# registers test for existing targets or adds new test executables from the +# given source files. This function additionally registers the tests as unit +# tests within DORiE and adds flags for coverage reports. Notice that +# `dune_add_test` requires more parameters than this function alone. +# +# Use this function exactly like `dune_add_test`. +# function(dorie_add_unit_test) set(SINGLE NAME TARGET) cmake_parse_arguments(UNIT_TEST "" "${SINGLE}" "" ${ARGN}) @@ -42,16 +71,87 @@ function(dorie_add_unit_test) add_dependencies(build_unit_tests ${UNIT_TEST_TARGET}) endfunction() -# Add a system test +# +# .. cmake_function:: dorie_add_metaini_test +# +# .. cmake_param:: UNIT_TEST +# :option: +# +# Registers the created tests as unit tests, including coverage flags. +# If not specified, the tests are registered as system tests. +# +# .. cmake_param:: TARGET +# :single: +# +# The existing target to apply these tests to. This is incompatible to +# the option `UNIT_TEST` and the parameter `BASENAME`, because the base +# name of the tests will automatically be set to the target name. +# +# .. cmake_param:: METAINI +# :single: +# :required: +# +# The meta-ini _input_ file for this test. +# +# .. cmake_param:: SCRIPT +# :single: +# +# The Python script to call for this test. The script has to be installed +# into the CMake `virtualenv`. Defaults to `dune_execute.py` for unit +# tests and `test_dorie.py` for system tests. +# +# .. cmake_param:: BASENAME +# :single: +# +# The basename for tests created from source files. This option is ignored +# (by `dune_add_system_test`) if `TARGET` is specified. +# +# .. cmake_param:: CREATED_TARGETS +# :single: +# +# The variable to hold the created CMake targets for post-processing. +# +# This function serves as wrapper around the function `dune_add_system_test` +# which registers test for existing targets or adds new test executables from +# the given source files by expanding meta ini files. +# This function can register the tests as unit or as system tests within +# DORiE. Notice that `dune_add_system_test` requires more parameters +# than this function alone. +# +# Use this function like `dune_add_system_test`, considering the options +# given above. +# function(dorie_add_metaini_test) set(OPTIONS UNIT_TEST) - set(SINGLE METAINI SCRIPT BASENAME CREATED_TARGETS) + set(SINGLE TARGET METAINI SCRIPT BASENAME CREATED_TARGETS) cmake_parse_arguments(SYSTEM_TEST "${OPTIONS}" "${SINGLE}" "" ${ARGN}) if(NOT SYSTEM_TEST_METAINI) message(SEND_ERROR "No meta ini file given!") endif() - configure_file(${SYSTEM_TEST_METAINI}.in ${CMAKE_CURRENT_LIST_DIR}/${SYSTEM_TEST_METAINI}) + + # configure meta ini file or just copy. + get_filename_component(metaini-name ${SYSTEM_TEST_METAINI} NAME_WE) + get_filename_component(metaini-extension ${SYSTEM_TEST_METAINI} EXT) + if(metaini-extension EQUAL ".mini.in") + configure_file(${SYSTEM_TEST_METAINI} ${metaini-name}.mini) + set(SYSTEM_TEST_METAINI "${metaini-name}.mini") + else() + configure_file(${SYSTEM_TEST_METAINI} ${SYSTEM_TEST_METAINI}) + endif() + + if(SYSTEM_TEST_TARGET AND SYSTEM_TEST_UNIT_TEST) + message(SEND_ERROR "Specifying TARGET is incompatible to option UNIT_TEST!") + endif() + + # Set at least the prefix as basename + if(NOT SYSTEM_TEST_TARGET) + if(SYSTEM_TEST_UNIT_TEST) + string(PREPEND SYSTEM_TEST_BASENAME ut-) + else() + string(PREPEND SYSTEM_TEST_BASENAME st-) + endif() + endif() # default script for system tests if(NOT SYSTEM_TEST_SCRIPT) @@ -60,21 +160,12 @@ function(dorie_add_metaini_test) endif() endif() - # Set at least the prefix as basename - if(NOT SYSTEM_TEST_BASENAME) - set(SYSTEM_TEST_BASENAME "") - endif() - if(SYSTEM_TEST_UNIT_TEST) - string(PREPEND SYSTEM_TEST_BASENAME ut-) - else() - string(PREPEND SYSTEM_TEST_BASENAME st-) - endif() - # forward to DUNE function dune_add_system_test( ${SYSTEM_TEST_UNPARSED_ARGUMENTS} + TARGET ${SYSTEM_TEST_TARGET} BASENAME ${SYSTEM_TEST_BASENAME} - INIFILE ${SYSTEM_TEST_METAINI} + INIFILE ${CMAKE_CURRENT_BINARY_DIR}/${SYSTEM_TEST_METAINI} SCRIPT ${SYSTEM_TEST_SCRIPT} CREATED_TARGETS created_targets ) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 088339f4..08ffd790 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,17 +11,17 @@ function(dorie_add_system_test_dependency test1 test2) endfunction() # dorie run: ODE tests -dorie_add_metaini_test(TARGET dorie METAINI ode_homogeneous_sand.mini) -dorie_add_metaini_test(TARGET dorie METAINI ode_homogeneous_silt.mini) -dorie_add_metaini_test(TARGET dorie METAINI ode_layered.mini) +dorie_add_metaini_test(TARGET dorie METAINI ode_homogeneous_sand.mini.in) +dorie_add_metaini_test(TARGET dorie METAINI ode_homogeneous_silt.mini.in) +dorie_add_metaini_test(TARGET dorie METAINI ode_layered.mini.in) # add target for ODE tests add_custom_target(test_run_ode COMMAND ctest --output-on-failure --tests-regex ^dorie_ode.+$) # dorie run: Reference tests -dorie_add_metaini_test(TARGET dorie-rfg METAINI parfield_muphi.mini) -dorie_add_metaini_test(TARGET dorie METAINI muphi.mini) +dorie_add_metaini_test(TARGET dorie-rfg METAINI parfield_muphi.mini.in) +dorie_add_metaini_test(TARGET dorie METAINI muphi.mini.in) set_tests_properties(dorie-rfg_ref_muphi_pfg PROPERTIES FIXTURES_SETUP muphi_ref) set_tests_properties(dorie_ref_muphi PROPERTIES FIXTURES_REQUIRED muphi_ref) @@ -29,14 +29,14 @@ add_custom_target(test_run_ref COMMAND ctest --output-on-failure --tests-regex ^.+_ref_.+$ ) -# dorie_add_system_test(dorie reference_2d.mini) -# dorie_add_system_test(dorie reference_3d.mini) -# dorie_add_system_test(dorie reference_evaporation.mini) -# dorie_add_system_test(dorie reference_interpolators.mini) +# dorie_add_system_test(dorie reference_2d.mini.in) +# dorie_add_system_test(dorie reference_3d.mini.in) +# dorie_add_system_test(dorie reference_evaporation.mini.in) +# dorie_add_system_test(dorie reference_interpolators.mini.in) # dorie in parallel -dorie_add_metaini_test(TARGET dorie METAINI parallel_reference.mini) -dorie_add_metaini_test(TARGET dorie METAINI parallel_reference_compare.mini) +dorie_add_metaini_test(TARGET dorie METAINI parallel_reference.mini.in) +dorie_add_metaini_test(TARGET dorie METAINI parallel_reference_compare.mini.in) set_tests_properties(dorie_parallel_reference_0000 PROPERTIES FIXTURES_SETUP dorie_par) set_tests_properties(dorie_parallel_reference_0001 PROPERTIES FIXTURES_SETUP dorie_par) @@ -50,17 +50,17 @@ add_custom_target(test_run_parallel ) # dorie exec tests -dorie_add_metaini_test(TARGET dorie-rfg METAINI parfield.mini) -dorie_add_metaini_test(TARGET dorie METAINI run.mini) +dorie_add_metaini_test(TARGET dorie-rfg METAINI parfield.mini.in) +dorie_add_metaini_test(TARGET dorie METAINI run.mini.in) set_tests_properties(dorie-rfg_exec_0000 PROPERTIES FIXTURES_SETUP dorie_run) set_tests_properties(dorie_exec_run PROPERTIES FIXTURES_REQUIRED dorie_run) -dorie_add_metaini_test(TARGET dorie METAINI plot.mini) +dorie_add_metaini_test(TARGET dorie METAINI plot.mini.in) dorie_add_system_test_dependency(dorie_exec_plot dorie_exec_run) set_tests_properties(dorie_exec_run PROPERTIES FIXTURES_SETUP dorie_plot) set_tests_properties(dorie_exec_plot PROPERTIES FIXTURES_REQUIRED dorie_plot) -dorie_add_metaini_test(TARGET dorie METAINI create.mini) +dorie_add_metaini_test(TARGET dorie METAINI create.mini.in) add_custom_target(test_dorie_exec COMMAND ctest --output-on-failure --tests-regex ^.+_exec.+$ @@ -70,7 +70,7 @@ dorie_add_metaini_test( SOURCE test-mass-conservation.cc BASENAME mass-conservation CREATED_TARGETS mc_target - METAINI mass_conservation.mini + METAINI mass_conservation.mini.in SCRIPT dune_execute.py ) @@ -83,7 +83,7 @@ dorie_add_metaini_test(UNIT_TEST SOURCE test-parameterization.cc BASENAME test-parameterization CREATED_TARGETS par_target - METAINI param.mini + METAINI param.mini.in SCRIPT ) add_custom_target(test_param -- GitLab