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