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