w.deng | e87b500 | 2025-08-20 10:43:03 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | if [ ! -z "$JSONC_TEST_TRACE" ] ; then |
| 4 | VERBOSE=1 |
| 5 | set -x |
| 6 | set -v |
| 7 | fi |
| 8 | # Make sure srcdir is an absolute path. Supply the variable |
| 9 | # if it does not exist. We want to be able to run the tests |
| 10 | # stand-alone!! |
| 11 | # |
| 12 | srcdir=${srcdir-.} |
| 13 | if test ! -d $srcdir ; then |
| 14 | echo "test-defs.sh: installation error" 1>&2 |
| 15 | exit 1 |
| 16 | fi |
| 17 | |
| 18 | # Use absolute paths |
| 19 | case "$srcdir" in |
| 20 | /* | [A-Za-z]:\\*) ;; |
| 21 | *) srcdir=`\cd $srcdir && pwd` ;; |
| 22 | esac |
| 23 | |
| 24 | case "$top_builddir" in |
| 25 | /* | [A-Za-z]:\\*) ;; |
| 26 | *) top_builddir=`\cd ${top_builddir-..} && pwd` ;; |
| 27 | esac |
| 28 | |
| 29 | top_builddir=${top_builddir}/tests |
| 30 | |
| 31 | progname=`echo "$0" | sed 's,^.*/,,'` |
| 32 | testname=`echo "$progname" | sed 's,-.*$,,'` |
| 33 | testsubdir=${testsubdir-testSubDir} |
| 34 | testsubdir=${testsubdir}/${progname} |
| 35 | |
| 36 | # User can set VERBOSE to cause output redirection |
| 37 | case "$VERBOSE" in |
| 38 | [Nn]|[Nn][Oo]|0|"") |
| 39 | VERBOSE=0 |
| 40 | exec > /dev/null |
| 41 | ;; |
| 42 | [Yy]|[Yy][Ee][Ss]) |
| 43 | VERBOSE=1 |
| 44 | ;; |
| 45 | esac |
| 46 | |
| 47 | rm -rf "$testsubdir" > /dev/null 2>&1 |
| 48 | mkdir -p "$testsubdir" |
| 49 | CURDIR=$(pwd) |
| 50 | cd "$testsubdir" \ |
| 51 | || { echo "Cannot make or change into $testsubdir"; exit 1; } |
| 52 | |
| 53 | echo "=== Running test $progname" |
| 54 | |
| 55 | CMP="${CMP-cmp}" |
| 56 | |
| 57 | use_valgrind=${USE_VALGRIND-1} |
| 58 | case "${use_valgrind}" in |
| 59 | [0Nn]*) |
| 60 | use_valgrind=0 |
| 61 | ;; |
| 62 | *) |
| 63 | use_valgrind=1 |
| 64 | ;; |
| 65 | esac |
| 66 | valgrind_path=$(which valgrind 2> /dev/null) |
| 67 | if [ -z "${valgrind_path}" -o ! -x "${valgrind_path}" ] ; then |
| 68 | use_valgrind=0 |
| 69 | fi |
| 70 | |
| 71 | # |
| 72 | # This is a common function to check the results of a test program |
| 73 | # that is intended to generate consistent output across runs. |
| 74 | # |
| 75 | # ${top_builddir} must be set to the top level build directory. |
| 76 | # |
| 77 | # Output will be written to the current directory. |
| 78 | # |
| 79 | # It must be passed the name of the test command to run, which must be present |
| 80 | # in the ${top_builddir} directory. |
| 81 | # |
| 82 | # It will compare the output of running that against <name of command>.expected |
| 83 | # |
| 84 | run_output_test() |
| 85 | { |
| 86 | if [ "$1" = "-o" ] ; then |
| 87 | TEST_OUTPUT="$2" |
| 88 | shift |
| 89 | shift |
| 90 | fi |
| 91 | TEST_COMMAND="$1" |
| 92 | shift |
| 93 | if [ -z "${TEST_OUTPUT}" ] ; then |
| 94 | TEST_OUTPUT=${TEST_COMMAND} |
| 95 | fi |
| 96 | |
| 97 | REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\"" |
| 98 | if [ $VERBOSE -gt 1 ] ; then |
| 99 | REDIR_OUTPUT="| tee \"${TEST_OUTPUT}.out\"" |
| 100 | fi |
| 101 | |
| 102 | if [ $use_valgrind -eq 1 ] ; then |
| 103 | eval valgrind --tool=memcheck \ |
| 104 | --trace-children=yes \ |
| 105 | --demangle=yes \ |
| 106 | --log-file="${TEST_OUTPUT}.vg.out" \ |
| 107 | --leak-check=full \ |
| 108 | --show-reachable=yes \ |
| 109 | --run-libc-freeres=yes \ |
| 110 | "\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT} |
| 111 | err=$? |
| 112 | |
| 113 | else |
| 114 | eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT} |
| 115 | err=$? |
| 116 | fi |
| 117 | |
| 118 | if [ $err -ne 0 ] ; then |
| 119 | echo "ERROR: \"${TEST_COMMAND} $@\" exited with non-zero exit status: $err" 1>&2 |
| 120 | fi |
| 121 | |
| 122 | if [ $use_valgrind -eq 1 ] ; then |
| 123 | if ! tail -1 "${TEST_OUTPUT}.vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then |
| 124 | echo "ERROR: valgrind found errors during execution:" 1>&2 |
| 125 | cat "${TEST_OUTPUT}.vg.out" |
| 126 | err=1 |
| 127 | fi |
| 128 | fi |
| 129 | |
| 130 | if ! "$CMP" -s "${srcdir}/${TEST_OUTPUT}.expected" "${TEST_OUTPUT}.out" ; then |
| 131 | echo "ERROR: \"${TEST_COMMAND} $@\" (${TEST_OUTPUT}) failed (set VERBOSE=1 to see full output):" 1>&2 |
| 132 | (cd "${CURDIR}" ; set -x ; diff "${srcdir}/${TEST_OUTPUT}.expected" "$testsubdir/${TEST_OUTPUT}.out") |
| 133 | echo "cp \"$testsubdir/${TEST_OUTPUT}.out\" \"${srcdir}/${TEST_OUTPUT}.expected\"" 1>&2 |
| 134 | |
| 135 | err=1 |
| 136 | fi |
| 137 | |
| 138 | return $err |
| 139 | } |