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 "$prefix" && prefix
=@prefix@
15 test -z "$datarootdir" && datarootdir
=@datarootdir@
16 test -z "$exec_prefix" && exec_prefix
=@exec_prefix@
17 test -z "$bindir" && bindir
="@bindir@"
18 test -z "$mandir" && mandir
="@mandir@"
19 test -z "$nmhetcdir" && nmhetcdir
="@sysconfdir@/nmh"
20 #### The following doesn't support running the distcheck version of
21 #### test-mhparam standalone, but only via make distcheck.
22 test -z "$nmhetcdirinst" && nmhetcdirinst
="@nmhetcdirinst@$nmhetcdir"
23 test -z "$nmhlibexecdir" && nmhlibexecdir
="@libexecdir@/nmh"
24 test -z "$supported_locks" && supported_locks
="@supported_locks@"
25 test -z "$default_locking" && default_locking
="@default_locking@"
26 test -z "$MULTIBYTE_ENABLED" && MULTIBYTE_ENABLED
="@MULTIBYTE_ENABLED@"
27 test -z "$ICONV_ENABLED" && ICONV_ENABLED
="@ICONV_ENABLED@"
29 #### If w3m is used, HOME needs to be set, assuming default w3m config.
30 #### So make sure that HOME is set to avoid run-time warning about not
31 #### being able to create config directory.
32 test -z "$HOME" && HOME
=$MH_TEST_DIR
36 unset MAILDROP MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE
37 unset MHLDEBUG MHPDEBUG MHWDEBUG PAGER
38 #### Set LC_ALL in individual tests as needed. Unset these so
39 #### that we don't depend on user's settings in other tests.
40 unset LANG LC_ALL LC_CTYPE
42 #### Use a test dir for tmp files when MHTMPDIR applies.
43 MHTMPDIR
=$MH_TEST_DIR/Mail
46 #### If you're reading this .... you can set MH_TEST_NOCLEANUP to prevent
47 #### the test suite from cleaning up the results of a test run, if you need
48 #### to do manual debugging on a test.
52 #### Output just the checksum. If the filename needs to appear on
53 #### the same line, the caller needs to add it. This avoids
54 #### differences due to a leading '*' binary file indicator, for
55 #### text files, on some platforms (Cygwin).
56 @MD5SUM@ $
* | @MD5FMT@
| awk '{print $1}'
59 #### Use built-in $((...)) in test suite if shell supports it.
60 #### Borrowed from configure's as_fn_arith. The result is placed
61 #### in global arith_val.
62 #### Detected at run-time instead of by configure to allow testing
63 #### with different shells.
64 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev
/null
); then
65 eval 'arith_eval () { arith_val=$(( $* )); }'
67 arith_eval
() { arith_val
=`expr "$@" || test $? -eq 1`; }
73 echo "$Test $0 SKIP ($WHY)"
77 # portable implementation of 'which' utility
81 #### Don't need to save current IFS because this function is run in
88 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
89 printf '%s\n' "$D/$PROG"
97 if [ -z "`findprog $1`" ]; then
98 test_skip
"missing $1"
102 # Skip test if none of the offered locales are supported.
105 for locale
in "$@"; do
106 if locale
-a | grep -i "$locale" >/dev
/null
; then
111 test_skip
"no suitable locale available"
114 # Some stuff for doing silly progress indicators
121 arith_eval
$LAST - $FIRST; RANGE
=$arith_val
122 arith_eval
$THIS - $FIRST; PROG
=$arith_val
123 # this automatically rounds to nearest integer
124 arith_eval
100 \
* $PROG / $RANGE; PERC
=$arith_val
125 # note \r so next update will overwrite
126 printf '%3d%%\r' $PERC
134 # don't emit anything if stdout is not connected to a tty.
145 check_for_hard_links
() {
148 printf '' > "${MH_TEST_DIR}/$$-1"
149 xdir_links_supported
=0
150 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev
/null
; then
151 hard_links_supported
=1
152 mkdir "${MH_TEST_DIR}/xlinkdir"
153 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/xlinkdir/$$-2" 2>/dev
/null
; then
154 xdir_links_supported
=1
157 hard_links_supported
=0
159 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
160 rm -rf "${MH_TEST_DIR}/xlinkdir"
165 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
166 #### but sufficient for our purpose.
167 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
172 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
174 sed 's/'"`printf '\\302\\240'`"'/ /g'
177 #### check() requires two arguments, each the name of a file to be
179 #### If the contents are same, the second file is removed. If different,
180 #### global variable "failed" is incremented.
181 #### Optional arguments:
182 #### 'keep first' -- first file is removed unless this is present.
183 #### 'ignore space' -- spacing differences will not be considered
184 #### significant, emulating GNU diff -w. It is assumed that the
185 #### first file has already been run through prepare_space.
186 #### ':' <test name> -- will print '<test name>' in the failure message,
187 #### to make it easier to tell which of multiple tests has failed.
194 while [ $# -gt 0 ]; do
196 'keep first') keepfirst
=1 ;;
197 'ignore space') ignorespace
=1 ;;
198 ':') shift; label
=\'"$*"\'; break ;;
199 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
205 if [ "$ignorespace" ]; then
206 #### POSIX diff should support -b.
207 prepare_space
<"$second" | diff -b "$first" - >/dev
/null
&& success
=1
209 cmp -s "$first" "$second" && success
=1
212 if [ "$success" ]; then
213 [ "$keepfirst" ] || rm -f "$first"
217 #### POSIX diff should support -c.
218 diff -c "$first" "$second" || true
220 echo "$0: $label failed, outputs are in $first and $second."
221 failed
=`expr ${failed:-0} + 1`
222 #### Set return status of the function.
228 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
229 #### variable (to anything) so run_* will use valgrind.
230 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
231 #### Need absolute path to valgrind.supp in case the test does a cd.
232 NMH_TEST_PREFIX
="valgrind --quiet --error-exitcode=1 \
233 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
236 #### Run test under another program by setting NMH_TEST_PREFIX
237 #### environment variable to, e.g., 'valgrind --quiet'.
240 #### Don't run valgrind on shell built-in.
242 *) ${NMH_TEST_PREFIX} "$@" ;;
247 #### run_test() requires two arguments, the first is a program and
248 #### arguments, the second is its expected one-line output string.
249 #### If the actual output does not match that string:
250 #### an error message is printed and global variable "failed" is incremented;
251 #### if there is an optional third argument, it is used in the error message.
255 #### Don't run valgrind on shell built-in.
256 eval\
*) actual_output
=`$1 2>&1` ;;
257 *) actual_output
=`${NMH_TEST_PREFIX} $1 2>&1` ;;
260 if test x
"$actual_output" != x
"$2"; then
261 echo "$0: ${3:-\"$1\"} expected:" 1>&2
263 echo "but instead got:" 1>&2
264 echo " '$actual_output'" 1>&2
265 failed
=`expr ${failed:-0} + 1`
269 #### Function invoked by trap on exit.
271 #### Save exit status to use as status for this program.
274 #### Clean up test mail space.
275 #### cd to $MH_TEST_DIR before trying to remove its Mail
276 #### subdirectory. rm on Solaris won't remove it if it's in the
277 #### path of the current working directory.
278 test -z "$MH_TEST_NOCLEANUP" && (cd $MH_TEST_DIR; rm -rf "$MH_TEST_DIR"/Mail
)
280 #### Report test name if set, which indicates failure.
281 #### Relies on set -e to invoke the trap which calls
282 #### this function on failure.
284 #### 1) Set test name before running the test, use start_test().
285 #### 2) Unset upon successful completion, use finish_test().
286 if test -n "$nmh_tests_testname"; then
287 echo $nmh_tests_testname failed
290 #### Exit with non-zero status if failure. Failure is defined as either
291 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
293 if test -n "$nmh_tests_testname" -o $status -ne 0; then
294 test $status -ne 0 && exit $status || exit 1
295 test $status -ne 0 && exit 0 || exit 0
299 #### Function to set the test name, and whatever the future brings.
301 nmh_tests_testname
="$1"
304 #### Corresponding function to indicate that the test has finished. It need
305 #### not be called after each test, just the last one in a file.
307 unset nmh_tests_testname
314 MH
="${MH_TEST_DIR}/Mail/.mh_profile"
315 MHMTSCONF
="${MH_INST_DIR}${nmhetcdir}/mts.conf"
316 MH_LIBEXEC_DIR
="${MH_INST_DIR}${nmhlibexecdir}"
317 export MH MHMTSCONF MH_LIBEXEC_DIR
322 if [ -d "${MH_INST_DIR}${bindir}" ]; then
325 (cd "${MH_OBJ_DIR}" &&
326 make DESTDIR
="${MH_INST_DIR}" SETGID_MAIL
= install) ||
329 #### Don't test with sendmail because it would really send the
330 #### mail. If configured to use sendmail, change to smtp instead
331 #### so that we use fakesmtp.
332 #### And set up the maildrop in the test directory so tests don't
333 #### use the user's real maildrop.
334 #### test-slocal needs to look at the original mts.conf, so save it.
335 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
336 sed -e 's/mts: *.*/mts: smtp/' \
337 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
338 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
339 "${MHMTSCONF}.old" >"${MHMTSCONF}"
342 #### On Solaris, must set PATH after the install!
343 PATH
="${MH_INST_DIR}${bindir}:${PATH}"
346 # clean old test data on exit
350 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
351 cat > "$MH" <<EOF || exit 1
352 Path: ${MH_TEST_DIR}/Mail
353 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
354 fileproc: ${MH_INST_DIR}${bindir}/refile
355 libexecdir: ${MH_LIBEXEC_DIR}
356 mhbuild: -nocontentid
357 mhlproc: ${MH_LIBEXEC_DIR}/mhl
359 postproc: ${MH_LIBEXEC_DIR}/post
360 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
361 showproc: ${MH_LIBEXEC_DIR}/mhl
362 #: The following aren't currently used by the test suite, but are
363 #: defined here in case they are in the future:
364 mailproc: ${MH_INST_DIR}${bindir}/mhmail
365 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
366 sendproc: ${MH_INST_DIR}${bindir}/send
367 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
368 whomproc: ${MH_INST_DIR}${bindir}/whom
369 #: incproc and packproc are defined in config.c but not used by any code.
372 if test -z '@nmhetcdirinst@'; then
373 #### This isn't used with make distcheck, so that we can use it to
374 #### be sure that etc files are not used from an existing nmh
376 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
377 #### the directory and provide an argument without quotes to cp.
378 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
381 folder
-create +inbox
> /dev
/null
382 # create 10 basic messages
383 for i
in 1 2 3 4 5 6 7 8 9 10;
385 cat > $MH_TEST_DIR/Mail
/inbox
/$i <<EOF || exit 1
386 From: Test$i <test$i@example.com>
387 To: Some User <user@example.com>
388 Date: Fri, 29 Sep 2006 00:00:00
389 Message-Id: $i@test.nmh
390 Subject: Testing message $i
392 This is message number $i