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