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