1 # Common helper routines for test shell scripts -- intended to be sourced by them
5 #### The following exported variables are set by "make check". Ensure
6 #### that they are set here so that individual tests can be run
7 #### outside of make. Requires that MH_OBJ_DIR be set on entry.
8 test -z "$MH_TEST_DIR" && MH_TEST_DIR="$MH_OBJ_DIR/test/testdir"
9 test -z "$MH_INST_DIR" && MH_INST_DIR="${MH_TEST_DIR}/inst"
10 test -z "$prefix" && prefix=@prefix@
11 test -z "$datarootdir" && datarootdir=@datarootdir@
12 test -z "$exec_prefix" && exec_prefix=@exec_prefix@
13 test -z "$bindir" && bindir="@bindir@"
14 test -z "$mandir" && mandir="@mandir@"
15 test -z "$nmhetcdir" && nmhetcdir="@sysconfdir@/nmh"
16 #### The following doesn't support running the distcheck version of
17 #### test-mhparam standalone, but only via make distcheck.
18 test -z "$nmhetcdirinst" && nmhetcdirinst="@nmhetcdirinst@$nmhetcdir"
19 test -z "$nmhlibexecdir" && nmhlibexecdir="@libexecdir@/nmh"
20 test -z "$supported_locks" && supported_locks="@supported_locks@"
21 test -z "$default_locking" && default_locking="@default_locking@"
22 test -z "$MULTIBYTE_ENABLED" && MULTIBYTE_ENABLED="@MULTIBYTE_ENABLED@"
23 test -z "$ICONV_ENABLED" && ICONV_ENABLED="@ICONV_ENABLED@"
24 export MH_TEST_DIR MH_INST_DIR
25 export bindir mandir nmhetcdir nmhetcdirinst nmhlibexecdir
26 export MULTIBYTE_ENABLED ICONV_ENABLED
28 unset MAILDROP MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE
29 unset MHLDEBUG MHPDEBUG MHWDEBUG PAGER
30 #### Set LC_ALL in individual tests as needed. Unset these so
31 #### that we don't depend on user's settings in other tests.
32 unset LANG LC_ALL LC_CTYPE
34 #### Use a test dir for tmp files when MHTMPDIR applies.
35 MHTMPDIR=$MH_TEST_DIR/Mail
40 #### Output just the checksum. If the filename needs to appear on
41 #### the same line, the caller needs to add it. This avoids
42 #### differences due to a leading '*' binary file indicator, for
43 #### text files, on some platforms (Cygwin).
44 @MD5SUM@ $* | @MD5FMT@ | awk '{print $1}'
47 #### Use built-in $((...)) in test suite if shell supports it.
48 #### Borrowed from configure's as_fn_arith. The result is placed
49 #### in global arith_val.
50 #### Detected at run-time instead of by configure to allow testing
51 #### with different shells.
52 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev/null); then
53 eval 'arith_eval () { arith_val=$(( $* )); }'
55 arith_eval () { arith_val=`expr "$@" || test $? -eq 1`; }
61 echo "$Test $0 SKIP ($WHY)"
65 # portable implementation of 'which' utility
69 #### Don't need to save current IFS because this function is run in
76 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
77 printf '%s\n' "$D/$PROG"
85 if [ -z "`findprog $1`" ]; then
86 test_skip "missing $1"
90 # Skip test if none of the offered locales are supported.
93 for locale in "$@"; do
94 if locale -a | grep -i "$locale" >/dev/null; then
99 test_skip "no suitable locale available"
102 # Some stuff for doing silly progress indicators
109 arith_eval $LAST - $FIRST; RANGE=$arith_val
110 arith_eval $THIS - $FIRST; PROG=$arith_val
111 # this automatically rounds to nearest integer
112 arith_eval 100 \* $PROG / $RANGE; PERC=$arith_val
113 # note \r so next update will overwrite
114 printf '%3d%%\r' $PERC
122 # don't emit anything if stdout is not connected to a tty.
133 check_for_hard_links () {
136 printf '' > "${MH_TEST_DIR}/$$-1"
137 if link "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev/null; then
138 hard_links_supported=1
140 hard_links_supported=0
142 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
147 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
148 #### but sufficient for our purpose.
149 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
154 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
156 sed 's/'"`printf '\\302\\240'`"'/ /g'
159 #### check() requires two arguments, each the name of a file to be
161 #### If the contents are same, the second file is removed. And the
162 #### first file is removed unless there's an optional argument with
163 #### a value of 'keep first'.
164 #### If different, global variable "failed" is incremented.
165 #### If there's an optional 'ignore space' argument, spacing differences
166 #### will not be considered signficant, emulating GNU diff -w. It
167 #### is assumed that the first file has already been run through
174 while [ $# -gt 0 ]; do
176 'keep first') keepfirst=1 ;;
177 'ignore space') ignorespace=1 ;;
178 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
184 if [ "$ignorespace" ]; then
185 #### POSIX diff should support -b.
186 prepare_space <"$second" | diff -b "$first" - >/dev/null && success=1
188 cmp -s "$first" "$second" && success=1
191 if [ "$success" ]; then
192 [ "$keepfirst" ] || rm -f "$first"
196 #### POSIX diff should support -c.
197 diff -c "$first" "$second" || true
199 echo "$0: test failed, outputs are in $first and $second."
200 failed=`expr ${failed:-0} + 1`
201 #### Set return status of the function.
207 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
208 #### variable (to anything) so run_* will use valgrind.
209 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
210 #### Need absolute path to valgrind.supp in case the test does a cd.
211 NMH_TEST_PREFIX="valgrind --quiet --error-exitcode=1 \
212 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
215 #### Run test under another program by setting NMH_TEST_PREFIX
216 #### environment variable to, e.g., 'valgrind --quiet'.
219 #### Don't run valgrind on shell built-in.
221 *) ${NMH_TEST_PREFIX} "$@" ;;
226 #### run_test() requires two arguments, the first is a program and
227 #### arguments, the second is its expected one-line output string.
228 #### If the actual output does not match that string:
229 #### an error message is printed and global variable "failed" is incremented;
230 #### if there is an optional third argument, it is used in the error message.
234 #### Don't run valgrind on shell built-in.
235 eval\ *) actual_output=`$1 2>&1` ;;
236 *) actual_output=`${NMH_TEST_PREFIX} $1 2>&1` ;;
239 if test x"$actual_output" != x"$2"; then
240 echo "$0: ${3:-\"$1\"} expected:" 1>&2
242 echo "but instead got:" 1>&2
243 echo " '$actual_output'" 1>&2
244 failed=`expr ${failed:-0} + 1`
250 MH="${MH_TEST_DIR}/Mail/.mh_profile"
251 MHMTSCONF="${MH_INST_DIR}${nmhetcdir}/mts.conf"
252 MH_LIBEXEC_DIR="${MH_INST_DIR}${nmhlibexecdir}"
253 export MH MHMTSCONF MH_LIBEXEC_DIR
258 if [ -d "${MH_INST_DIR}${bindir}" ]; then
261 (cd "${MH_OBJ_DIR}" &&
262 make DESTDIR="${MH_INST_DIR}" SETGID_MAIL= install) ||
265 #### Don't test with sendmail because it would really send the
266 #### mail. If configured to use sendmail, change to smtp instead
267 #### so that we use fakesmtp.
268 #### And set up the maildrop in the test directory so tests don't
269 #### use the user's real maildrop.
270 #### test-slocal needs to look at the original mts.conf, so save it.
271 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
272 sed -e 's/mts: *.*/mts: smtp/' \
273 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
274 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
275 "${MHMTSCONF}.old" >"${MHMTSCONF}"
278 #### On Solaris, must set PATH after the install!
279 PATH="${MH_INST_DIR}${bindir}:${PATH}"
282 # clean old test data
283 trap "cd $MH_TEST_DIR; rm -rf '$MH_TEST_DIR/Mail'" 0
285 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
286 cat > "$MH" <<EOF || exit 1
287 Path: ${MH_TEST_DIR}/Mail
288 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
289 fileproc: ${MH_INST_DIR}${bindir}/refile
290 libexecdir: ${MH_LIBEXEC_DIR}
291 mhbuild: -nocontentid
292 mhlproc: ${MH_LIBEXEC_DIR}/mhl
294 postproc: ${MH_LIBEXEC_DIR}/post
295 showproc: ${MH_LIBEXEC_DIR}/mhl
298 folder -create +inbox > /dev/null
299 # create 10 basic messages
300 for i in 1 2 3 4 5 6 7 8 9 10;
302 cat > $MH_TEST_DIR/Mail/inbox/$i <<EOF || exit 1
303 From: Test$i <test$i@example.com>
304 To: Some User <user@example.com>
305 Date: Fri, 29 Sep 2006 00:00:00
306 Message-Id: $i@test.nmh
307 Subject: Testing message $i
309 This is message number $i