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 #### If w3m is used, HOME needs to be set, assuming default w3m config.
33 #### So make sure that HOME is set to avoid run-time warning about not
34 #### being able to create config directory.
35 test -z "$HOME" && HOME
=$MH_TEST_DIR
39 #### If w3m is used, HOME needs to be set, assuming default w3m config.
40 #### So make sure that HOME is set to avoid run-time warning about not
41 #### being able to create config directory.
42 test -z "$HOME" && HOME
=$MH_TEST_DIR
46 unset MAILDROP MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE
47 unset MHLDEBUG MHPDEBUG MHWDEBUG PAGER
48 #### Set LC_ALL in individual tests as needed. Unset these so
49 #### that we don't depend on user's settings in other tests.
50 unset LANG LC_ALL LC_CTYPE
52 #### Use a test dir for tmp files when MHTMPDIR applies.
53 MHTMPDIR
=$MH_TEST_DIR/Mail
56 #### If you're reading this .... you can set MH_TEST_NOCLEANUP to prevent
57 #### the test suite from cleaning up the results of a test run, if you need
58 #### to do manual debugging on a test.
62 #### Output just the checksum. If the filename needs to appear on
63 #### the same line, the caller needs to add it. This avoids
64 #### differences due to a leading '*' binary file indicator, for
65 #### text files, on some platforms (Cygwin).
66 @MD5SUM@ $
* | @MD5FMT@
| awk '{print $1}'
69 #### Use built-in $((...)) in test suite if shell supports it.
70 #### Borrowed from configure's as_fn_arith. The result is placed
71 #### in global arith_val.
72 #### Detected at run-time instead of by configure to allow testing
73 #### with different shells.
74 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev
/null
); then
75 eval 'arith_eval () { arith_val=$(( $* )); }'
77 arith_eval
() { arith_val
=`expr "$@" || test $? -eq 1`; }
83 echo "$Test $0 SKIP ($WHY)"
87 # portable implementation of 'which' utility
91 #### Don't need to save current IFS because this function is run in
98 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
99 printf '%s\n' "$D/$PROG"
107 if [ -z "`findprog $1`" ]; then
108 test_skip
"missing $1"
112 # Skip test if none of the offered locales are supported.
115 for locale
in "$@"; do
116 if locale
-a | grep -i "$locale" >/dev
/null
; then
121 test_skip
"no suitable locale available"
124 # Some stuff for doing silly progress indicators
131 arith_eval
$LAST - $FIRST; RANGE
=$arith_val
132 arith_eval
$THIS - $FIRST; PROG
=$arith_val
133 # this automatically rounds to nearest integer
134 arith_eval
100 \
* $PROG / $RANGE; PERC
=$arith_val
135 # note \r so next update will overwrite
136 printf '%3d%%\r' $PERC
144 # don't emit anything if stdout is not connected to a tty.
155 check_for_hard_links
() {
158 printf '' > "${MH_TEST_DIR}/$$-1"
159 xdir_links_supported
=0
160 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev
/null
; then
161 hard_links_supported
=1
162 mkdir "${MH_TEST_DIR}/xlinkdir"
163 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/xlinkdir/$$-2" 2>/dev
/null
; then
164 xdir_links_supported
=1
167 hard_links_supported
=0
169 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
170 rm -rf "${MH_TEST_DIR}/xlinkdir"
175 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
176 #### but sufficient for our purpose.
177 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
182 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
184 sed 's/'"`printf '\\302\\240'`"'/ /g'
187 #### check() requires two arguments, each the name of a file to be
189 #### If the contents are same, the second file is removed. If different,
190 #### global variable "failed" is incremented.
191 #### Optional arguments:
192 #### 'keep first' -- first file is removed unless this is present.
193 #### 'ignore space' -- spacing differences will not be considered
194 #### significant, emulating GNU diff -w. It is assumed that the
195 #### first file has already been run through prepare_space.
196 #### ':' <test name> -- will print '<test name>' in the failure message,
197 #### to make it easier to tell which of multiple tests has failed.
204 while [ $# -gt 0 ]; do
206 'keep first') keepfirst
=1 ;;
207 'ignore space') ignorespace
=1 ;;
208 ':') shift; label
=\'"$*"\'; break ;;
209 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
215 if [ "$ignorespace" ]; then
216 #### POSIX diff should support -b.
217 prepare_space
<"$second" | diff -b "$first" - >/dev
/null
&& success
=1
219 cmp -s "$first" "$second" && success
=1
222 if [ "$success" ]; then
223 [ "$keepfirst" ] || rm -f "$first"
227 #### POSIX diff should support -c.
228 diff -c "$first" "$second" || true
230 echo "$0: $label failed, outputs are in $first and $second."
231 failed
=`expr ${failed:-0} + 1`
232 #### Set return status of the function.
238 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
239 #### variable (to anything) so run_* will use valgrind.
240 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
241 #### Need absolute path to valgrind.supp in case the test does a cd.
242 NMH_TEST_PREFIX
="valgrind --quiet --error-exitcode=1 \
243 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
246 #### Run test under another program by setting NMH_TEST_PREFIX
247 #### environment variable to, e.g., 'valgrind --quiet'.
250 #### Don't run valgrind on shell built-in.
252 *) ${NMH_TEST_PREFIX} "$@" ;;
257 #### run_test() requires two arguments, the first is a program and
258 #### arguments, the second is its expected one-line output string.
259 #### If the actual output does not match that string:
260 #### an error message is printed and global variable "failed" is incremented;
261 #### if there is an optional third argument, it is used in the error message.
265 #### Don't run valgrind on shell built-in.
266 eval\
*) actual_output
=`$1 2>&1` ;;
267 *) actual_output
=`${NMH_TEST_PREFIX} $1 2>&1` ;;
270 if test x
"$actual_output" != x
"$2"; then
271 echo "$0: ${3:-\"$1\"} expected:" 1>&2
273 echo "but instead got:" 1>&2
274 echo " '$actual_output'" 1>&2
275 failed
=`expr ${failed:-0} + 1`
279 #### Function invoked by trap on exit.
281 #### Save exit status to use as status for this program.
284 #### Clean up test mail space.
285 #### cd to $MH_TEST_DIR before trying to remove its Mail
286 #### subdirectory. rm on Solaris won't remove it if it's in the
287 #### path of the current working directory.
288 test -z "$MH_TEST_NOCLEANUP" && (cd $MH_TEST_DIR; rm -rf "$MH_TEST_DIR"/Mail
)
290 #### Report test name if set, which indicates failure.
291 #### Relies on set -e to invoke the trap which calls
292 #### this function on failure.
294 #### 1) Set test name before running the test, use start_test().
295 #### 2) Unset upon successful completion, use finish_test().
296 if test -n "$nmh_tests_testname"; then
297 echo $nmh_tests_testname failed
300 #### Exit with non-zero status if failure. Failure is defined as either
301 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
303 if test -n "$nmh_tests_testname" -o $status -ne 0; then
304 test $status -ne 0 && exit $status || exit 1
305 test $status -ne 0 && exit 0 || exit 0
309 #### Function to set the test name, and whatever the future brings.
311 nmh_tests_testname
="$1"
314 #### Corresponding function to indicate that the test has finished. It need
315 #### not be called after each test, just the last one in a file.
317 unset nmh_tests_testname
324 MH
="${MH_TEST_DIR}/Mail/.mh_profile"
325 MHMTSCONF
="${MH_INST_DIR}${nmhetcdir}/mts.conf"
326 MH_LIBEXEC_DIR
="${MH_INST_DIR}${nmhlibexecdir}"
327 export MH MHMTSCONF MH_LIBEXEC_DIR
332 if [ -d "${MH_INST_DIR}${bindir}" ]; then
335 (cd "${MH_OBJ_DIR}" &&
336 make DESTDIR
="${MH_INST_DIR}" SETGID_MAIL
= install) ||
339 #### Don't test with sendmail because it would really send the
340 #### mail. If configured to use sendmail, change to smtp instead
341 #### so that we use fakesmtp.
342 #### And set up the maildrop in the test directory so tests don't
343 #### use the user's real maildrop.
344 #### test-slocal needs to look at the original mts.conf, so save it.
345 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
346 sed -e 's/mts: *.*/mts: smtp/' \
347 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
348 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
349 "${MHMTSCONF}.old" >"${MHMTSCONF}"
352 #### On Solaris, must set PATH after the install!
353 PATH
="${MH_INST_DIR}${bindir}:${PATH}"
356 # clean old test data on exit
360 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
361 cat > "$MH" <<EOF || exit 1
362 Path: ${MH_TEST_DIR}/Mail
363 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
364 fileproc: ${MH_INST_DIR}${bindir}/refile
365 libexecdir: ${MH_LIBEXEC_DIR}
366 mhbuild: -nocontentid
367 mhlproc: ${MH_LIBEXEC_DIR}/mhl
369 postproc: ${MH_LIBEXEC_DIR}/post
370 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
371 showproc: ${MH_LIBEXEC_DIR}/mhl
372 #: The following aren't currently used by the test suite, but are
373 #: defined here in case they are in the future:
374 mailproc: ${MH_INST_DIR}${bindir}/mhmail
375 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
376 sendproc: ${MH_INST_DIR}${bindir}/send
377 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
378 whomproc: ${MH_INST_DIR}${bindir}/whom
379 #: incproc and packproc are defined in config.c but not used by any code.
382 if test -z '@nmhetcdirinst@'; then
383 #### This isn't used with make distcheck, so that we can use it to
384 #### be sure that etc files are not used from an existing nmh
386 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
387 #### the directory and provide an argument without quotes to cp.
388 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
391 folder
-create +inbox
> /dev
/null
392 # create 10 basic messages
393 for i
in 1 2 3 4 5 6 7 8 9 10;
395 cat > $MH_TEST_DIR/Mail
/inbox
/$i <<EOF || exit 1
396 From: Test$i <test$i@example.com>
397 To: Some User <user@example.com>
398 Date: Fri, 29 Sep 2006 00:00:00
399 Message-Id: $i@test.nmh
400 Subject: Testing message $i
402 This is message number $i