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