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