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