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