futher test suite fixes to work on Solaris
[mmh] / test / common.sh.in
1 # Common helper routines for test shell scripts -- intended to be sourced by them
2 # @configure_input@
3
4
5 #### The following exported 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 test -z "$prefix"  &&  prefix=@prefix@
10 test -z "$datarootdir"  &&  datarootdir=@datarootdir@
11 test -z "$exec_prefix"  &&  exec_prefix=@exec_prefix@
12 test -z "$auxexecdir"  &&  auxexecdir="@libdir@"
13 test -z "$bindir"  &&  bindir="@bindir@"
14 test -z "$mandir"  &&  mandir="@mandir@"
15 test -z "$sysconfdir"  &&  sysconfdir="@sysconfdir@"
16 test -z "$pagerpath"  &&  pagerpath="@pagerpath@"
17 export MH_TEST_DIR auxexecdir bindir mandir sysconfdir pagerpath
18
19 test -z "$MH_INST_DIR"  &&  MH_INST_DIR=${MH_TEST_DIR}/inst
20 export MH_INST_DIR
21
22 unset MHCONTEXT
23
24 output_md5()
25 {
26   #### Output just the checksum.  If the filename needs to appear on
27   #### the same line, the caller needs to add it.  This avoids
28   #### differences due to a leading '*' binary file indicator, for
29   #### text files, on some platforms (Cygwin).
30   @MD5SUM@ $* | @MD5FMT@ | cut -d ' ' -f 1
31 }
32
33 #### Use built-in $((...)) in test suite if shell supports it.
34 #### Borrowed from configure's as_fn_arith.  The result is placed
35 #### in global arith_val.
36 #### Detected at run-time instead of by configure to allow testing
37 #### with different shells.
38 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev/null); then
39   eval 'arith_eval () { arith_val=$(( $* )); }'
40 else
41   arith_eval () { arith_val=`expr "$@" || test $? -eq 1`; }
42 fi
43
44 test_skip ()
45 {
46   WHY="$1"
47   echo "$Test $0 SKIP ($WHY)"
48   exit 77
49 }
50
51 # portable implementation of 'which' utility
52 findprog()
53 {
54   FOUND=
55   PROG="$1"
56   IFS_SAVE="$IFS"
57   IFS=:
58   for D in $PATH; do
59     if [ -z "$D" ]; then
60       D=.
61     fi
62     if [ -f "$D/$PROG" ] && [ -x "$D/$PROG" ]; then
63       printf '%s\n' "$D/$PROG"
64       break
65     fi
66   done
67   IFS="$IFS_SAVE"
68 }
69
70 require_prog ()
71 {
72   if [ -z "`findprog $1`" ]; then
73     test_skip "missing $1"
74   fi
75 }
76
77 # Some stuff for doing silly progress indicators
78 progress_update ()
79 {
80   THIS="$1"
81   FIRST="$2"
82   LAST="$3"
83   arith_eval $LAST - $FIRST; RANGE=$arith_val
84   arith_eval $THIS - $FIRST; PROG=$arith_val
85   # this automatically rounds to nearest integer
86   arith_eval 100 \* $PROG / $RANGE; PERC=$arith_val
87   # note \r so next update will overwrite
88   printf "%3d%%\r" $PERC
89 }
90
91 progress_done ()
92 {
93   printf "100%%\n"
94 }
95
96 #### check() requires two arguments, each the name of a file to be
97 #### diff'ed.
98 #### If the same, the second file is removed.  And the first file is
99 ####   removed unless the optional third argument has a value of
100 ####   'keep first'.
101 #### If different, global variable "failed" is incremented.
102 check() {
103     #### POSIX diff should support -c.
104     if diff -c "$1" "$2"; then
105       test $# -lt 3 -o "$3" != 'keep first'  &&  rm -f "$1"
106       rm -f "$2"
107     else
108       echo "$0: test failed, outputs are in $1 and $2."
109       failed=`expr ${failed:-0} + 1`
110     fi
111 }
112
113 #### run_test() requires two arguments, the first is a program and
114 #### arguments, the second is its expected one-line output string.
115 #### If the actual output does not match that string:
116 #### an error message is printed and global variable "failed" is incremented;
117 #### if there is an optional third argument, it is used in the error message.
118 run_test() {
119   set +e
120   actual_output=`$1 2>&1`
121   set -e
122   if test x"$actual_output" != x"$2"; then
123     echo "$0: ${3:-\"$1\"} expected:" 1>&2
124     echo "    '$2'" 1>&2
125     echo "but instead got:" 1>&2
126     echo "    '$actual_output'" 1>&2
127     failed=`expr ${failed:-0} + 1`
128   fi
129 }
130
131 setup_test ()
132 {
133   MH=${MH_TEST_DIR}/Mail/.mh_profile
134   MHMTSCONF=${MH_INST_DIR}${sysconfdir}/mts.conf
135   PATH=${MH_INST_DIR}${bindir}:${PATH}
136   MH_LIB_DIR=${MH_INST_DIR}${auxexecdir}
137   export MH MHMTSCONF MH_LIB_DIR PATH
138
139   #
140   # Only install once
141   #
142   if [ -d ${MH_INST_DIR}${bindir} ]; then
143     :
144   else
145     (cd ${MH_OBJ_DIR} && make DESTDIR=${MH_INST_DIR} install) || exit 1
146   fi
147
148   # clean old test data
149   trap "rm -rf $MH_TEST_DIR/Mail" 0
150   # setup test data
151   mkdir $MH_TEST_DIR/Mail || exit 1
152   cat > $MH <<EOF || exit 1
153 Path: ${MH_TEST_DIR}/Mail
154 mhlproc: ${MH_LIB_DIR}/mhl
155 showproc: ${MH_LIB_DIR}/mhl
156 postproc: ${MH_LIB_DIR}/post
157 fileproc: ${MH_INST_DIR}${bindir}/refile
158 EOF
159
160   for f in MailAliases components digestcomps distcomps forwcomps mhl.body \
161            mhl.digest mhl.format mhl.forward mhl.headers mhl.reply \
162            mhn.defaults rcvdistcomps replcomps replgroupcomps scan.MMDDYY \
163            scan.YYYYMMDD scan.default scan.mailx scan.nomime scan.size \
164            scan.time scan.timely scan.unseen
165   do
166     cp ${MH_INST_DIR}${sysconfdir}/${f} ${MH_TEST_DIR}/Mail || exit 1
167   done
168
169   folder -create +inbox > /dev/null
170   # create 10 basic messages
171   for i in 1 2 3 4 5 6 7 8 9 10;
172   do
173     cat > $MH_TEST_DIR/Mail/inbox/$i <<EOF || exit 1
174 From: Test$i <test$i@example.com>
175 To: Some User <user@example.com>
176 Date: Fri, 29 Sep 2006 00:00:00
177 Subject: Testing message $i
178
179 This is message number $i
180 EOF
181   done
182 }