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