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 MHPDEBUG 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 "$Test $0 SKIP ($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 converts non-breakable space U+00A0 to an ASCII space.
180 sed 's/'"`printf '\\302\\240'`"'/ /g'
183 #### check() requires two arguments, each the name of a file to be
185 #### If the contents are same, the second file is removed. If different,
186 #### global variable "failed" is incremented.
187 #### Optional arguments:
188 #### 'keep first' -- first file is removed unless this is present.
189 #### 'ignore space' -- spacing differences will not be considered
190 #### significant, emulating GNU diff -w. It is assumed that the
191 #### first file has already been run through prepare_space.
192 #### ':' <test name> -- will print '<test name>' in the failure message,
193 #### to make it easier to tell which of multiple tests has failed.
200 while [ $# -gt 0 ]; do
202 'keep first') keepfirst
=1 ;;
203 'ignore space') ignorespace
=1 ;;
204 ':') shift; label
=\'"$*"\'; break ;;
205 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
211 if [ "$ignorespace" ]; then
212 #### POSIX diff should support -b.
213 prepare_space
<"$second" | diff -b "$first" - >/dev
/null
&& success
=1
215 cmp -s "$first" "$second" && success
=1
218 if [ "$success" ]; then
219 [ "$keepfirst" ] || rm -f "$first"
223 #### POSIX diff should support -c.
224 diff -c "$first" "$second" || true
226 echo "$0: $label failed, outputs are in $first and $second."
227 failed
=`expr ${failed:-0} + 1`
228 #### Set return status of the function.
234 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
235 #### variable (to anything) so run_* will use valgrind.
236 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
237 #### Need absolute path to valgrind.supp in case the test does a cd.
238 NMH_TEST_PREFIX
="valgrind --quiet --error-exitcode=1 \
239 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
242 #### Run test under another program by setting NMH_TEST_PREFIX
243 #### environment variable to, e.g., 'valgrind --quiet'.
246 #### Don't run valgrind on shell built-in.
248 *) ${NMH_TEST_PREFIX} "$@" ;;
253 #### run_test() requires two arguments, the first is a program and
254 #### arguments, the second is its expected one-line output string.
255 #### If the actual output does not match that string:
256 #### an error message is printed and global variable "failed" is incremented;
257 #### if there is an optional third argument, it is used in the error message.
261 #### Don't run valgrind on shell built-in.
262 eval\
*) actual_output
=`$1 2>&1` ;;
263 *) actual_output
=`${NMH_TEST_PREFIX} $1 2>&1` ;;
266 if test x
"$actual_output" != x
"$2"; then
267 echo "$0: ${3:-\"$1\"} expected:" 1>&2
269 echo "but instead got:" 1>&2
270 echo " '$actual_output'" 1>&2
271 failed
=`expr ${failed:-0} + 1`
275 #### check_exit() runs the command in $2..., capturing stdout and
276 #### stderr, and then tests its exit status with the test(1) condition
277 #### in $1. If that fails, the captured stdout and stderr are
278 #### displayed, and $failed incremented.
281 t
="$MH_TEST_DIR/.check_exit"
283 $NMH_TEST_PREFIX "$@" >"$t.1" 2>"$t.2" || r
=$?
287 echo "$0: check_exit: $r $c failed: $*" >&2
289 sed 's/^/ /' "$t.1" >&2
291 sed 's/^/ /' "$t.2" >&2
292 failed
=`expr ${failed:-0} + 1`
296 #### Function invoked by trap on exit.
298 #### Save exit status to use as status for this program.
301 #### Clean up test mail space.
302 #### cd to $MH_TEST_DIR before trying to remove its Mail
303 #### subdirectory. rm on Solaris won't remove it if it's in the
304 #### path of the current working directory.
305 test -z "$MH_TEST_NOCLEANUP" && (cd "$MH_TEST_DIR"; rm -rf "$MH_TEST_DIR"/Mail
)
307 #### Report test name if set, which indicates failure.
308 #### Relies on set -e to invoke the trap which calls
309 #### this function on failure.
311 #### 1) Set test name before running the test, use start_test().
312 #### 2) Unset upon successful completion, use finish_test().
313 if test -n "$nmh_tests_testname"; then
314 echo $nmh_tests_testname failed
317 #### Exit with non-zero status if failure. Failure is defined as either
318 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
320 if test -n "$nmh_tests_testname" -o $status -ne 0; then
321 test $status -ne 0 && exit $status || exit 1
322 test $status -ne 0 && exit 0 || exit 0
326 #### Function to set the test name, and whatever the future brings.
328 #### run_test disables exit on non-zero status, but does increment
329 #### failed. Don't overwrite nmh_tests_testname if there was a
330 #### failure; remember the first test that failed.
331 [ ${failed:-0} -eq 0 ] && nmh_tests_testname
="$1"
334 #### Corresponding function to indicate that the test has finished. It need
335 #### not be called after each test, just the last one in a file.
337 #### run_test disables exit on non-zero status, but does increment
338 #### failed. Don't unset nmh_tests_testname if there was a failure.
339 [ ${failed:-0} -eq 0 ] && unset nmh_tests_testname
346 MH
="${MH_TEST_DIR}/Mail/.mh_profile"
347 MHMTSCONF
="${MH_INST_DIR}${nmhetcdir}/mts.conf"
348 MH_LIBEXEC_DIR
="${MH_INST_DIR}${nmhlibexecdir}"
349 export MH MHMTSCONF MH_LIBEXEC_DIR
354 if [ -d "${MH_INST_DIR}${bindir}" ]; then
357 (cd "${MH_OBJ_DIR}" &&
358 make DESTDIR
="${MH_INST_DIR}" SETGID_MAIL
= install) ||
361 #### Don't test with sendmail because it would really send the
362 #### mail. If configured to use sendmail, change to smtp instead
363 #### so that we use fakesmtp.
364 #### And set up the maildrop in the test directory so tests don't
365 #### use the user's real maildrop.
366 #### test-slocal needs to look at the original mts.conf, so save it.
367 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
368 sed -e 's/mts: *.*/mts: smtp/' \
369 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
370 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
371 "${MHMTSCONF}.old" >"${MHMTSCONF}"
374 #### On Solaris, must set PATH after the install!
375 PATH
="${MH_INST_DIR}${bindir}:${PATH}"
378 # clean old test data on exit
382 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
383 cat > "$MH" <<EOF || exit 1
384 Path: ${MH_TEST_DIR}/Mail
385 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
386 fileproc: ${MH_INST_DIR}${bindir}/refile
387 libexecdir: ${MH_LIBEXEC_DIR}
388 mhbuild: -nocontentid
389 mhlproc: ${MH_LIBEXEC_DIR}/mhl
391 postproc: ${MH_LIBEXEC_DIR}/post
392 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
393 showproc: ${MH_LIBEXEC_DIR}/mhl
394 #: The following aren't currently used by the test suite, but are
395 #: defined here in case they are in the future:
396 mailproc: ${MH_INST_DIR}${bindir}/mhmail
397 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
398 sendproc: ${MH_INST_DIR}${bindir}/send
399 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
400 whomproc: ${MH_INST_DIR}${bindir}/whom
401 #: incproc and packproc are defined in config.c but not used by any code.
404 if test -z '@nmhetcdirinst@'; then
405 #### This isn't used with make distcheck, so that we can use it to
406 #### be sure that etc files are not used from an existing nmh
408 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
409 #### the directory and provide an argument without quotes to cp.
410 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
413 folder
-create +inbox
> /dev
/null
414 #### Suppress warnings about a new version.
415 #### mhparam version includes the nmh- prefix, so be consistent with that.
416 printf 'Version: nmh-%s\n' "${MH_VERSION}" >> "${MH_TEST_DIR}"/Mail
/context
418 # create 10 basic messages
419 for i
in 1 2 3 4 5 6 7 8 9 10;
421 cat > $MH_TEST_DIR/Mail
/inbox
/$i <<EOF || exit 1
422 From: Test$i <test$i@example.com>
423 To: Some User <user@example.com>
424 Date: Fri, 29 Sep 2006 00:00:00
425 Message-Id: $i@test.nmh
426 Subject: Testing message $i
428 This is message number $i