]> diplodocus.org Git - nmh/blob - test/common.sh.in
8c054cd602d24f334508a78cbbdcf8063e3a670e
[nmh] / test / common.sh.in
1 # Common helper routines for test shell scripts -- to be sourced by them
2 # @configure_input@
3
4
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`
12 export MH_TEST_DIR
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@"
31
32 #### Make sure that HOME is set to avoid run-time warning from w3m about
33 #### not being able to create config directory.
34 HOME="$MH_TEST_DIR"
35 export HOME
36
37 unset MAILDROP MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE
38 unset MHLDEBUG MHWDEBUG PAGER XOAUTH SMTPUTF8
39 #### Set LC_ALL in individual tests as needed. Unset these so
40 #### that we don't depend on user's settings in other tests.
41 unset LANG LC_ALL LC_CTYPE
42
43 #### Use a test dir for tmp files when MHTMPDIR applies.
44 MHTMPDIR="$MH_TEST_DIR"/Mail
45 export MHTMPDIR
46
47 #### If you're reading this .... you can set MH_TEST_NOCLEANUP to prevent
48 #### the test suite from cleaning up the results of a test run, if you need
49 #### to do manual debugging on a test.
50
51 output_md5()
52 {
53 #### Output just the checksum. If the filename needs to appear on
54 #### the same line, the caller needs to add it. This avoids
55 #### differences due to a leading '*' binary file indicator, for
56 #### text files, on some platforms (Cygwin).
57 @MD5SUM@ $* | @MD5FMT@ | awk '{print $1}'
58 }
59
60 #### Use built-in $((...)) in test suite if shell supports it.
61 #### Borrowed from configure's as_fn_arith. The result is placed
62 #### in global arith_val.
63 #### Detected at run-time instead of by configure to allow testing
64 #### with different shells.
65 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev/null); then
66 eval 'arith_eval () { arith_val=$(( $* )); }'
67 else
68 arith_eval () { arith_val=`expr "$@" || test $? -eq 1`; }
69 fi
70
71 test_skip ()
72 {
73 why="$1"
74 echo "$0: skipped: $why"
75 exit 77
76 }
77
78 # portable implementation of 'which' utility
79 findprog()
80 {
81 PROG="$1"
82 #### Don't need to save current IFS because this function is run in
83 #### a subshell.
84 IFS=:
85 for D in $PATH; do
86 if [ -z "$D" ]; then
87 D=.
88 fi
89 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
90 printf '%s\n' "$D/$PROG"
91 break
92 fi
93 done
94 }
95
96 require_prog ()
97 {
98 if [ -z "`findprog $1`" ]; then
99 test_skip "missing $1"
100 fi
101 }
102
103 # Skip test if none of the offered locales are supported.
104 # As side effect, use the first locale that is found. Note that
105 # some platforms allow, by way of example, en_US.UTF-8 to be used
106 # even though en_US.UTF8 is listed by locale -a. But by setting
107 # LC_ALL here, we don't rely on that.
108 require_locale ()
109 {
110 for locale in "$@"; do
111 if locale -a | grep -i "$locale" >/dev/null; then
112 LC_ALL="$locale"; export LC_ALL
113 return
114 fi
115 done
116
117 test_skip "no suitable locale available"
118 }
119
120 # Some stuff for doing silly progress indicators
121 if [ -t 1 ] ; then
122 progress_update ()
123 {
124 THIS="$1"
125 FIRST="$2"
126 LAST="$3"
127 arith_eval $LAST - $FIRST; RANGE=$arith_val
128 arith_eval $THIS - $FIRST; PROG=$arith_val
129 # this automatically rounds to nearest integer
130 arith_eval 100 \* $PROG / $RANGE; PERC=$arith_val
131 # note \r so next update will overwrite
132 printf '%3d%%\r' $PERC
133 }
134
135 progress_done ()
136 {
137 printf '100%%\n'
138 }
139 else
140 # don't emit anything if stdout is not connected to a tty.
141 progress_update ()
142 {
143 :
144 }
145 progress_done ()
146 {
147 :
148 }
149 fi
150
151 check_for_hard_links () {
152 set +e
153
154 printf '' > "${MH_TEST_DIR}/$$-1"
155 xdir_links_supported=0
156 if link "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev/null; then
157 hard_links_supported=1
158 mkdir "${MH_TEST_DIR}/xlinkdir"
159 if link "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/xlinkdir/$$-2" 2>/dev/null; then
160 xdir_links_supported=1
161 fi
162 else
163 hard_links_supported=0
164 fi
165 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
166 rm -rf "${MH_TEST_DIR}/xlinkdir"
167
168 set -e
169 }
170
171 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
172 #### but sufficient for our purpose.
173 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
174 squeeze_lines() {
175 sed '/^$/N;/\n$/D'
176 }
177
178 #### Filter that removes blank lines and leading space that lynx inserts.
179 #### but sufficient for our purpose.
180 squeeze_whitespace() {
181 sed '/^$/D; s/^ *//;'
182 }
183
184 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
185 prepare_space() {
186 sed 's/'"`printf '\\302\\240'`"'/ /g'
187 }
188
189 #### check() requires two arguments, each the name of a file to be
190 #### diff'ed.
191 #### If the contents are same, the second file is removed. If different,
192 #### global variable "failed" is incremented.
193 #### Optional arguments:
194 #### 'keep first' -- first file is removed unless this is present.
195 #### 'ignore space' -- spacing differences will not be considered
196 #### significant, emulating GNU diff -w. It is assumed that the
197 #### first file has already been run through prepare_space.
198 #### ':' <test name> -- will print '<test name>' in the failure message,
199 #### to make it easier to tell which of multiple tests has failed.
200 check() {
201 first=$1; shift
202 second=$1; shift
203 keepfirst=
204 ignorespace=
205 label=test
206 while [ $# -gt 0 ]; do
207 case $1 in
208 'keep first') keepfirst=1 ;;
209 'ignore space') ignorespace=1 ;;
210 ':') shift; label=\'"$*"\'; break ;;
211 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
212 esac
213 shift
214 done
215
216 success=
217 if [ "$ignorespace" ]; then
218 #### POSIX diff should support -b.
219 prepare_space <"$second" | diff -b "$first" - >/dev/null && success=1
220 else
221 cmp -s "$first" "$second" && success=1
222 fi
223
224 if [ "$success" ]; then
225 [ "$keepfirst" ] || rm -f "$first"
226 rm -f "$second"
227 else
228 echo
229 #### POSIX diff should support -c.
230 diff -c "$first" "$second" || true
231 echo
232 echo "$0: $label failed, outputs are in $first and $second."
233 failed=`expr ${failed:-0} + 1`
234 #### Set return status of the function.
235 [ $failed -eq 0 ]
236 fi
237 }
238
239
240 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
241 #### variable (to anything) so run_* will use valgrind.
242 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
243 #### Need absolute path to valgrind.supp in case the test does a cd.
244 NMH_TEST_PREFIX="valgrind --quiet --error-exitcode=1 \
245 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
246 fi
247
248 #### Run test under another program by setting NMH_TEST_PREFIX
249 #### environment variable to, e.g., 'valgrind --quiet'.
250 run_prog() {
251 case $1 in
252 #### Don't run valgrind on shell built-in.
253 eval\ *) "$@" ;;
254 *) ${NMH_TEST_PREFIX} "$@" ;;
255 esac
256 }
257
258
259 #### run_test() requires two arguments, the first is a program and
260 #### arguments, the second is its expected one-line output string.
261 #### If the actual output does not match that string:
262 #### an error message is printed and global variable "failed" is incremented;
263 #### if there is an optional third argument, it is used in the error message.
264 run_test() {
265 set +e
266 case $1 in
267 #### Don't run valgrind on shell built-in.
268 eval\ *) actual_output=`$1 2>&1` ;;
269 *) actual_output=`${NMH_TEST_PREFIX} $1 2>&1` ;;
270 esac
271 set -e
272 if test x"$actual_output" != x"$2"; then
273 echo "$0: ${3:-\"$1\"} expected:" 1>&2
274 echo " '$2'" 1>&2
275 echo "but instead got:" 1>&2
276 echo " '$actual_output'" 1>&2
277 failed=`expr ${failed:-0} + 1`
278 fi
279 }
280
281 #### check_exit() runs the command in $2..., capturing stdout and
282 #### stderr, and then tests its exit status with the test(1) condition
283 #### in $1. If that fails, the captured stdout and stderr are
284 #### displayed, and $failed incremented.
285 check_exit() {
286 c="$1"; shift
287 t="$MH_TEST_DIR/.check_exit"
288 r=0
289 $NMH_TEST_PREFIX "$@" >"$t.1" 2>"$t.2" || r=$?
290 if test $r $c; then
291 rm "$t.1" "$t.2"
292 else
293 echo "$0: check_exit: $r $c failed: $*" >&2
294 echo " stdout:" >&2
295 sed 's/^/ /' "$t.1" >&2
296 echo " stderr:" >&2
297 sed 's/^/ /' "$t.2" >&2
298 failed=`expr ${failed:-0} + 1`
299 fi
300 }
301
302 #### Function invoked by trap on exit.
303 cleanup() {
304 #### Save exit status to use as status for this program.
305 status=$?
306
307 #### Clean up test mail space.
308 #### cd to $MH_TEST_DIR before trying to remove its Mail
309 #### subdirectory. rm on Solaris won't remove it if it's in the
310 #### path of the current working directory.
311 test -z "$MH_TEST_NOCLEANUP" && (cd "$MH_TEST_DIR" && rm -rf "$MH_TEST_DIR"/Mail)
312
313 #### Report test name if set, which indicates failure.
314 #### Relies on set -e to invoke the trap which calls
315 #### this function on failure.
316 #### To use:
317 #### 1) Set test name before running the test, use start_test().
318 #### 2) Unset upon successful completion, use finish_test().
319 if test -n "$nmh_tests_testname"; then
320 echo "first named test failure: $nmh_tests_testname"
321 fi
322
323 #### Exit with non-zero status if failure. Failure is defined as either
324 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
325 #### function.
326 if test -n "$nmh_tests_testname" -o $status -ne 0; then
327 test $status -ne 0 && exit $status || exit 1
328 test $status -ne 0 && exit 0 || exit 0
329 fi
330 }
331
332 #### Function to set the test name, and whatever the future brings.
333 start_test() {
334 #### run_test disables exit on non-zero status, but does increment
335 #### failed. Don't overwrite nmh_tests_testname if there was a
336 #### failure; remember the first test that failed.
337 [ ${failed:-0} -eq 0 ] && nmh_tests_testname="$1"
338 }
339
340 #### Corresponding function to indicate that the test has finished. It need
341 #### not be called after each test, just the last one in a file.
342 finish_test() {
343 #### run_test disables exit on non-zero status, but does increment
344 #### failed. Don't unset nmh_tests_testname if there was a failure.
345 [ ${failed:-0} -eq 0 ] && unset nmh_tests_testname
346 }
347
348 setup_test ()
349 {
350 set -e
351
352 MH="${MH_TEST_DIR}/Mail/.mh_profile"
353 MHMTSCONF="${MH_INST_DIR}${nmhetcdir}/mts.conf"
354 MH_LIBEXEC_DIR="${MH_INST_DIR}${nmhlibexecdir}"
355 export MH MHMTSCONF MH_LIBEXEC_DIR
356
357 #
358 # Only install once
359 #
360 if [ -d "${MH_INST_DIR}${bindir}" ]; then
361 :
362 else
363 (cd "${MH_OBJ_DIR}" &&
364 make DESTDIR="${MH_INST_DIR}" SETGID_MAIL= install) ||
365 exit 1
366
367 #### Don't test with sendmail because it would really send the
368 #### mail. If configured to use sendmail, change to smtp instead
369 #### so that we use fakesmtp.
370 #### And set up the maildrop in the test directory so tests don't
371 #### use the user's real maildrop.
372 #### test-slocal needs to look at the original mts.conf, so save it.
373 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
374 sed -e 's/mts: *.*/mts: smtp/' \
375 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
376 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
377 "${MHMTSCONF}.old" >"${MHMTSCONF}"
378 fi
379
380 #### On Solaris, must set PATH after the install!
381 PATH="${MH_INST_DIR}${bindir}:${PATH}"
382 export PATH
383
384 # clean old test data on exit
385 trap cleanup 0
386
387 # setup test data
388 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
389 cat > "$MH" <<EOF || exit 1
390 Path: ${MH_TEST_DIR}/Mail
391 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
392 fileproc: ${MH_INST_DIR}${bindir}/refile
393 libexecdir: ${MH_LIBEXEC_DIR}
394 mhbuild: -nocontentid
395 mhlproc: ${MH_LIBEXEC_DIR}/mhl
396 moreproc: cat
397 postproc: ${MH_LIBEXEC_DIR}/post
398 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
399 showproc: ${MH_LIBEXEC_DIR}/mhl
400 #: The following aren't currently used by the test suite, but are
401 #: defined here in case they are in the future:
402 mailproc: ${MH_INST_DIR}${bindir}/mhmail
403 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
404 sendproc: ${MH_INST_DIR}${bindir}/send
405 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
406 whomproc: ${MH_INST_DIR}${bindir}/whom
407 #: incproc and packproc are defined in config.c but not used by any code.
408 EOF
409
410 if test -z '@nmhetcdirinst@'; then
411 #### This isn't used with make distcheck, so that we can use it to
412 #### be sure that etc files are not used from an existing nmh
413 #### installation.
414 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
415 #### the directory and provide an argument without quotes to cp.
416 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
417 fi
418
419 folder -create +inbox > /dev/null
420 #### Suppress warnings about a new version.
421 #### mhparam version includes the nmh- prefix, so be consistent with that.
422 printf 'Version: nmh-%s\n' "${MH_VERSION}" >> "${MH_TEST_DIR}"/Mail/context
423
424 # create 10 basic messages
425 for i in 1 2 3 4 5 6 7 8 9 10;
426 do
427 cat > $MH_TEST_DIR/Mail/inbox/$i <<EOF || exit 1
428 From: Test$i <test$i@example.com>
429 To: Some User <user@example.com>
430 Date: Fri, 29 Sep 2006 00:00:00
431 Message-Id: $i@test.nmh
432 Subject: Testing message $i
433
434 This is message number $i
435 EOF
436 done
437 }