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 test -z "$MH_TEST_DIR" && MH_TEST_DIR
="$MH_OBJ_DIR/test/testdir"
10 test -z "$MH_INST_DIR" && MH_INST_DIR
="${MH_TEST_DIR}/inst"
11 test -z "$prefix" && prefix
=@prefix@
12 test -z "$datarootdir" && datarootdir
=@datarootdir@
13 test -z "$exec_prefix" && exec_prefix
=@exec_prefix@
14 test -z "$bindir" && bindir
="@bindir@"
15 test -z "$mandir" && mandir
="@mandir@"
16 test -z "$nmhetcdir" && nmhetcdir
="@sysconfdir@/nmh"
17 #### The following doesn't support running the distcheck version of
18 #### test-mhparam standalone, but only via make distcheck.
19 test -z "$nmhetcdirinst" && nmhetcdirinst
="@nmhetcdirinst@$nmhetcdir"
20 test -z "$nmhlibexecdir" && nmhlibexecdir
="@libexecdir@/nmh"
21 test -z "$supported_locks" && supported_locks
="@supported_locks@"
22 test -z "$default_locking" && default_locking
="@default_locking@"
23 test -z "$MULTIBYTE_ENABLED" && MULTIBYTE_ENABLED
="@MULTIBYTE_ENABLED@"
24 test -z "$ICONV_ENABLED" && ICONV_ENABLED
="@ICONV_ENABLED@"
26 unset MAILDROP MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE
27 unset MHLDEBUG MHPDEBUG MHWDEBUG PAGER
28 #### Set LC_ALL in individual tests as needed. Unset these so
29 #### that we don't depend on user's settings in other tests.
30 unset LANG LC_ALL LC_CTYPE
32 #### Use a test dir for tmp files when MHTMPDIR applies.
33 MHTMPDIR
=$MH_TEST_DIR/Mail
36 #### If you're reading this .... you can set MH_TEST_NOCLEANUP to prevent
37 #### the test suite from cleaning up the results of a test run, if you need
38 #### to do manual debugging on a test.
42 #### Output just the checksum. If the filename needs to appear on
43 #### the same line, the caller needs to add it. This avoids
44 #### differences due to a leading '*' binary file indicator, for
45 #### text files, on some platforms (Cygwin).
46 @MD5SUM@ $
* | @MD5FMT@
| awk '{print $1}'
49 #### Use built-in $((...)) in test suite if shell supports it.
50 #### Borrowed from configure's as_fn_arith. The result is placed
51 #### in global arith_val.
52 #### Detected at run-time instead of by configure to allow testing
53 #### with different shells.
54 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev
/null
); then
55 eval 'arith_eval () { arith_val=$(( $* )); }'
57 arith_eval
() { arith_val
=`expr "$@" || test $? -eq 1`; }
63 echo "$Test $0 SKIP ($WHY)"
67 # portable implementation of 'which' utility
71 #### Don't need to save current IFS because this function is run in
78 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
79 printf '%s\n' "$D/$PROG"
87 if [ -z "`findprog $1`" ]; then
88 test_skip
"missing $1"
92 # Skip test if none of the offered locales are supported.
95 for locale
in "$@"; do
96 if locale
-a | grep -i "$locale" >/dev
/null
; then
101 test_skip
"no suitable locale available"
104 # Some stuff for doing silly progress indicators
111 arith_eval
$LAST - $FIRST; RANGE
=$arith_val
112 arith_eval
$THIS - $FIRST; PROG
=$arith_val
113 # this automatically rounds to nearest integer
114 arith_eval
100 \
* $PROG / $RANGE; PERC
=$arith_val
115 # note \r so next update will overwrite
116 printf '%3d%%\r' $PERC
124 # don't emit anything if stdout is not connected to a tty.
135 check_for_hard_links
() {
138 printf '' > "${MH_TEST_DIR}/$$-1"
139 xdir_links_supported
=0
140 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev
/null
; then
141 hard_links_supported
=1
142 mkdir "${MH_TEST_DIR}/xlinkdir"
143 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/xlinkdir/$$-2" 2>/dev
/null
; then
144 xdir_links_supported
=1
147 hard_links_supported
=0
149 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
150 rm -rf "${MH_TEST_DIR}/xlinkdir"
155 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
156 #### but sufficient for our purpose.
157 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
162 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
164 sed 's/'"`printf '\\302\\240'`"'/ /g'
167 #### check() requires two arguments, each the name of a file to be
169 #### If the contents are same, the second file is removed. If different,
170 #### global variable "failed" is incremented.
171 #### Optional arguments:
172 #### 'keep first' -- first file is removed unless this is present.
173 #### 'ignore space' -- spacing differences will not be considered
174 #### significant, emulating GNU diff -w. It is assumed that the
175 #### first file has already been run through prepare_space.
176 #### ':' <test name> -- will print '<test name>' in the failure message,
177 #### to make it easier to tell which of multiple tests has failed.
184 while [ $# -gt 0 ]; do
186 'keep first') keepfirst
=1 ;;
187 'ignore space') ignorespace
=1 ;;
188 ':') shift; label
=\'"$*"\'; break ;;
189 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
195 if [ "$ignorespace" ]; then
196 #### POSIX diff should support -b.
197 prepare_space
<"$second" | diff -b "$first" - >/dev
/null
&& success
=1
199 cmp -s "$first" "$second" && success
=1
202 if [ "$success" ]; then
203 [ "$keepfirst" ] || rm -f "$first"
207 #### POSIX diff should support -c.
208 diff -c "$first" "$second" || true
210 echo "$0: $label failed, outputs are in $first and $second."
211 failed
=`expr ${failed:-0} + 1`
212 #### Set return status of the function.
218 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
219 #### variable (to anything) so run_* will use valgrind.
220 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
221 #### Need absolute path to valgrind.supp in case the test does a cd.
222 NMH_TEST_PREFIX
="valgrind --quiet --error-exitcode=1 \
223 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
226 #### Run test under another program by setting NMH_TEST_PREFIX
227 #### environment variable to, e.g., 'valgrind --quiet'.
230 #### Don't run valgrind on shell built-in.
232 *) ${NMH_TEST_PREFIX} "$@" ;;
237 #### run_test() requires two arguments, the first is a program and
238 #### arguments, the second is its expected one-line output string.
239 #### If the actual output does not match that string:
240 #### an error message is printed and global variable "failed" is incremented;
241 #### if there is an optional third argument, it is used in the error message.
245 #### Don't run valgrind on shell built-in.
246 eval\
*) actual_output
=`$1 2>&1` ;;
247 *) actual_output
=`${NMH_TEST_PREFIX} $1 2>&1` ;;
250 if test x
"$actual_output" != x
"$2"; then
251 echo "$0: ${3:-\"$1\"} expected:" 1>&2
253 echo "but instead got:" 1>&2
254 echo " '$actual_output'" 1>&2
255 failed
=`expr ${failed:-0} + 1`
259 #### Function invoked by trap on exit.
261 #### Save exit status to use as status for this program.
264 #### Clean up test mail space.
265 #### cd to $MH_TEST_DIR before trying to remove its Mail
266 #### subdirectory. rm on Solaris won't remove it if it's in the
267 #### path of the current working directory.
268 test -z "$MH_TEST_NOCLEANUP" && (cd $MH_TEST_DIR; rm -rf "$MH_TEST_DIR"/Mail
)
270 #### Report test name if set, which indicates failure.
271 #### Relies on set -e to invoke the trap which calls
272 #### this function on failure.
274 #### 1) Set test name before running the test, use start_test().
275 #### 2) Unset upon successful completion, use finish_test().
276 if test -n "$nmh_tests_testname"; then
277 echo $nmh_tests_testname failed
280 #### Exit with non-zero status if failure. Failure is defined as either
281 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
283 if test -n "$nmh_tests_testname" -o $status -ne 0; then
284 test $status -ne 0 && exit $status || exit 1
285 test $status -ne 0 && exit 0 || exit 0
289 #### Function to set the test name, and whatever the future brings.
291 nmh_tests_testname
="$1"
294 #### Corresponding function to indicate that the test has finished. It need
295 #### not be called after each test, just the last one in a file.
297 unset nmh_tests_testname
304 MH
="${MH_TEST_DIR}/Mail/.mh_profile"
305 MHMTSCONF
="${MH_INST_DIR}${nmhetcdir}/mts.conf"
306 MH_LIBEXEC_DIR
="${MH_INST_DIR}${nmhlibexecdir}"
307 export MH MHMTSCONF MH_LIBEXEC_DIR
312 if [ -d "${MH_INST_DIR}${bindir}" ]; then
315 (cd "${MH_OBJ_DIR}" &&
316 make DESTDIR
="${MH_INST_DIR}" SETGID_MAIL
= install) ||
319 #### Don't test with sendmail because it would really send the
320 #### mail. If configured to use sendmail, change to smtp instead
321 #### so that we use fakesmtp.
322 #### And set up the maildrop in the test directory so tests don't
323 #### use the user's real maildrop.
324 #### test-slocal needs to look at the original mts.conf, so save it.
325 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
326 sed -e 's/mts: *.*/mts: smtp/' \
327 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
328 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
329 "${MHMTSCONF}.old" >"${MHMTSCONF}"
332 #### On Solaris, must set PATH after the install!
333 PATH
="${MH_INST_DIR}${bindir}:${PATH}"
336 # clean old test data on exit
340 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
341 cat > "$MH" <<EOF || exit 1
342 Path: ${MH_TEST_DIR}/Mail
343 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
344 fileproc: ${MH_INST_DIR}${bindir}/refile
345 libexecdir: ${MH_LIBEXEC_DIR}
346 mhbuild: -nocontentid
347 mhlproc: ${MH_LIBEXEC_DIR}/mhl
349 postproc: ${MH_LIBEXEC_DIR}/post
350 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
351 showproc: ${MH_LIBEXEC_DIR}/mhl
352 #: The following aren't currently used by the test suite, but are
353 #: defined here in case they are in the future:
354 mailproc: ${MH_INST_DIR}${bindir}/mhmail
355 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
356 sendproc: ${MH_INST_DIR}${bindir}/send
357 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
358 whomproc: ${MH_INST_DIR}${bindir}/whom
359 #: incproc and packproc are defined in config.c but not used by any code.
362 if test -z '@nmhetcdirinst@'; then
363 #### This isn't used with make distcheck, so that we can use it to
364 #### be sure that etc files are not used from an existing nmh
366 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
367 #### the directory and provide an argument without quotes to cp.
368 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
371 folder
-create +inbox
> /dev
/null
372 # create 10 basic messages
373 for i
in 1 2 3 4 5 6 7 8 9 10;
375 cat > $MH_TEST_DIR/Mail
/inbox
/$i <<EOF || exit 1
376 From: Test$i <test$i@example.com>
377 To: Some User <user@example.com>
378 Date: Fri, 29 Sep 2006 00:00:00
379 Message-Id: $i@test.nmh
380 Subject: Testing message $i
382 This is message number $i