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