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