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