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