From b75a5294db07332467f53971610f6dd604f9941e Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Sun, 2 Jul 2023 11:17:26 -0500 Subject: [PATCH] Tests examples: use a diff rule to update source files during test invocation --- tests/examples/CMakeLists.txt | 41 ++++++++++++++++---------------- tests/examples/CMakeLists.txt.in | 41 ++++++++++++++++++++++++++++++++ tests/examples/update.sh | 32 ------------------------- 3 files changed, 62 insertions(+), 52 deletions(-) create mode 100644 tests/examples/CMakeLists.txt.in delete mode 100755 tests/examples/update.sh diff --git a/tests/examples/CMakeLists.txt b/tests/examples/CMakeLists.txt index 3440d08416..a9fdd58a5c 100644 --- a/tests/examples/CMakeLists.txt +++ b/tests/examples/CMakeLists.txt @@ -3,40 +3,41 @@ include(../scripts/setup_testsubproject.cmake) project(testsuite CXX) # -# Make sure that we have been called with a proper deal.II source directory -# layout: +# Setting up example tests is a bit tricky because we need to patch the +# current source file of the examples to make them suitable for the +# testsuite (sanitize output, reduce run time). # -set(DEAL_II_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../..") +# We thus create a set of "source" and "binary" sub directories in our +# CMAKE_CURRENT_BINARY_DIR that we can manipulate at will. We then populate +# the source directory with all (dynamically) patched step-*.cc files, +# create symlinks to all output files, and put the CMakelists.txt.in file +# in place. +# +# As a last step we call into the subdirectory and process all tests: +# + +set(DEAL_II_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") if (EXISTS "${DEAL_II_SOURCE_DIR}/examples") # # Create CMake file structure in the current binary dir: # + set(_base_directory "${CMAKE_CURRENT_SOURCE_DIR}") set(_source_directory "${CMAKE_CURRENT_BINARY_DIR}/source") set(_binary_directory "${CMAKE_CURRENT_BINARY_DIR}/binary") message(STATUS "Temporary source directory: ${_source_directory}") message(STATUS "Temporary binary directory: ${_binary_directory}") file(MAKE_DIRECTORY "${_source_directory}") - file(WRITE "${_source_directory}/CMakeLists.txt" "deal_ii_pickup_tests(examples)") + file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/example_test.h" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt.in" "${_source_directory}/CMakeLists.txt" + COPYONLY + ) enable_testing() file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CTestFile.cmake" "subdirs(binary)") - # - # Create tests from diffs: - # - execute_process( - COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/update.sh" "${DEAL_II_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" - WORKING_DIRECTORY "${_source_directory}" - ) - + # and now call into the second half of our setup in the writable "source" + # directory: add_subdirectory(${_source_directory} ${_binary_directory}) - - # - # Create a top-level diff target: - # - add_custom_target(update_diffs - COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/update_diffs.sh" "${DEAL_II_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" - WORKING_DIRECTORY "${_source_directory}" - ) endif() diff --git a/tests/examples/CMakeLists.txt.in b/tests/examples/CMakeLists.txt.in new file mode 100644 index 0000000000..5399bb328a --- /dev/null +++ b/tests/examples/CMakeLists.txt.in @@ -0,0 +1,41 @@ +# +# Parse all diff files... +# + +file(GLOB _diff_files "${_base_directory}/*.diff") +foreach(_diff_file ${_diff_files}) + get_filename_component(_step "${_diff_file}" NAME_WLE) + set(_source_file "${DEAL_II_SOURCE_DIR}/examples/${_step}/${_step}.cc") + set(_output_file "${_source_directory}/${_step}.cc") + + # ... and create a rule that updates all diff files if necessary during + # testsuite invocation: + add_custom_command(OUTPUT ${_output_file} + COMMAND patch ${_source_file} ${_diff_file} -o ${_output_file} + DEPENDS ${_source_file} ${_diff_file} + COMMENT "Patching ${_output_file}" + ) + + execute_process( + COMMAND patch ${_source_file} ${_diff_file} -o ${_output_file} + ) + + # Create symbolic links to the output files: + file(GLOB _output_files "${_base_directory}/${_step}*.output*") + foreach(_file ${_output_files}) + get_filename_component(_destination "${_file}" NAME) + set(_destination "${_source_directory}/${_destination}") + file(CREATE_LINK ${_file} ${_destination} SYMBOLIC) + endforeach() +endforeach() + +# +# Create a top-level diff target: +# + +add_custom_target(update_diffs + COMMAND "${_base_directory}/update_diffs.sh" "${DEAL_II_SOURCE_DIR}" "${_base_directory}" + WORKING_DIRECTORY "${_source_directory}" + ) + +deal_ii_pickup_tests(examples) diff --git a/tests/examples/update.sh b/tests/examples/update.sh deleted file mode 100755 index 64c79a05d3..0000000000 --- a/tests/examples/update.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash -set -eu - -DEAL_II_SOURCE_DIR="${1}" -shift -CMAKE_CURRENT_SOURCE_DIR="${1}" -shift - -# -# use .diff files to create .cc files -# - -# Find all *.diff files in the current directory -diff_files="$(find ${CMAKE_CURRENT_SOURCE_DIR} -maxdepth 1 -type f -name "*.diff")" - -for file in ${diff_files}; do - # Extract the filename without the extension - filename="$(basename "${file}" ".diff")" - source_file="${DEAL_II_SOURCE_DIR}/examples/${filename}/${filename}.cc" - output_files="${CMAKE_CURRENT_SOURCE_DIR}/${filename}*.output*" - - # Check if the corresponding .cc file exists - if [[ -f "${source_file}" ]]; then - # Apply the diff and save it to a new file - patch "${source_file}" -o "${filename}.cc" < "${file}" - echo "copying file(s) ${output_files}" - cp ${output_files} . - else - echo "No matching .cc file found for ${file}" - exit 1 - fi -done -- 2.39.5