]> diplodocus.org Git - nmh/blob - test/common.sh.in
Updated send(1) man page to note that sendfrom looks in
[nmh] / test / common.sh.in
1 # Common helper routines for test shell scripts -- to be sourced by them
2 # @configure_input@
3
4
5 #### The following 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 export MH_TEST_DIR
10 test -z "$MH_INST_DIR" && MH_INST_DIR="${MH_TEST_DIR}/inst"
11 test -z "$prefix" && prefix=@prefix@
12 test -z "$datarootdir" && datarootdir=@datarootdir@
13 test -z "$exec_prefix" && exec_prefix=@exec_prefix@
14 test -z "$bindir" && bindir="@bindir@"
15 test -z "$mandir" && mandir="@mandir@"
16 test -z "$nmhetcdir" && nmhetcdir="@sysconfdir@/nmh"
17 #### The following doesn't support running the distcheck version of
18 #### test-mhparam standalone, but only via make distcheck.
19 test -z "$nmhetcdirinst" && nmhetcdirinst="@nmhetcdirinst@$nmhetcdir"
20 test -z "$nmhlibexecdir" && nmhlibexecdir="@libexecdir@/nmh"
21 test -z "$supported_locks" && supported_locks="@supported_locks@"
22 test -z "$default_locking" && default_locking="@default_locking@"
23 test -z "$MULTIBYTE_ENABLED" && MULTIBYTE_ENABLED="@MULTIBYTE_ENABLED@"
24 test -z "$ICONV_ENABLED" && ICONV_ENABLED="@ICONV_ENABLED@"
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 #### If you're reading this .... you can set MH_TEST_NOCLEANUP to prevent
37 #### the test suite from cleaning up the results of a test run, if you need
38 #### to do manual debugging on a test.
39
40 output_md5()
41 {
42 #### Output just the checksum. If the filename needs to appear on
43 #### the same line, the caller needs to add it. This avoids
44 #### differences due to a leading '*' binary file indicator, for
45 #### text files, on some platforms (Cygwin).
46 @MD5SUM@ $* | @MD5FMT@ | awk '{print $1}'
47 }
48
49 #### Use built-in $((...)) in test suite if shell supports it.
50 #### Borrowed from configure's as_fn_arith. The result is placed
51 #### in global arith_val.
52 #### Detected at run-time instead of by configure to allow testing
53 #### with different shells.
54 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev/null); then
55 eval 'arith_eval () { arith_val=$(( $* )); }'
56 else
57 arith_eval () { arith_val=`expr "$@" || test $? -eq 1`; }
58 fi
59
60 test_skip ()
61 {
62 WHY="$1"
63 echo "$Test $0 SKIP ($WHY)"
64 exit 77
65 }
66
67 # portable implementation of 'which' utility
68 findprog()
69 {
70 PROG="$1"
71 #### Don't need to save current IFS because this function is run in
72 #### a subshell.
73 IFS=:
74 for D in $PATH; do
75 if [ -z "$D" ]; then
76 D=.
77 fi
78 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
79 printf '%s\n' "$D/$PROG"
80 break
81 fi
82 done
83 }
84
85 require_prog ()
86 {
87 if [ -z "`findprog $1`" ]; then
88 test_skip "missing $1"
89 fi
90 }
91
92 # Skip test if none of the offered locales are supported.
93 require_locale ()
94 {
95 for locale in "$@"; do
96 if locale -a | grep -i "$locale" >/dev/null; then
97 return
98 fi
99 done
100
101 test_skip "no suitable locale available"
102 }
103
104 # Some stuff for doing silly progress indicators
105 if [ -t 1 ] ; then
106 progress_update ()
107 {
108 THIS="$1"
109 FIRST="$2"
110 LAST="$3"
111 arith_eval $LAST - $FIRST; RANGE=$arith_val
112 arith_eval $THIS - $FIRST; PROG=$arith_val
113 # this automatically rounds to nearest integer
114 arith_eval 100 \* $PROG / $RANGE; PERC=$arith_val
115 # note \r so next update will overwrite
116 printf '%3d%%\r' $PERC
117 }
118
119 progress_done ()
120 {
121 printf '100%%\n'
122 }
123 else
124 # don't emit anything if stdout is not connected to a tty.
125 progress_update ()
126 {
127 :
128 }
129 progress_done ()
130 {
131 :
132 }
133 fi
134
135 check_for_hard_links () {
136 set +e
137
138 printf '' > "${MH_TEST_DIR}/$$-1"
139 xdir_links_supported=0
140 if link "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev/null; then
141 hard_links_supported=1
142 mkdir "${MH_TEST_DIR}/xlinkdir"
143 if link "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/xlinkdir/$$-2" 2>/dev/null; then
144 xdir_links_supported=1
145 fi
146 else
147 hard_links_supported=0
148 fi
149 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
150 rm -rf "${MH_TEST_DIR}/xlinkdir"
151
152 set -e
153 }
154
155 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
156 #### but sufficient for our purpose.
157 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
158 squeeze_lines() {
159 sed '/^$/N;/\n$/D'
160 }
161
162 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
163 prepare_space() {
164 sed 's/'"`printf '\\302\\240'`"'/ /g'
165 }
166
167 #### check() requires two arguments, each the name of a file to be
168 #### diff'ed.
169 #### If the contents are same, the second file is removed. If different,
170 #### global variable "failed" is incremented.
171 #### Optional arguments:
172 #### 'keep first' -- first file is removed unless this is present.
173 #### 'ignore space' -- spacing differences will not be considered
174 #### significant, emulating GNU diff -w. It is assumed that the
175 #### first file has already been run through prepare_space.
176 #### ':' <test name> -- will print '<test name>' in the failure message,
177 #### to make it easier to tell which of multiple tests has failed.
178 check() {
179 first=$1; shift
180 second=$1; shift
181 keepfirst=
182 ignorespace=
183 label=test
184 while [ $# -gt 0 ]; do
185 case $1 in
186 'keep first') keepfirst=1 ;;
187 'ignore space') ignorespace=1 ;;
188 ':') shift; label=\'"$*"\'; break ;;
189 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
190 esac
191 shift
192 done
193
194 success=
195 if [ "$ignorespace" ]; then
196 #### POSIX diff should support -b.
197 prepare_space <"$second" | diff -b "$first" - >/dev/null && success=1
198 else
199 cmp -s "$first" "$second" && success=1
200 fi
201
202 if [ "$success" ]; then
203 [ "$keepfirst" ] || rm -f "$first"
204 rm -f "$second"
205 else
206 echo
207 #### POSIX diff should support -c.
208 diff -c "$first" "$second" || true
209 echo
210 echo "$0: $label failed, outputs are in $first and $second."
211 failed=`expr ${failed:-0} + 1`
212 #### Set return status of the function.
213 [ $failed -eq 0 ]
214 fi
215 }
216
217
218 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
219 #### variable (to anything) so run_* will use valgrind.
220 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
221 #### Need absolute path to valgrind.supp in case the test does a cd.
222 NMH_TEST_PREFIX="valgrind --quiet --error-exitcode=1 \
223 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
224 fi
225
226 #### Run test under another program by setting NMH_TEST_PREFIX
227 #### environment variable to, e.g., 'valgrind --quiet'.
228 run_prog() {
229 case $1 in
230 #### Don't run valgrind on shell built-in.
231 eval\ *) "$@" ;;
232 *) ${NMH_TEST_PREFIX} "$@" ;;
233 esac
234 }
235
236
237 #### run_test() requires two arguments, the first is a program and
238 #### arguments, the second is its expected one-line output string.
239 #### If the actual output does not match that string:
240 #### an error message is printed and global variable "failed" is incremented;
241 #### if there is an optional third argument, it is used in the error message.
242 run_test() {
243 set +e
244 case $1 in
245 #### Don't run valgrind on shell built-in.
246 eval\ *) actual_output=`$1 2>&1` ;;
247 *) actual_output=`${NMH_TEST_PREFIX} $1 2>&1` ;;
248 esac
249 set -e
250 if test x"$actual_output" != x"$2"; then
251 echo "$0: ${3:-\"$1\"} expected:" 1>&2
252 echo " '$2'" 1>&2
253 echo "but instead got:" 1>&2
254 echo " '$actual_output'" 1>&2
255 failed=`expr ${failed:-0} + 1`
256 fi
257 }
258
259 #### Function invoked by trap on exit.
260 cleanup() {
261 #### Save exit status to use as status for this program.
262 status=$?
263
264 #### Clean up test mail space.
265 #### cd to $MH_TEST_DIR before trying to remove its Mail
266 #### subdirectory. rm on Solaris won't remove it if it's in the
267 #### path of the current working directory.
268 test -z "$MH_TEST_NOCLEANUP" && (cd $MH_TEST_DIR; rm -rf "$MH_TEST_DIR"/Mail)
269
270 #### Report test name if set, which indicates failure.
271 #### Relies on set -e to invoke the trap which calls
272 #### this function on failure.
273 #### To use:
274 #### 1) Set test name before running the test, use start_test().
275 #### 2) Unset upon successful completion, use finish_test().
276 if test -n "$nmh_tests_testname"; then
277 echo $nmh_tests_testname failed
278 fi
279
280 #### Exit with non-zero status if failure. Failure is defined as either
281 #### non-empty nmh_tests_testname or non-zero exit status on entry to the
282 #### function.
283 if test -n "$nmh_tests_testname" -o $status -ne 0; then
284 test $status -ne 0 && exit $status || exit 1
285 test $status -ne 0 && exit 0 || exit 0
286 fi
287 }
288
289 #### Function to set the test name, and whatever the future brings.
290 start_test() {
291 nmh_tests_testname="$1"
292 }
293
294 #### Corresponding function to indicate that the test has finished. It need
295 #### not be called after each test, just the last one in a file.
296 finish_test() {
297 unset nmh_tests_testname
298 }
299
300 setup_test ()
301 {
302 set -e
303
304 MH="${MH_TEST_DIR}/Mail/.mh_profile"
305 MHMTSCONF="${MH_INST_DIR}${nmhetcdir}/mts.conf"
306 MH_LIBEXEC_DIR="${MH_INST_DIR}${nmhlibexecdir}"
307 export MH MHMTSCONF MH_LIBEXEC_DIR
308
309 #
310 # Only install once
311 #
312 if [ -d "${MH_INST_DIR}${bindir}" ]; then
313 :
314 else
315 (cd "${MH_OBJ_DIR}" &&
316 make DESTDIR="${MH_INST_DIR}" SETGID_MAIL= install) ||
317 exit 1
318
319 #### Don't test with sendmail because it would really send the
320 #### mail. If configured to use sendmail, change to smtp instead
321 #### so that we use fakesmtp.
322 #### And set up the maildrop in the test directory so tests don't
323 #### use the user's real maildrop.
324 #### test-slocal needs to look at the original mts.conf, so save it.
325 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
326 sed -e 's/mts: *.*/mts: smtp/' \
327 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
328 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
329 "${MHMTSCONF}.old" >"${MHMTSCONF}"
330 fi
331
332 #### On Solaris, must set PATH after the install!
333 PATH="${MH_INST_DIR}${bindir}:${PATH}"
334 export PATH
335
336 # clean old test data on exit
337 trap cleanup 0
338
339 # setup test data
340 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
341 cat > "$MH" <<EOF || exit 1
342 Path: ${MH_TEST_DIR}/Mail
343 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
344 fileproc: ${MH_INST_DIR}${bindir}/refile
345 libexecdir: ${MH_LIBEXEC_DIR}
346 mhbuild: -nocontentid
347 mhlproc: ${MH_LIBEXEC_DIR}/mhl
348 moreproc: cat
349 postproc: ${MH_LIBEXEC_DIR}/post
350 showmimeproc: ${MH_INST_DIR}${bindir}/mhshow
351 showproc: ${MH_LIBEXEC_DIR}/mhl
352 #: The following aren't currently used by the test suite, but are
353 #: defined here in case they are in the future:
354 mailproc: ${MH_INST_DIR}${bindir}/mhmail
355 rcvstoreproc: ${MH_LIBEXEC_DIR}/rcvstore
356 sendproc: ${MH_INST_DIR}${bindir}/send
357 whatnowproc: ${MH_INST_DIR}${bindir}/whatnow
358 whomproc: ${MH_INST_DIR}${bindir}/whom
359 #: incproc and packproc are defined in config.c but not used by any code.
360 EOF
361
362 if test -z '@nmhetcdirinst@'; then
363 #### This isn't used with make distcheck, so that we can use it to
364 #### be sure that etc files are not used from an existing nmh
365 #### installation.
366 #### posh doesn't like "${MH_INST_DIR}${nmhetcdir}"/*, so cd to
367 #### the directory and provide an argument without quotes to cp.
368 (cd "${MH_INST_DIR}${nmhetcdir}/" && cp * "${MH_TEST_DIR}/Mail")
369 fi
370
371 folder -create +inbox > /dev/null
372 # create 10 basic messages
373 for i in 1 2 3 4 5 6 7 8 9 10;
374 do
375 cat > $MH_TEST_DIR/Mail/inbox/$i <<EOF || exit 1
376 From: Test$i <test$i@example.com>
377 To: Some User <user@example.com>
378 Date: Fri, 29 Sep 2006 00:00:00
379 Message-Id: $i@test.nmh
380 Subject: Testing message $i
381
382 This is message number $i
383 EOF
384 done
385 }