]> diplodocus.org Git - nmh/blob - test/common.sh.in
In expand_pseudoheader(), set Content-Type to 7-bit for ASCII text.
[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 "$nmhexecdir" && nmhexecdir="@libexecdir@/nmh"
13 test -z "$bindir" && bindir="@bindir@"
14 test -z "$mandir" && mandir="@mandir@"
15 test -z "$nmhetcdir" && nmhetcdir="@sysconfdir@/nmh"
16 test -z "$supported_locks" && supported_locks="@supported_locks@"
17 test -z "$default_locking" && default_locking="@default_locking@"
18 test -z "$MULTIBYTE_ENABLED" && MULTIBYTE_ENABLED="@MULTIBYTE_ENABLED@"
19 test -z "$ICONV_ENABLED" && ICONV_ENABLED="@ICONV_ENABLED@"
20 export MH_TEST_DIR nmhexecdir bindir mandir nmhetcdir
21 export MULTIBYTE_ENABLED ICONV_ENABLED
22
23 test -z "$MH_INST_DIR" && MH_INST_DIR="${MH_TEST_DIR}/inst"
24 export MH_INST_DIR
25
26 unset MAILDROP MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE
27 unset MHLDEBUG MHPDEBUG MHWDEBUG PAGER
28 #### Set LC_ALL in individual tests as needed. Unset these so
29 #### that we don't depend on user's settings in other tests.
30 unset LANG LC_ALL LC_CTYPE
31
32 #### Use a test dir for tmp files when MHTMPDIR applies.
33 MHTMPDIR=$MH_TEST_DIR/Mail
34 export MHTMPDIR
35
36 output_md5()
37 {
38 #### Output just the checksum. If the filename needs to appear on
39 #### the same line, the caller needs to add it. This avoids
40 #### differences due to a leading '*' binary file indicator, for
41 #### text files, on some platforms (Cygwin).
42 @MD5SUM@ $* | @MD5FMT@ | awk '{print $1}'
43 }
44
45 #### Use built-in $((...)) in test suite if shell supports it.
46 #### Borrowed from configure's as_fn_arith. The result is placed
47 #### in global arith_val.
48 #### Detected at run-time instead of by configure to allow testing
49 #### with different shells.
50 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev/null); then
51 eval 'arith_eval () { arith_val=$(( $* )); }'
52 else
53 arith_eval () { arith_val=`expr "$@" || test $? -eq 1`; }
54 fi
55
56 test_skip ()
57 {
58 WHY="$1"
59 echo "$Test $0 SKIP ($WHY)"
60 exit 77
61 }
62
63 # portable implementation of 'which' utility
64 findprog()
65 {
66 PROG="$1"
67 #### Don't need to save current IFS because this function is run in
68 #### a subshell.
69 IFS=:
70 for D in $PATH; do
71 if [ -z "$D" ]; then
72 D=.
73 fi
74 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
75 printf '%s\n' "$D/$PROG"
76 break
77 fi
78 done
79 }
80
81 require_prog ()
82 {
83 if [ -z "`findprog $1`" ]; then
84 test_skip "missing $1"
85 fi
86 }
87
88 # Skip test if none of the offered locales are supported.
89 require_locale ()
90 {
91 for locale in "$@"; do
92 if locale -a | grep -i "$locale" >/dev/null; then
93 return
94 fi
95 done
96
97 test_skip "no suitable locale available"
98 }
99
100 # Some stuff for doing silly progress indicators
101 if [ -t 1 ] ; then
102 progress_update ()
103 {
104 THIS="$1"
105 FIRST="$2"
106 LAST="$3"
107 arith_eval $LAST - $FIRST; RANGE=$arith_val
108 arith_eval $THIS - $FIRST; PROG=$arith_val
109 # this automatically rounds to nearest integer
110 arith_eval 100 \* $PROG / $RANGE; PERC=$arith_val
111 # note \r so next update will overwrite
112 printf '%3d%%\r' $PERC
113 }
114
115 progress_done ()
116 {
117 printf '100%%\n'
118 }
119 else
120 # don't emit anything if stdout is not connected to a tty.
121 progress_update ()
122 {
123 :
124 }
125 progress_done ()
126 {
127 :
128 }
129 fi
130
131 check_for_hard_links () {
132 set +e
133
134 printf '' > "${MH_TEST_DIR}/$$-1"
135 if link "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev/null; then
136 hard_links_supported=1
137 else
138 hard_links_supported=0
139 fi
140 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
141
142 set -e
143 }
144
145 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
146 #### but sufficient for our purpose.
147 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
148 squeeze_lines() {
149 sed '/^$/N;/\n$/D'
150 }
151
152 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
153 prepare_space() {
154 sed 's/'"`printf '\\302\\240'`"'/ /g'
155 }
156
157 #### check() requires two arguments, each the name of a file to be
158 #### diff'ed.
159 #### If the contents are same, the second file is removed. And the
160 #### first file is removed unless there's an optional argument with
161 #### a value of 'keep first'.
162 #### If different, global variable "failed" is incremented.
163 #### If there's an optional 'ignore space' argument, spacing differences
164 #### will not be considered signficant, emulating GNU diff -w. It
165 #### is assumed that the first file has already been run through
166 #### prepare_space.
167 check() {
168 first=$1; shift
169 second=$1; shift
170 keepfirst=
171 ignorespace=
172 while [ $# -gt 0 ]; do
173 case $1 in
174 'keep first') keepfirst=1 ;;
175 'ignore space') ignorespace=1 ;;
176 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
177 esac
178 shift
179 done
180
181 success=
182 if [ "$ignorespace" ]; then
183 #### POSIX diff should support -b.
184 prepare_space <"$second" | diff -b "$first" - >/dev/null && success=1
185 else
186 cmp -s "$first" "$second" && success=1
187 fi
188
189 if [ "$success" ]; then
190 [ "$keepfirst" ] || rm -f "$first"
191 rm -f "$second"
192 else
193 echo
194 #### POSIX diff should support -c.
195 diff -c "$first" "$second" || true
196 echo
197 echo "$0: test failed, outputs are in $first and $second."
198 failed=`expr ${failed:-0} + 1`
199 #### Set return status of the function.
200 [ $failed -eq 0 ]
201 fi
202 }
203
204
205 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
206 #### variable (to anything) so run_* will use valgrind.
207 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
208 #### Need absolute path to valgrind.supp in case the test does a cd.
209 NMH_TEST_PREFIX="valgrind --quiet --error-exitcode=1 \
210 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
211 fi
212
213 #### Run test under another program by setting NMH_TEST_PREFIX
214 #### environment variable to, e.g., 'valgrind --quiet'.
215 run_prog() {
216 case $1 in
217 #### Don't run valgrind on shell built-in.
218 eval\ *) "$@" ;;
219 *) ${NMH_TEST_PREFIX} "$@" ;;
220 esac
221 }
222
223
224 #### run_test() requires two arguments, the first is a program and
225 #### arguments, the second is its expected one-line output string.
226 #### If the actual output does not match that string:
227 #### an error message is printed and global variable "failed" is incremented;
228 #### if there is an optional third argument, it is used in the error message.
229 run_test() {
230 set +e
231 case $1 in
232 #### Don't run valgrind on shell built-in.
233 eval\ *) actual_output=`$1 2>&1` ;;
234 *) actual_output=`${NMH_TEST_PREFIX} $1 2>&1` ;;
235 esac
236 set -e
237 if test x"$actual_output" != x"$2"; then
238 echo "$0: ${3:-\"$1\"} expected:" 1>&2
239 echo " '$2'" 1>&2
240 echo "but instead got:" 1>&2
241 echo " '$actual_output'" 1>&2
242 failed=`expr ${failed:-0} + 1`
243 fi
244 }
245
246 setup_test ()
247 {
248 MH="${MH_TEST_DIR}/Mail/.mh_profile"
249 MHMTSCONF="${MH_INST_DIR}${nmhetcdir}/mts.conf"
250 MH_LIBEXEC_DIR="${MH_INST_DIR}${nmhexecdir}"
251 export MH MHMTSCONF MH_LIBEXEC_DIR
252
253 #
254 # Only install once
255 #
256 if [ -d "${MH_INST_DIR}${bindir}" ]; then
257 :
258 else
259 (cd "${MH_OBJ_DIR}" &&
260 make DESTDIR="${MH_INST_DIR}" SETGID_MAIL= install) ||
261 exit 1
262
263 #### Don't test with sendmail because it would really send the
264 #### mail. If configured to use sendmail, change to smtp instead
265 #### so that we use fakesmtp.
266 #### And set up the maildrop in the test directory so tests don't
267 #### use the user's real maildrop.
268 #### test-slocal needs to look at the original mts.conf, so save it.
269 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
270 sed -e 's/mts: *.*/mts: smtp/' \
271 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
272 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
273 "${MHMTSCONF}.old" >"${MHMTSCONF}"
274 fi
275
276 #### On Solaris, must set PATH after the install!
277 PATH="${MH_INST_DIR}${bindir}:${PATH}"
278 export PATH
279
280 # clean old test data
281 trap "cd $MH_TEST_DIR; rm -rf '$MH_TEST_DIR/Mail'" 0
282 # setup test data
283 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
284 cat > "$MH" <<EOF || exit 1
285 Path: ${MH_TEST_DIR}/Mail
286 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
287 fileproc: ${MH_INST_DIR}${bindir}/refile
288 libexecdir: ${MH_LIBEXEC_DIR}
289 mhbuild: -nocontentid
290 mhlproc: ${MH_LIBEXEC_DIR}/mhl
291 moreproc: cat
292 postproc: ${MH_LIBEXEC_DIR}/post
293 showproc: ${MH_LIBEXEC_DIR}/mhl
294 EOF
295
296 for f in MailAliases components digestcomps distcomps forwcomps mhl.body \
297 mhl.digest mhl.format mhl.forward mhl.headers mhl.reply \
298 mhl.replywithoutbody mhn.defaults rcvdistcomps replcomps \
299 replgroupcomps scan.MMDDYY scan.YYYYMMDD scan.curses scan.default \
300 scan.highlighted scan.mailx scan.nomime scan.size scan.time \
301 scan.timely scan.unseen
302 do
303 cp "${MH_INST_DIR}${nmhetcdir}/${f}" "${MH_TEST_DIR}/Mail" || exit 1
304 done
305
306 folder -create +inbox > /dev/null
307 # create 10 basic messages
308 for i in 1 2 3 4 5 6 7 8 9 10;
309 do
310 cat > $MH_TEST_DIR/Mail/inbox/$i <<EOF || exit 1
311 From: Test$i <test$i@example.com>
312 To: Some User <user@example.com>
313 Date: Fri, 29 Sep 2006 00:00:00
314 Message-Id: $i@test.nmh
315 Subject: Testing message $i
316
317 This is message number $i
318 EOF
319 done
320 }