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