]> diplodocus.org Git - nmh/blob - test/common.sh.in
getpass.c: Move interface to own file.
[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 converts non-breakable space U+00A0 to an ASCII space.
179 prepare_space() {
180 sed 's/'"`printf '\\302\\240'`"'/ /g'
181 }
182
183 #### check() requires two arguments, each the name of a file to be
184 #### diff'ed.
185 #### If the contents are same, the second file is removed. If different,
186 #### global variable "failed" is incremented.
187 #### Optional arguments:
188 #### 'keep first' -- first file is removed unless this is present.
189 #### 'ignore space' -- spacing differences will not be considered
190 #### significant, emulating GNU diff -w. It is assumed that the
191 #### first file has already been run through prepare_space.
192 #### ':' <test name> -- will print '<test name>' in the failure message,
193 #### to make it easier to tell which of multiple tests has failed.
194 check() {
195 first=$1; shift
196 second=$1; shift
197 keepfirst=
198 ignorespace=
199 label=test
200 while [ $# -gt 0 ]; do
201 case $1 in
202 'keep first') keepfirst=1 ;;
203 'ignore space') ignorespace=1 ;;
204 ':') shift; label=\'"$*"\'; break ;;
205 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
206 esac
207 shift
208 done
209
210 success=
211 if [ "$ignorespace" ]; then
212 #### POSIX diff should support -b.
213 prepare_space <"$second" | diff -b "$first" - >/dev/null && success=1
214 else
215 cmp -s "$first" "$second" && success=1
216 fi
217
218 if [ "$success" ]; then
219 [ "$keepfirst" ] || rm -f "$first"
220 rm -f "$second"
221 else
222 echo
223 #### POSIX diff should support -c.
224 diff -c "$first" "$second" || true
225 echo
226 echo "$0: $label failed, outputs are in $first and $second."
227 failed=`expr ${failed:-0} + 1`
228 #### Set return status of the function.
229 [ $failed -eq 0 ]
230 fi
231 }
232
233
234 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
235 #### variable (to anything) so run_* will use valgrind.
236 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
237 #### Need absolute path to valgrind.supp in case the test does a cd.
238 NMH_TEST_PREFIX="valgrind --quiet --error-exitcode=1 \
239 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
240 fi
241
242 #### Run test under another program by setting NMH_TEST_PREFIX
243 #### environment variable to, e.g., 'valgrind --quiet'.
244 run_prog() {
245 case $1 in
246 #### Don't run valgrind on shell built-in.
247 eval\ *) "$@" ;;
248 *) ${NMH_TEST_PREFIX} "$@" ;;
249 esac
250 }
251
252
253 #### run_test() requires two arguments, the first is a program and
254 #### arguments, the second is its expected one-line output string.
255 #### If the actual output does not match that string:
256 #### an error message is printed and global variable "failed" is incremented;
257 #### if there is an optional third argument, it is used in the error message.
258 run_test() {
259 set +e
260 case $1 in
261 #### Don't run valgrind on shell built-in.
262 eval\ *) actual_output=`$1 2>&1` ;;
263 *) actual_output=`${NMH_TEST_PREFIX} $1 2>&1` ;;
264 esac
265 set -e
266 if test x"$actual_output" != x"$2"; then
267 echo "$0: ${3:-\"$1\"} expected:" 1>&2
268 echo " '$2'" 1>&2
269 echo "but instead got:" 1>&2
270 echo " '$actual_output'" 1>&2
271 failed=`expr ${failed:-0} + 1`
272 fi
273 }
274
275 #### check_exit() runs the command in $2..., capturing stdout and
276 #### stderr, and then tests its exit status with the test(1) condition
277 #### in $1. If that fails, the captured stdout and stderr are
278 #### displayed, and $failed incremented.
279 check_exit() {
280 c="$1"; shift
281 t="$MH_TEST_DIR/.check_exit"
282 r=0
283 $NMH_TEST_PREFIX "$@" >"$t.1" 2>"$t.2" || r=$?
284 if test $r $c; then
285 rm "$t.1" "$t.2"
286 else
287 echo "$0: check_exit: $r $c failed: $*" >&2
288 echo " stdout:" >&2
289 sed 's/^/ /' "$t.1" >&2
290 echo " stderr:" >&2
291 sed 's/^/ /' "$t.2" >&2
292 failed=`expr ${failed:-0} + 1`
293 fi
294 }
295
296 #### Function invoked by trap on exit.
297 cleanup() {
298 #### Save exit status to use as status for this program.
299 status=$?
300
301 #### Clean up test mail space.
302 #### cd to $MH_TEST_DIR before trying to remove its Mail
303 #### subdirectory. rm on Solaris won't remove it if it's in the
304 #### path of the current working directory.
305 test -z "$MH_TEST_NOCLEANUP" && (cd "$MH_TEST_DIR" && rm -rf "$MH_TEST_DIR"/Mail)
306
307 #### Report test name if set, which indicates failure.
308 #### Relies on set -e to invoke the trap which calls
309 #### this function on failure.
310 #### To use:
311 #### 1) Set test name before running the test, use start_test().
312 #### 2) Unset upon successful completion, use finish_test().
313 if test -n "$nmh_tests_testname"; then
314 echo "first named test failure: $nmh_tests_testname"
315 fi
316
317 #### Exit with non-zero status if failure. Failure is defined as either
318 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
319 #### function.
320 if test -n "$nmh_tests_testname" -o $status -ne 0; then
321 test $status -ne 0 && exit $status || exit 1
322 test $status -ne 0 && exit 0 || exit 0
323 fi
324 }
325
326 #### Function to set the test name, and whatever the future brings.
327 start_test() {
328 #### run_test disables exit on non-zero status, but does increment
329 #### failed. Don't overwrite nmh_tests_testname if there was a
330 #### failure; remember the first test that failed.
331 [ ${failed:-0} -eq 0 ] && nmh_tests_testname="$1"
332 }
333
334 #### Corresponding function to indicate that the test has finished. It need
335 #### not be called after each test, just the last one in a file.
336 finish_test() {
337 #### run_test disables exit on non-zero status, but does increment
338 #### failed. Don't unset nmh_tests_testname if there was a failure.
339 [ ${failed:-0} -eq 0 ] && unset nmh_tests_testname
340 }
341
342 setup_test ()
343 {
344 set -e
345
346 MH="${MH_TEST_DIR}/Mail/.mh_profile"
347 MHMTSCONF="${MH_INST_DIR}${nmhetcdir}/mts.conf"
348 MH_LIBEXEC_DIR="${MH_INST_DIR}${nmhlibexecdir}"
349 export MH MHMTSCONF MH_LIBEXEC_DIR
350
351 #
352 # Only install once
353 #
354 if [ -d "${MH_INST_DIR}${bindir}" ]; then
355 :
356 else
357 (cd "${MH_OBJ_DIR}" &&
358 make DESTDIR="${MH_INST_DIR}" SETGID_MAIL= install) ||
359 exit 1
360
361 #### Don't test with sendmail because it would really send the
362 #### mail. If configured to use sendmail, change to smtp instead
363 #### so that we use fakesmtp.
364 #### And set up the maildrop in the test directory so tests don't
365 #### use the user's real maildrop.
366 #### test-slocal needs to look at the original mts.conf, so save it.
367 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
368 sed -e 's/mts: *.*/mts: smtp/' \
369 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
370 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
371 "${MHMTSCONF}.old" >"${MHMTSCONF}"
372 fi
373
374 #### On Solaris, must set PATH after the install!
375 PATH="${MH_INST_DIR}${bindir}:${PATH}"
376 export PATH
377
378 # clean old test data on exit
379 trap cleanup 0
380
381 # setup test data
382 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
383 cat > "$MH" <<EOF || exit 1
384 Path: ${MH_TEST_DIR}/Mail
385 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
386 fileproc: ${MH_INST_DIR}${bindir}/refile
387 libexecdir: ${MH_LIBEXEC_DIR}
388 mhbuild: -nocontentid
389 mhlproc: ${MH_LIBEXEC_DIR}/mhl
390 moreproc: cat
391 postproc: ${MH_LIBEXEC_DIR}/post
392 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
393 showproc: ${MH_LIBEXEC_DIR}/mhl
394 #: The following aren't currently used by the test suite, but are
395 #: defined here in case they are in the future:
396 mailproc: ${MH_INST_DIR}${bindir}/mhmail
397 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
398 sendproc: ${MH_INST_DIR}${bindir}/send
399 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
400 whomproc: ${MH_INST_DIR}${bindir}/whom
401 #: incproc and packproc are defined in config.c but not used by any code.
402 EOF
403
404 if test -z '@nmhetcdirinst@'; then
405 #### This isn't used with make distcheck, so that we can use it to
406 #### be sure that etc files are not used from an existing nmh
407 #### installation.
408 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
409 #### the directory and provide an argument without quotes to cp.
410 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
411 fi
412
413 folder -create +inbox > /dev/null
414 #### Suppress warnings about a new version.
415 #### mhparam version includes the nmh- prefix, so be consistent with that.
416 printf 'Version: nmh-%s\n' "${MH_VERSION}" >> "${MH_TEST_DIR}"/Mail/context
417
418 # create 10 basic messages
419 for i in 1 2 3 4 5 6 7 8 9 10;
420 do
421 cat > $MH_TEST_DIR/Mail/inbox/$i <<EOF || exit 1
422 From: Test$i <test$i@example.com>
423 To: Some User <user@example.com>
424 Date: Fri, 29 Sep 2006 00:00:00
425 Message-Id: $i@test.nmh
426 Subject: Testing message $i
427
428 This is message number $i
429 EOF
430 done
431 }