From 0c5ef5e87ff718a723dabc4a2f881696f88c9abc Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Thu, 12 Jan 2017 05:41:43 -0600 Subject: [PATCH] CMake: Simplify run_test.sh and avoid array handling magic --- cmake/scripts/run_test.sh | 227 +++++++++++++++++++------------------- 1 file changed, 115 insertions(+), 112 deletions(-) diff --git a/cmake/scripts/run_test.sh b/cmake/scripts/run_test.sh index 53c82727f7..4365a5ab33 100644 --- a/cmake/scripts/run_test.sh +++ b/cmake/scripts/run_test.sh @@ -18,143 +18,146 @@ # ("diff") individual tests. # # Usage: -# run_test.sh run TEST_FULL RUN_COMMAND +# run_test.sh run TEST_FULL [COMMAND and ARGS] # -# run_test.sh diff TEST_FULL DIFF_EXECUTABLE DIFF_EXECUTABLE COMPARISON_FILE +# run_test.sh diff TEST_FULL NUMDIFF_EXECUTABLE DIFF_EXECUTABLE \ +# COMPARISON_FILE # set -u STAGE="$1" TEST_FULL="$2" -NUMDIFF_EXECUTABLE="$3" -DIFF_EXECUTABLE="$4" -COMPARISON_FILE="$5" -shift 5 - -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; - done - echo " " -} - -RUN_COMMAND=$(save "$@") +shift 2 # Ensure uniform sorting for pathname expansion export LC_ALL=C -# -# Add a top level target to run and compare the test: -# - -run(){ - rm -f failing_output - rm -f output - rm -f stdout - - eval "set -- ${RUN_COMMAND}" - "$@" > stdout 2>&1 - RETURN_VALUE=$? - - [ -f output ] || mv stdout output - - if [ $RETURN_VALUE -ne 0 ]; then - mv output failing_output - echo "${TEST_FULL}: BUILD successful." - echo "${TEST_FULL}: RUN failed. ------ Return code $RETURN_VALUE" - echo "${TEST_FULL}: RUN failed. ------ Result: `pwd`/failing_output" - echo "${TEST_FULL}: RUN failed. ------ Partial output:" - cat failing_output - if [ -f stdout ]; then - echo "" - echo "${TEST_FULL}: RUN failed. ------ Additional output on stdout/stderr:" - echo "" - cat stdout +case $STAGE in + run) + ## + # run stage: + # - run specified command and parameters given by $@ + # - creates file "output" on success containing stdout and stderr of + # the test + # - if test exits with non-zero return value, output is renamed to + # failing_output + ## + + rm -f failing_output + rm -f output + rm -f stdout + + "$@" > stdout 2>&1 + RETURN_VALUE=$? + + [ -f output ] || mv stdout output + + if [ $RETURN_VALUE -ne 0 ]; then + mv output failing_output + echo "${TEST_FULL}: BUILD successful." + echo "${TEST_FULL}: RUN failed. ------ Return code $RETURN_VALUE" + echo "${TEST_FULL}: RUN failed. ------ Result: `pwd`/failing_output" + echo "${TEST_FULL}: RUN failed. ------ Partial output:" + cat failing_output + if [ -f stdout ]; then + echo "" + echo "${TEST_FULL}: RUN failed. ------ Additional output on stdout/stderr:" + echo "" + cat stdout + fi + exit 1 fi - exit 1 - fi -} -diff() { - rm -f failing_diff* - rm -f diff* - touch diff + ;; + + diff) + + ## + # diff stage: + # - compares the file "output" against various comparison files. + ## + + NUMDIFF_EXECUTABLE="$1" + DIFF_EXECUTABLE="$2" + COMPARISON_FILE="$3" - test_successful=false + rm -f failing_diff* + rm -f diff* + touch diff - # - # Pick up main comparison file and all variants. A valid variant name is - # of the form [...].output.[STRING] - # - for file in "${COMPARISON_FILE}"*; do - # determine variant name (empty string for main comparison file): - variant="${file#*.output}" + test_successful=false # - # Run diff or numdiff (if available) to determine whether files are the - # same. Create a diff file "diff${variant}" for each variant file that - # is found (including the main comparison file). + # Pick up main comparison file and all variants. A valid variant name is + # of the form [...].output.[STRING] # - case "${NUMDIFF_EXECUTABLE}" in - *numdiff*) - "${NUMDIFF_EXECUTABLE}" -a 1e-6 -r 1e-8 -s ' \t\n:<>=,;' \ - "${file}" output > diff${variant} - ;; - *) - "${DIFF_EXECUTABLE}" "${file}" output > diff${variant} - ;; - esac - - if [ $? -eq 0 ]; then + for file in "${COMPARISON_FILE}"*; do + # determine variant name (empty string for main comparison file): + variant="${file#*.output}" + # - # Ensure that only a single diff file with no contents remains - # (numdiff has the bad habit of being very verbose...): + # Run diff or numdiff (if available) to determine whether files are the + # same. Create a diff file "diff${variant}" for each variant file that + # is found (including the main comparison file). # - rm -f diff* - touch diff - - if [ -n "${variant}" ]; then + case "${NUMDIFF_EXECUTABLE}" in + *numdiff*) + "${NUMDIFF_EXECUTABLE}" -a 1e-6 -r 1e-8 -s ' \t\n:<>=,;' \ + "${file}" output > diff${variant} + ;; + *) + "${DIFF_EXECUTABLE}" "${file}" output > diff${variant} + ;; + esac + + if [ $? -eq 0 ]; then # - # In case of a successful comparison against a variant, store the - # fact that we compared against a variant in the diff file. + # Ensure that only a single diff file with no contents remains + # (numdiff has the bad habit of being very verbose...): # - echo "${TEST_FULL}: DIFF successful. - Variant: ${file}" > diff + rm -f diff* + touch diff + + if [ -n "${variant}" ]; then + # + # In case of a successful comparison against a variant, store the + # fact that we compared against a variant in the diff file. + # + echo "${TEST_FULL}: DIFF successful. - Variant: ${file}" > diff + fi + + test_successful=true + break fi + done - test_successful=true - break + # + # If none of the diffs succeeded, use the diff against the main comparison + # file. Output the first few lines of the output of numdiff, followed by + # the results of regular diff since the latter is just more readable. + # + if [ $test_successful = false ] ; then + for file in diff*; do + mv "$file" failing_"$file" + done + echo "${TEST_FULL}: BUILD successful." + echo "${TEST_FULL}: RUN successful." + echo "${TEST_FULL}: DIFF failed. ------ Source: ${COMPARISON_FILE}" + echo "${TEST_FULL}: DIFF failed. ------ Result: `pwd`/output" + echo "Check `pwd`/output ${COMPARISON_FILE}" + echo "${TEST_FULL}: DIFF failed. ------ Diff: `pwd`/failing_diff" + echo "${TEST_FULL}: DIFF failed. ------ First 8 lines of numdiff/diff output:" + cat failing_diff | head -n 8 + echo "${TEST_FULL}: DIFF failed. ------ First 50 lines diff output:" + "${DIFF_EXECUTABLE}" -c "${COMPARISON_FILE}" output | head -n 50 + exit 1 fi - done - - # - # If none of the diffs succeeded, use the diff against the main comparison - # file. Output the first few lines of the output of numdiff, followed by - # the results of regular diff since the latter is just more readable. - # - if [ $test_successful = false ] ; then - for file in diff*; do - mv "$file" failing_"$file" - done - echo "${TEST_FULL}: BUILD successful." - echo "${TEST_FULL}: RUN successful." - echo "${TEST_FULL}: DIFF failed. ------ Source: ${COMPARISON_FILE}" - echo "${TEST_FULL}: DIFF failed. ------ Result: `pwd`/output" - echo "Check `pwd`/output ${COMPARISON_FILE}" - echo "${TEST_FULL}: DIFF failed. ------ Diff: `pwd`/failing_diff" - echo "${TEST_FULL}: DIFF failed. ------ First 8 lines of numdiff/diff output:" - cat failing_diff | head -n 8 - echo "${TEST_FULL}: DIFF failed. ------ First 50 lines diff output:" - "${DIFF_EXECUTABLE}" -c "${COMPARISON_FILE}" output | head -n 50 - exit 1 - fi - exit 0 -} + exit 0 -case $STAGE in - run) - run;; - diff) - diff;; + ;; *) - exit 1;; + + exit 1 + ;; esac -- 2.39.5