1 # Common helper routines for test shell scripts -- to be sourced by them
5 #### The following 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 #### Use the result of cd and pwd -P so that the result will agree
9 #### with what getcwd(3) returns.
10 test -d "$MH_OBJ_DIR/test/testdir" || mkdir -p "$MH_OBJ_DIR/test/testdir"
11 test -z "$MH_TEST_DIR" && MH_TEST_DIR
=`cd "$MH_OBJ_DIR/test/testdir" && pwd -P`
13 test -z "$MH_INST_DIR" && MH_INST_DIR
="${MH_TEST_DIR}/inst"
14 test -z "$MH_VERSION" && MH_VERSION
="@VERSION@"
15 test -z "$prefix" && prefix
=@prefix@
16 test -z "$datarootdir" && datarootdir
=@datarootdir@
17 test -z "$exec_prefix" && exec_prefix
=@exec_prefix@
18 test -z "$bindir" && bindir
="@bindir@"
19 test -z "$mandir" && mandir
="@mandir@"
20 test -z "$nmhetcdir" && nmhetcdir
="@sysconfdir@/nmh"
21 #### The following doesn't support running the distcheck version of
22 #### test-mhparam standalone, but only via make distcheck.
23 test -z "$nmhetcdirinst" && nmhetcdirinst
="@nmhetcdirinst@$nmhetcdir"
24 test -z "$nmhlibexecdir" && nmhlibexecdir
="@libexecdir@/nmh"
25 test -z "$supported_locks" && supported_locks
="@supported_locks@"
26 test -z "$default_locking" && default_locking
="@default_locking@"
27 test -z "$MULTIBYTE_ENABLED" && MULTIBYTE_ENABLED
="@MULTIBYTE_ENABLED@"
28 test -z "$ICONV_ENABLED" && ICONV_ENABLED
="@ICONV_ENABLED@"
29 test -z "$OAUTH_SUPPORT" && OAUTH_SUPPORT
="@OAUTH_SUPPORT@"
30 test -z "$CURL_USER_AGENT" && CURL_USER_AGENT
="@CURL_USER_AGENT@"
32 #### Make sure that HOME is set to avoid run-time warning from w3m about
33 #### not being able to create config directory.
37 unset MAILDROP MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE
38 unset MHLDEBUG MHWDEBUG PAGER XOAUTH SMTPUTF8
39 #### Set LC_ALL in individual tests as needed. Unset these so
40 #### that we don't depend on user's settings in other tests.
41 unset LANG LC_ALL LC_CTYPE
43 #### Use a test dir for tmp files when MHTMPDIR applies.
44 MHTMPDIR
="$MH_TEST_DIR"/Mail
47 #### If you're reading this .... you can set MH_TEST_NOCLEANUP to prevent
48 #### the test suite from cleaning up the results of a test run, if you need
49 #### to do manual debugging on a test.
53 #### Output just the checksum. If the filename needs to appear on
54 #### the same line, the caller needs to add it. This avoids
55 #### differences due to a leading '*' binary file indicator, for
56 #### text files, on some platforms (Cygwin).
57 @MD5SUM@ $
* | @MD5FMT@
| awk '{print $1}'
60 #### Use built-in $((...)) in test suite if shell supports it.
61 #### Borrowed from configure's as_fn_arith. The result is placed
62 #### in global arith_val.
63 #### Detected at run-time instead of by configure to allow testing
64 #### with different shells.
65 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev
/null
); then
66 eval 'arith_eval () { arith_val=$(( $* )); }'
68 arith_eval
() { arith_val
=`expr "$@" || test $? -eq 1`; }
74 echo "$0: skipped: $why"
78 # portable implementation of 'which' utility
82 #### Don't need to save current IFS because this function is run in
89 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
90 printf '%s\n' "$D/$PROG"
98 if [ -z "`findprog $1`" ]; then
99 test_skip
"missing $1"
103 # Skip test if none of the offered locales are supported.
104 # As side effect, use the first locale that is found. Note that
105 # some platforms allow, by way of example, en_US.UTF-8 to be used
106 # even though en_US.UTF8 is listed by locale -a. But by setting
107 # LC_ALL here, we don't rely on that.
110 for locale
in "$@"; do
111 if locale
-a | grep -i "$locale" >/dev
/null
; then
112 LC_ALL
="$locale"; export LC_ALL
117 test_skip
"no suitable locale available"
120 # Some stuff for doing silly progress indicators
127 arith_eval
$LAST - $FIRST; RANGE
=$arith_val
128 arith_eval
$THIS - $FIRST; PROG
=$arith_val
129 # this automatically rounds to nearest integer
130 arith_eval
100 \
* $PROG / $RANGE; PERC
=$arith_val
131 # note \r so next update will overwrite
132 printf '%3d%%\r' $PERC
140 # don't emit anything if stdout is not connected to a tty.
151 check_for_hard_links
() {
154 printf '' > "${MH_TEST_DIR}/$$-1"
155 xdir_links_supported
=0
156 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev
/null
; then
157 hard_links_supported
=1
158 mkdir "${MH_TEST_DIR}/xlinkdir"
159 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/xlinkdir/$$-2" 2>/dev
/null
; then
160 xdir_links_supported
=1
163 hard_links_supported
=0
165 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
166 rm -rf "${MH_TEST_DIR}/xlinkdir"
171 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
172 #### but sufficient for our purpose.
173 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
178 #### Filter that removes blank lines and leading space that lynx inserts.
179 #### but sufficient for our purpose.
180 squeeze_whitespace
() {
181 sed '/^$/D; s/^ *//;'
184 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
186 sed 's/'"`printf '\\302\\240'`"'/ /g'
189 #### check() requires two arguments, each the name of a file to be
191 #### If the contents are same, the second file is removed. If different,
192 #### global variable "failed" is incremented.
193 #### Optional arguments:
194 #### 'keep first' -- first file is removed unless this is present.
195 #### 'ignore space' -- spacing differences will not be considered
196 #### significant, emulating GNU diff -w. It is assumed that the
197 #### first file has already been run through prepare_space.
198 #### ':' <test name> -- will print '<test name>' in the failure message,
199 #### to make it easier to tell which of multiple tests has failed.
206 while [ $# -gt 0 ]; do
208 'keep first') keepfirst
=1 ;;
209 'ignore space') ignorespace
=1 ;;
210 ':') shift; label
=\'"$*"\'; break ;;
211 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
217 if [ "$ignorespace" ]; then
218 #### POSIX diff should support -b.
219 prepare_space
<"$second" | diff -b "$first" - >/dev
/null
&& success
=1
221 cmp -s "$first" "$second" && success
=1
224 if [ "$success" ]; then
225 [ "$keepfirst" ] || rm -f "$first"
229 #### POSIX diff should support -c.
230 diff -c "$first" "$second" || true
232 echo "$0: $label failed, outputs are in $first and $second."
233 failed
=`expr ${failed:-0} + 1`
234 #### Set return status of the function.
240 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
241 #### variable (to anything) so run_* will use valgrind.
242 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
243 #### Need absolute path to valgrind.supp in case the test does a cd.
244 NMH_TEST_PREFIX
="valgrind --quiet --error-exitcode=1 \
245 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
248 #### Run test under another program by setting NMH_TEST_PREFIX
249 #### environment variable to, e.g., 'valgrind --quiet'.
252 #### Don't run valgrind on shell built-in.
254 *) ${NMH_TEST_PREFIX} "$@" ;;
259 #### run_test() requires two arguments, the first is a program and
260 #### arguments, the second is its expected one-line output string.
261 #### If the actual output does not match that string:
262 #### an error message is printed and global variable "failed" is incremented;
263 #### if there is an optional third argument, it is used in the error message.
267 #### Don't run valgrind on shell built-in.
268 eval\
*) actual_output
=`$1 2>&1` ;;
269 *) actual_output
=`${NMH_TEST_PREFIX} $1 2>&1` ;;
272 if test x
"$actual_output" != x
"$2"; then
273 echo "$0: ${3:-\"$1\"} expected:" 1>&2
275 echo "but instead got:" 1>&2
276 echo " '$actual_output'" 1>&2
277 failed
=`expr ${failed:-0} + 1`
281 #### check_exit() runs the command in $2..., capturing stdout and
282 #### stderr, and then tests its exit status with the test(1) condition
283 #### in $1. If that fails, the captured stdout and stderr are
284 #### displayed, and $failed incremented.
287 t
="$MH_TEST_DIR/.check_exit"
289 $NMH_TEST_PREFIX "$@" >"$t.1" 2>"$t.2" || r
=$?
293 echo "$0: check_exit: $r $c failed: $*" >&2
295 sed 's/^/ /' "$t.1" >&2
297 sed 's/^/ /' "$t.2" >&2
298 failed
=`expr ${failed:-0} + 1`
302 #### Function invoked by trap on exit.
304 #### Save exit status to use as status for this program.
307 #### Clean up test mail space.
308 #### cd to $MH_TEST_DIR before trying to remove its Mail
309 #### subdirectory. rm on Solaris won't remove it if it's in the
310 #### path of the current working directory.
311 test -z "$MH_TEST_NOCLEANUP" && (cd "$MH_TEST_DIR" && rm -rf "$MH_TEST_DIR"/Mail
)
313 #### Report test name if set, which indicates failure.
314 #### Relies on set -e to invoke the trap which calls
315 #### this function on failure.
317 #### 1) Set test name before running the test, use start_test().
318 #### 2) Unset upon successful completion, use finish_test().
319 if test -n "$nmh_tests_testname"; then
320 echo "first named test failure: $nmh_tests_testname"
323 #### Exit with non-zero status if failure. Failure is defined as either
324 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
326 if test -n "$nmh_tests_testname" -o $status -ne 0; then
327 test $status -ne 0 && exit $status || exit 1
328 test $status -ne 0 && exit 0 || exit 0
332 #### Function to set the test name, and whatever the future brings.
334 #### run_test disables exit on non-zero status, but does increment
335 #### failed. Don't overwrite nmh_tests_testname if there was a
336 #### failure; remember the first test that failed.
337 [ ${failed:-0} -eq 0 ] && nmh_tests_testname
="$1"
340 #### Corresponding function to indicate that the test has finished. It need
341 #### not be called after each test, just the last one in a file.
343 #### run_test disables exit on non-zero status, but does increment
344 #### failed. Don't unset nmh_tests_testname if there was a failure.
345 [ ${failed:-0} -eq 0 ] && unset nmh_tests_testname
352 MH
="${MH_TEST_DIR}/Mail/.mh_profile"
353 MHMTSCONF
="${MH_INST_DIR}${nmhetcdir}/mts.conf"
354 MH_LIBEXEC_DIR
="${MH_INST_DIR}${nmhlibexecdir}"
355 export MH MHMTSCONF MH_LIBEXEC_DIR
360 if [ -d "${MH_INST_DIR}${bindir}" ]; then
363 (cd "${MH_OBJ_DIR}" &&
364 make DESTDIR
="${MH_INST_DIR}" SETGID_MAIL
= install) ||
367 #### Don't test with sendmail because it would really send the
368 #### mail. If configured to use sendmail, change to smtp instead
369 #### so that we use fakesmtp.
370 #### And set up the maildrop in the test directory so tests don't
371 #### use the user's real maildrop.
372 #### test-slocal needs to look at the original mts.conf, so save it.
373 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
374 sed -e 's/mts: *.*/mts: smtp/' \
375 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
376 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
377 "${MHMTSCONF}.old" >"${MHMTSCONF}"
380 #### On Solaris, must set PATH after the install!
381 PATH
="${MH_INST_DIR}${bindir}:${PATH}"
384 # clean old test data on exit
388 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
389 cat > "$MH" <<EOF || exit 1
390 Path: ${MH_TEST_DIR}/Mail
391 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
392 fileproc: ${MH_INST_DIR}${bindir}/refile
393 libexecdir: ${MH_LIBEXEC_DIR}
394 mhbuild: -nocontentid
395 mhlproc: ${MH_LIBEXEC_DIR}/mhl
397 postproc: ${MH_LIBEXEC_DIR}/post
398 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
399 showproc: ${MH_LIBEXEC_DIR}/mhl
400 #: The following aren't currently used by the test suite, but are
401 #: defined here in case they are in the future:
402 mailproc: ${MH_INST_DIR}${bindir}/mhmail
403 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
404 sendproc: ${MH_INST_DIR}${bindir}/send
405 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
406 whomproc: ${MH_INST_DIR}${bindir}/whom
407 #: incproc and packproc are defined in config.c but not used by any code.
410 if test -z '@nmhetcdirinst@'; then
411 #### This isn't used with make distcheck, so that we can use it to
412 #### be sure that etc files are not used from an existing nmh
414 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
415 #### the directory and provide an argument without quotes to cp.
416 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
419 folder
-create +inbox
> /dev
/null
420 #### Suppress warnings about a new version.
421 #### mhparam version includes the nmh- prefix, so be consistent with that.
422 printf 'Version: nmh-%s\n' "${MH_VERSION}" >> "${MH_TEST_DIR}"/Mail
/context
424 # create 10 basic messages
425 for i
in 1 2 3 4 5 6 7 8 9 10;
427 cat > $MH_TEST_DIR/Mail
/inbox
/$i <<EOF || exit 1
428 From: Test$i <test$i@example.com>
429 To: Some User <user@example.com>
430 Date: Fri, 29 Sep 2006 00:00:00
431 Message-Id: $i@test.nmh
432 Subject: Testing message $i
434 This is message number $i