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