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