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"
9 test -z "$MH_INST_DIR" && MH_INST_DIR
="${MH_TEST_DIR}/inst"
10 test -z "$MH_VERSION" && MH_VERSION
="@VERSION@"
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@"
25 test -z "$OAUTH_SUPPORT" && OAUTH_SUPPORT
="@OAUTH_SUPPORT@"
26 test -z "$CURL_USER_AGENT" && CURL_USER_AGENT
="@CURL_USER_AGENT@"
28 unset MAILDROP MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE
29 unset MHLDEBUG MHPDEBUG MHWDEBUG PAGER
30 #### Set LC_ALL in individual tests as needed. Unset these so
31 #### that we don't depend on user's settings in other tests.
32 unset LANG LC_ALL LC_CTYPE
34 #### Use a test dir for tmp files when MHTMPDIR applies.
35 MHTMPDIR
=$MH_TEST_DIR/Mail
38 #### If you're reading this .... you can set MH_TEST_NOCLEANUP to prevent
39 #### the test suite from cleaning up the results of a test run, if you need
40 #### to do manual debugging on a test.
44 #### Output just the checksum. If the filename needs to appear on
45 #### the same line, the caller needs to add it. This avoids
46 #### differences due to a leading '*' binary file indicator, for
47 #### text files, on some platforms (Cygwin).
48 @MD5SUM@ $
* | @MD5FMT@
| awk '{print $1}'
51 #### Use built-in $((...)) in test suite if shell supports it.
52 #### Borrowed from configure's as_fn_arith. The result is placed
53 #### in global arith_val.
54 #### Detected at run-time instead of by configure to allow testing
55 #### with different shells.
56 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev
/null
); then
57 eval 'arith_eval () { arith_val=$(( $* )); }'
59 arith_eval
() { arith_val
=`expr "$@" || test $? -eq 1`; }
65 echo "$Test $0 SKIP ($WHY)"
69 # portable implementation of 'which' utility
73 #### Don't need to save current IFS because this function is run in
80 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
81 printf '%s\n' "$D/$PROG"
89 if [ -z "`findprog $1`" ]; then
90 test_skip
"missing $1"
94 # Skip test if none of the offered locales are supported.
97 for locale
in "$@"; do
98 if locale
-a | grep -i "$locale" >/dev
/null
; then
103 test_skip
"no suitable locale available"
106 # Some stuff for doing silly progress indicators
113 arith_eval
$LAST - $FIRST; RANGE
=$arith_val
114 arith_eval
$THIS - $FIRST; PROG
=$arith_val
115 # this automatically rounds to nearest integer
116 arith_eval
100 \
* $PROG / $RANGE; PERC
=$arith_val
117 # note \r so next update will overwrite
118 printf '%3d%%\r' $PERC
126 # don't emit anything if stdout is not connected to a tty.
137 check_for_hard_links
() {
140 printf '' > "${MH_TEST_DIR}/$$-1"
141 if link
"${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev
/null
; then
142 hard_links_supported
=1
144 hard_links_supported
=0
146 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
151 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
152 #### but sufficient for our purpose.
153 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
158 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
160 sed 's/'"`printf '\\302\\240'`"'/ /g'
163 #### check() requires two arguments, each the name of a file to be
165 #### If the contents are same, the second file is removed. If different,
166 #### global variable "failed" is incremented.
167 #### Optional arguments:
168 #### 'keep first' -- first file is removed unless this is present.
169 #### 'ignore space' -- spacing differences will not be considered
170 #### signficant, emulating GNU diff -w. It is assumed that the
171 #### first file has already been run through prepare_space.
172 #### ':' <test name> -- will print '<test name>' in the failure message,
173 #### to make it easier to tell which of multiple tests has failed.
180 while [ $# -gt 0 ]; do
182 'keep first') keepfirst
=1 ;;
183 'ignore space') ignorespace
=1 ;;
184 ':') shift; label
=\'"$*"\'; break ;;
185 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
191 if [ "$ignorespace" ]; then
192 #### POSIX diff should support -b.
193 prepare_space
<"$second" | diff -b "$first" - >/dev
/null
&& success
=1
195 cmp -s "$first" "$second" && success
=1
198 if [ "$success" ]; then
199 [ "$keepfirst" ] || rm -f "$first"
203 #### POSIX diff should support -c.
204 diff -c "$first" "$second" || true
206 echo "$0: $label failed, outputs are in $first and $second."
207 failed
=`expr ${failed:-0} + 1`
208 #### Set return status of the function.
214 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
215 #### variable (to anything) so run_* will use valgrind.
216 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
217 #### Need absolute path to valgrind.supp in case the test does a cd.
218 NMH_TEST_PREFIX
="valgrind --quiet --error-exitcode=1 \
219 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
222 #### Run test under another program by setting NMH_TEST_PREFIX
223 #### environment variable to, e.g., 'valgrind --quiet'.
226 #### Don't run valgrind on shell built-in.
228 *) ${NMH_TEST_PREFIX} "$@" ;;
233 #### run_test() requires two arguments, the first is a program and
234 #### arguments, the second is its expected one-line output string.
235 #### If the actual output does not match that string:
236 #### an error message is printed and global variable "failed" is incremented;
237 #### if there is an optional third argument, it is used in the error message.
241 #### Don't run valgrind on shell built-in.
242 eval\
*) actual_output
=`$1 2>&1` ;;
243 *) actual_output
=`${NMH_TEST_PREFIX} $1 2>&1` ;;
246 if test x
"$actual_output" != x
"$2"; then
247 echo "$0: ${3:-\"$1\"} expected:" 1>&2
249 echo "but instead got:" 1>&2
250 echo " '$actual_output'" 1>&2
251 failed
=`expr ${failed:-0} + 1`
255 #### Function invoked by trap on exit.
257 #### Save exit status to use as status for this program.
260 #### Clean up test mail space.
261 #### cd to $MH_TEST_DIR before trying to remove its Mail
262 #### subdirectory. rm on Solaris won't remove it if it's in the
263 #### path of the current working directory.
264 test -z "$MH_TEST_NOCLEANUP" && (cd $MH_TEST_DIR; rm -rf "$MH_TEST_DIR"/Mail
)
266 #### Report test name if set, which indicates failure.
267 #### Relies on set -e to invoke the trap which calls
268 #### this function on failure.
270 #### 1) Set test name before running the test, use start_test().
271 #### 2) Unset upon successful completion, use finish_test().
272 if test -n "$nmh_tests_testname"; then
273 echo $nmh_tests_testname failed
276 #### Exit with non-zero status if failure. Failure is defined as either
277 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
279 if test -n "$nmh_tests_testname" -o $status -ne 0; then
280 test $status -ne 0 && exit $status || exit 1
281 test $status -ne 0 && exit 0 || exit 0
285 #### Function to set the test name, and whatever the future brings.
287 nmh_tests_testname
="$1"
290 #### Corresponding function to indicate that the test has finished. It need
291 #### not be called after each test, just the last one in a file.
293 unset nmh_tests_testname
300 MH
="${MH_TEST_DIR}/Mail/.mh_profile"
301 MHMTSCONF
="${MH_INST_DIR}${nmhetcdir}/mts.conf"
302 MH_LIBEXEC_DIR
="${MH_INST_DIR}${nmhlibexecdir}"
303 export MH MHMTSCONF MH_LIBEXEC_DIR
308 if [ -d "${MH_INST_DIR}${bindir}" ]; then
311 (cd "${MH_OBJ_DIR}" &&
312 make DESTDIR
="${MH_INST_DIR}" SETGID_MAIL
= install) ||
315 #### Don't test with sendmail because it would really send the
316 #### mail. If configured to use sendmail, change to smtp instead
317 #### so that we use fakesmtp.
318 #### And set up the maildrop in the test directory so tests don't
319 #### use the user's real maildrop.
320 #### test-slocal needs to look at the original mts.conf, so save it.
321 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
322 sed -e 's/mts: *.*/mts: smtp/' \
323 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
324 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
325 "${MHMTSCONF}.old" >"${MHMTSCONF}"
328 #### On Solaris, must set PATH after the install!
329 PATH
="${MH_INST_DIR}${bindir}:${PATH}"
332 # clean old test data on exit
335 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
336 cat > "$MH" <<EOF || exit 1
337 Path: ${MH_TEST_DIR}/Mail
338 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
339 fileproc: ${MH_INST_DIR}${bindir}/refile
340 libexecdir: ${MH_LIBEXEC_DIR}
341 mhbuild: -nocontentid
342 mhlproc: ${MH_LIBEXEC_DIR}/mhl
344 postproc: ${MH_LIBEXEC_DIR}/post
345 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
346 showproc: ${MH_LIBEXEC_DIR}/mhl
347 #: The following aren't currently used by the test suite, but are
348 #: defined here in case they are in the future:
349 mailproc: ${MH_INST_DIR}${bindir}/mhmail
350 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
351 sendproc: ${MH_INST_DIR}${bindir}/send
352 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
353 whomproc: ${MH_INST_DIR}${bindir}/whom
354 #: incproc and packproc are defined in config.c but not used by any code.
357 if test -z '@nmhetcdirinst@'; then
358 #### This isn't used with make distcheck, so that we can use it to
359 #### be sure that etc files are not used from an existing nmh
361 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
362 #### the directory and provide an argument without quotes to cp.
363 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
366 folder
-create +inbox
> /dev
/null
367 # create 10 basic messages
368 for i
in 1 2 3 4 5 6 7 8 9 10;
370 cat > $MH_TEST_DIR/Mail
/inbox
/$i <<EOF || exit 1
371 From: Test$i <test$i@example.com>
372 To: Some User <user@example.com>
373 Date: Fri, 29 Sep 2006 00:00:00
374 Message-Id: $i@test.nmh
375 Subject: Testing message $i
377 This is message number $i