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