You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test-defs.sh 3.3 kB

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