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