test-defs.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/sh
  2. # Make sure srcdir is an absolute path. Supply the variable
  3. # if it does not exist. We want to be able to run the tests
  4. # stand-alone!!
  5. #
  6. srcdir=${srcdir-.}
  7. if test ! -d $srcdir ; then
  8. echo "test-defs.sh: installation error" 1>&2
  9. exit 1
  10. fi
  11. # Use absolute paths
  12. case "$srcdir" in
  13. /* | [A-Za-z]:\\*) ;;
  14. *) srcdir=`\cd $srcdir && pwd` ;;
  15. esac
  16. case "$top_builddir" in
  17. /* | [A-Za-z]:\\*) ;;
  18. *) top_builddir=`\cd ${top_builddir-..} && pwd` ;;
  19. esac
  20. top_builddir=${top_builddir}/tests
  21. progname=`echo "$0" | sed 's,^.*/,,'`
  22. testname=`echo "$progname" | sed 's,-.*$,,'`
  23. testsubdir=${testsubdir-testSubDir}
  24. testsubdir=${testsubdir}/${progname}
  25. # User can set VERBOSE to cause output redirection
  26. case "$VERBOSE" in
  27. [Nn]|[Nn][Oo]|0|"")
  28. VERBOSE=0
  29. exec > /dev/null
  30. ;;
  31. [Yy]|[Yy][Ee][Ss])
  32. VERBOSE=1
  33. ;;
  34. esac
  35. rm -rf "$testsubdir" > /dev/null 2>&1
  36. mkdir -p "$testsubdir"
  37. CURDIR=$(pwd)
  38. cd "$testsubdir" \
  39. || { echo "Cannot make or change into $testsubdir"; exit 1; }
  40. echo "=== Running test $progname"
  41. CMP="${CMP-cmp}"
  42. use_valgrind=${USE_VALGRIND-1}
  43. case "${use_valgrind}" in
  44. [0Nn]*)
  45. use_valgrind=0
  46. ;;
  47. *)
  48. use_valgrind=1
  49. ;;
  50. esac
  51. valgrind_path=$(which valgrind 2> /dev/null)
  52. if [ -z "${valgrind_path}" -o ! -x "${valgrind_path}" ] ; then
  53. use_valgrind=0
  54. fi
  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. if [ "$1" = "-o" ] ; then
  71. TEST_OUTPUT="$2"
  72. shift
  73. shift
  74. fi
  75. TEST_COMMAND="$1"
  76. shift
  77. if [ -z "${TEST_OUTPUT}" ] ; then
  78. TEST_OUTPUT=${TEST_COMMAND}
  79. fi
  80. REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\""
  81. if [ $VERBOSE -gt 1 ] ; then
  82. REDIR_OUTPUT="| tee \"${TEST_OUTPUT}.out\""
  83. fi
  84. if [ $use_valgrind -eq 1 ] ; then
  85. eval valgrind --tool=memcheck \
  86. --trace-children=yes \
  87. --demangle=yes \
  88. --log-file="${TEST_OUTPUT}.vg.out" \
  89. --leak-check=full \
  90. --show-reachable=yes \
  91. --run-libc-freeres=yes \
  92. "\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT}
  93. err=$?
  94. else
  95. eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT}
  96. err=$?
  97. fi
  98. if [ $err -ne 0 ] ; then
  99. echo "ERROR: \"${TEST_COMMAND} $@\" exited with non-zero exit status: $err" 1>&2
  100. fi
  101. if [ $use_valgrind -eq 1 ] ; then
  102. if ! tail -1 "${TEST_OUTPUT}.vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then
  103. echo "ERROR: valgrind found errors during execution:" 1>&2
  104. cat "${TEST_OUTPUT}.vg.out"
  105. err=1
  106. fi
  107. fi
  108. if ! "$CMP" -s "${srcdir}/${TEST_OUTPUT}.expected" "${TEST_OUTPUT}.out" ; then
  109. echo "ERROR: \"${TEST_COMMAND} $@\" (${TEST_OUTPUT}) failed (set VERBOSE=1 to see full output):" 1>&2
  110. (cd "${CURDIR}" ; set -x ; diff "${srcdir}/${TEST_OUTPUT}.expected" "$testsubdir/${TEST_OUTPUT}.out")
  111. echo "cp \"$testsubdir/${TEST_OUTPUT}.out\" \"${srcdir}/${TEST_OUTPUT}.expected\"" 1>&2
  112. err=1
  113. fi
  114. return $err
  115. }