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