blob: 8c5b521f01fddfc609e0cf4bda8a0f0b357ffd20 [file] [log] [blame]
w.denge87b5002025-08-20 10:43:03 +08001#!/bin/sh
2
3if [ ! -z "$JSONC_TEST_TRACE" ] ; then
4 VERBOSE=1
5 set -x
6 set -v
7fi
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#
12srcdir=${srcdir-.}
13if test ! -d $srcdir ; then
14 echo "test-defs.sh: installation error" 1>&2
15 exit 1
16fi
17
18# Use absolute paths
19case "$srcdir" in
20 /* | [A-Za-z]:\\*) ;;
21 *) srcdir=`\cd $srcdir && pwd` ;;
22esac
23
24case "$top_builddir" in
25 /* | [A-Za-z]:\\*) ;;
26 *) top_builddir=`\cd ${top_builddir-..} && pwd` ;;
27esac
28
29top_builddir=${top_builddir}/tests
30
31progname=`echo "$0" | sed 's,^.*/,,'`
32testname=`echo "$progname" | sed 's,-.*$,,'`
33testsubdir=${testsubdir-testSubDir}
34testsubdir=${testsubdir}/${progname}
35
36# User can set VERBOSE to cause output redirection
37case "$VERBOSE" in
38[Nn]|[Nn][Oo]|0|"")
39 VERBOSE=0
40 exec > /dev/null
41 ;;
42[Yy]|[Yy][Ee][Ss])
43 VERBOSE=1
44 ;;
45esac
46
47rm -rf "$testsubdir" > /dev/null 2>&1
48mkdir -p "$testsubdir"
49CURDIR=$(pwd)
50cd "$testsubdir" \
51 || { echo "Cannot make or change into $testsubdir"; exit 1; }
52
53echo "=== Running test $progname"
54
55CMP="${CMP-cmp}"
56
57use_valgrind=${USE_VALGRIND-1}
58case "${use_valgrind}" in
59 [0Nn]*)
60 use_valgrind=0
61 ;;
62 *)
63 use_valgrind=1
64 ;;
65esac
66valgrind_path=$(which valgrind 2> /dev/null)
67if [ -z "${valgrind_path}" -o ! -x "${valgrind_path}" ] ; then
68 use_valgrind=0
69fi
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#
84run_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}