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
42 # Don't have libcurl(3) use the environment's proxy for tests'
43 # connections to 127.0.0.1.
44 unset http_proxy ALL_PROXY all_proxy
46 #### Use a test dir for tmp files when MHTMPDIR applies.
47 MHTMPDIR
="$MH_TEST_DIR"/Mail
50 #### If you're reading this .... you can set MH_TEST_NOCLEANUP to prevent
51 #### the test suite from cleaning up the results of a test run, if you need
52 #### to do manual debugging on a test.
56 #### Output just the checksum. If the filename needs to appear on
57 #### the same line, the caller needs to add it. This avoids
58 #### differences due to a leading '*' binary file indicator, for
59 #### text files, on some platforms (Cygwin).
60 @MD5SUM@ $
* | @MD5FMT@
| awk '{print $1}'
63 #### Use built-in $((...)) in test suite if shell supports it.
64 #### Borrowed from configure's as_fn_arith. The result is placed
65 #### in global arith_val.
66 #### Detected at run-time instead of by configure to allow testing
67 #### with different shells.
68 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev
/null
); then
69 eval 'arith_eval () { arith_val=$(( $* )); }'
71 arith_eval
() { arith_val
=`expr "$@" || test $? -eq 1`; }
77 echo "$0: skipped: $why"
81 # portable implementation of 'which' utility
85 #### Don't need to save current IFS because this function is run in
92 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
93 printf '%s\n' "$D/$PROG"
101 if [ -z "`findprog $1`" ]; then
102 test_skip
"missing $1"
106 # Skip test if none of the offered locales are supported.
107 # As side effect, use the first locale that is found. Note that
108 # some platforms allow, by way of example, en_US.UTF-8 to be used
109 # even though en_US.UTF8 is listed by locale -a. But by setting
110 # LC_ALL here, we don't rely on that.
113 for locale
in "$@"; do
114 if locale
-a | grep -i "$locale" >/dev
/null
; then
115 LC_ALL
="$locale"; export LC_ALL
120 test_skip
"no suitable locale available"
123 # Some stuff for doing silly progress indicators
130 arith_eval
$LAST - $FIRST; RANGE
=$arith_val
131 arith_eval
$THIS - $FIRST; PROG
=$arith_val
132 # this automatically rounds to nearest integer
133 arith_eval
100 \
* $PROG / $RANGE; PERC
=$arith_val
134 # note \r so next update will overwrite
135 printf '%3d%%\r' $PERC
143 # don't emit anything if stdout is not connected to a tty.
154 check_for_hard_links
() {
157 printf '' > "${MH_TEST_DIR}/$$-1"
158 xdir_links_supported
=0
159 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev
/null
; then
160 hard_links_supported
=1
161 mkdir "${MH_TEST_DIR}/xlinkdir"
162 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/xlinkdir/$$-2" 2>/dev
/null
; then
163 xdir_links_supported
=1
166 hard_links_supported
=0
168 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
169 rm -rf "${MH_TEST_DIR}/xlinkdir"
174 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
175 #### but sufficient for our purpose.
176 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
181 #### Filter that removes blank lines and leading space that lynx inserts.
182 #### but sufficient for our purpose.
183 squeeze_whitespace
() {
184 sed '/^$/D; s/^ *//;'
187 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
189 sed 's/'"`printf '\\302\\240'`"'/ /g'
192 #### check() requires two arguments, each the name of a file to be
194 #### If the contents are same, the second file is removed. If different,
195 #### global variable "failed" is incremented.
196 #### Optional arguments:
197 #### 'keep first' -- first file is removed unless this is present.
198 #### 'ignore space' -- spacing differences will not be considered
199 #### significant, emulating GNU diff -w. It is assumed that the
200 #### first file has already been run through prepare_space.
201 #### ':' <test name> -- will print '<test name>' in the failure message,
202 #### to make it easier to tell which of multiple tests has failed.
209 while [ $# -gt 0 ]; do
211 'keep first') keepfirst
=1 ;;
212 'ignore space') ignorespace
=1 ;;
213 ':') shift; label
=\'"$*"\'; break ;;
214 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
220 if [ "$ignorespace" ]; then
221 #### POSIX diff should support -b.
222 prepare_space
<"$second" | diff -b "$first" - >/dev
/null
&& success
=1
224 cmp -s "$first" "$second" && success
=1
227 if [ "$success" ]; then
228 [ "$keepfirst" ] || rm -f "$first"
232 #### POSIX diff should support -c.
233 diff -c "$first" "$second" || true
235 echo "$0: $label failed, outputs are in $first and $second."
236 failed
=`expr ${failed:-0} + 1`
237 #### Set return status of the function.
243 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
244 #### variable (to anything) so run_* will use valgrind.
245 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
246 #### Need absolute path to valgrind.supp in case the test does a cd.
247 NMH_TEST_PREFIX
="valgrind --quiet --error-exitcode=1 \
248 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
251 #### Run test under another program by setting NMH_TEST_PREFIX
252 #### environment variable to, e.g., 'valgrind --quiet'.
255 #### Don't run valgrind on shell built-in.
257 *) ${NMH_TEST_PREFIX} "$@" ;;
262 #### run_test() requires two arguments, the first is a program and
263 #### arguments, the second is its expected one-line output string.
264 #### If the actual output does not match that string:
265 #### an error message is printed and global variable "failed" is incremented;
266 #### if there is an optional third argument, it is used in the error message.
270 #### Don't run valgrind on shell built-in.
271 eval\
*) actual_output
=`$1 2>&1` ;;
272 *) actual_output
=`${NMH_TEST_PREFIX} $1 2>&1` ;;
275 if test x
"$actual_output" != x
"$2"; then
276 echo "$0: ${3:-\"$1\"} expected:" 1>&2
278 echo "but instead got:" 1>&2
279 echo " '$actual_output'" 1>&2
280 failed
=`expr ${failed:-0} + 1`
284 #### Run a command but don't wait for user input. We switched from script(1)
285 #### to our own utility, runpty. Check that it makes the command look like
286 #### it's connected to a terminal below.
287 #### The single argument is the name of the file in which to place the output.
288 run_without_input
() {
291 runpty
="${MH_OBJ_DIR}/test/runpty"
293 #### Create a command to use as a shell for script.
294 cmd
="${MH_TEST_DIR}/Mail/cmd"
301 "${runpty}" "${actual}" "${cmd}"
304 #### Ensure that runpty makes the program look like it's connected to a
305 #### terminal. The welcome message code in sbr/utils.c needs that.
306 require_runpty_to_simulate_tty
() {
307 actual
="${MH_TEST_DIR}/test-runpty"
308 run_without_input
"${actual}" \
309 'if test -t 0 -a -t 1 -a -t 2; then echo tty; fi'
310 grep tty
"${actual}" >/dev
/null
|| test_skip
"runpty doesn't simulate tty"
314 #### check_exit() runs the command in $2..., capturing stdout and
315 #### stderr, and then tests its exit status with the test(1) condition
316 #### in $1. If that fails, the captured stdout and stderr are
317 #### displayed, and $failed incremented.
320 t
="$MH_TEST_DIR/.check_exit"
322 $NMH_TEST_PREFIX "$@" >"$t.1" 2>"$t.2" || r
=$?
326 echo "$0: check_exit: $r $c failed: $*" >&2
328 sed 's/^/ /' "$t.1" >&2
330 sed 's/^/ /' "$t.2" >&2
331 failed
=`expr ${failed:-0} + 1`
335 #### Function invoked by trap on exit.
337 #### Save exit status to use as status for this program.
340 #### Clean up test mail space.
341 #### cd to $MH_TEST_DIR before trying to remove its Mail
342 #### subdirectory. rm on Solaris won't remove it if it's in the
343 #### path of the current working directory.
344 test -z "$MH_TEST_NOCLEANUP" && (cd "$MH_TEST_DIR" && rm -rf "$MH_TEST_DIR"/Mail
)
346 #### Report test name if set, which indicates failure.
347 #### Relies on set -e to invoke the trap which calls
348 #### this function on failure.
350 #### 1) Set test name before running the test, use start_test().
351 #### 2) Unset upon successful completion, use finish_test().
352 if test -n "$nmh_tests_testname"; then
353 echo "first named test failure: $nmh_tests_testname"
356 #### Exit with non-zero status if failure. Failure is defined as either
357 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
359 if test -n "$nmh_tests_testname" -o $status -ne 0; then
360 test $status -ne 0 && exit $status || exit 1
361 test $status -ne 0 && exit 0 || exit 0
365 #### Function to set the test name, and whatever the future brings.
367 #### run_test disables exit on non-zero status, but does increment
368 #### failed. Don't overwrite nmh_tests_testname if there was a
369 #### failure; remember the first test that failed.
370 [ ${failed:-0} -eq 0 ] && nmh_tests_testname
="$1"
373 #### Corresponding function to indicate that the test has finished. It need
374 #### not be called after each test, just the last one in a file.
376 #### run_test disables exit on non-zero status, but does increment
377 #### failed. Don't unset nmh_tests_testname if there was a failure.
378 [ ${failed:-0} -eq 0 ] && unset nmh_tests_testname
385 MH
="${MH_TEST_DIR}/Mail/.mh_profile"
386 MHMTSCONF
="${MH_INST_DIR}${nmhetcdir}/mts.conf"
387 MH_LIBEXEC_DIR
="${MH_INST_DIR}${nmhlibexecdir}"
388 export MH MHMTSCONF MH_LIBEXEC_DIR
393 if [ -d "${MH_INST_DIR}${bindir}" ]; then
396 (cd "${MH_OBJ_DIR}" &&
397 make DESTDIR
="${MH_INST_DIR}" SETGID_MAIL
= install) ||
400 #### Don't test with sendmail because it would really send the
401 #### mail. If configured to use sendmail, change to smtp instead
402 #### so that we use fakesmtp.
403 #### And set up the maildrop in the test directory so tests don't
404 #### use the user's real maildrop.
405 #### test-slocal needs to look at the original mts.conf, so save it.
406 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
407 sed -e 's/mts: *.*/mts: smtp/' \
408 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
409 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
410 "${MHMTSCONF}.old" >"${MHMTSCONF}"
413 #### On Solaris, must set PATH after the install!
414 PATH
="${MH_INST_DIR}${bindir}:${PATH}"
417 # clean old test data on exit
421 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
422 cat > "$MH" <<EOF || exit 1
423 Path: ${MH_TEST_DIR}/Mail
424 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
425 fileproc: ${MH_INST_DIR}${bindir}/refile
426 libexecdir: ${MH_LIBEXEC_DIR}
427 mhbuild: -nocontentid
428 mhlproc: ${MH_LIBEXEC_DIR}/mhl
430 postproc: ${MH_LIBEXEC_DIR}/post
431 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
432 showproc: ${MH_LIBEXEC_DIR}/mhl
433 #: The following aren't currently used by the test suite, but are
434 #: defined here in case they are in the future:
435 mailproc: ${MH_INST_DIR}${bindir}/mhmail
436 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
437 sendproc: ${MH_INST_DIR}${bindir}/send
438 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
439 whomproc: ${MH_INST_DIR}${bindir}/whom
440 #: incproc and packproc are defined in config.c but not used by any code.
443 if test -z '@nmhetcdirinst@'; then
444 #### This isn't used with make distcheck, so that we can use it to
445 #### be sure that etc files are not used from an existing nmh
447 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
448 #### the directory and provide an argument without quotes to cp.
449 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
452 folder
-create +inbox
> /dev
/null
453 #### Suppress warnings about a new version.
454 #### mhparam version includes the nmh- prefix, so be consistent with that.
455 printf 'Version: nmh-%s\n' "${MH_VERSION}" >> "${MH_TEST_DIR}"/Mail
/context
457 # create 10 basic messages
458 for i
in 1 2 3 4 5 6 7 8 9 10;
460 cat > $MH_TEST_DIR/Mail
/inbox
/$i <<EOF || exit 1
461 From: Test$i <test$i@example.com>
462 To: Some User <user@example.com>
463 Date: Fri, 29 Sep 2006 00:00:00
464 Message-Id: $i@test.nmh
465 Subject: Testing message $i
467 This is message number $i