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