]> diplodocus.org Git - nmh/blob - test/common.sh.in
Make sure we don't modify the original text when processing the disposition
[nmh] / test / common.sh.in
1 # Common helper routines for test shell scripts -- intended to be sourced by them
2 # @configure_input@
3
4
5 #### The following exported 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 "$prefix" && prefix=@prefix@
10 test -z "$datarootdir" && datarootdir=@datarootdir@
11 test -z "$exec_prefix" && exec_prefix=@exec_prefix@
12 test -z "$auxexecdir" && auxexecdir="@libdir@"
13 test -z "$bindir" && bindir="@bindir@"
14 test -z "$mandir" && mandir="@mandir@"
15 test -z "$sysconfdir" && sysconfdir="@sysconfdir@"
16 test -z "$supported_locks" && supported_locks="@supported_locks@"
17 test -z "$MULTIBYTE_ENABLED" && MULTIBYTE_ENABLED="@MULTIBYTE_ENABLED@"
18 test -z "$ICONV_ENABLED" && ICONV_ENABLED="@ICONV_ENABLED@"
19 export MH_TEST_DIR auxexecdir bindir mandir sysconfdir
20 export MULTIBYTE_ENABLED ICONV_ENABLED
21
22 test -z "$MH_INST_DIR" && MH_INST_DIR="${MH_TEST_DIR}/inst"
23 export MH_INST_DIR
24
25 unset 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_TYPE
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 # Some stuff for doing silly progress indicators
88 if [ -t 1 ] ; then
89 progress_update ()
90 {
91 THIS="$1"
92 FIRST="$2"
93 LAST="$3"
94 arith_eval $LAST - $FIRST; RANGE=$arith_val
95 arith_eval $THIS - $FIRST; PROG=$arith_val
96 # this automatically rounds to nearest integer
97 arith_eval 100 \* $PROG / $RANGE; PERC=$arith_val
98 # note \r so next update will overwrite
99 printf '%3d%%\r' $PERC
100 }
101
102 progress_done ()
103 {
104 printf '100%%\n'
105 }
106 else
107 # don't emit anything if stdout is not connected to a tty.
108 progress_update ()
109 {
110 :
111 }
112 progress_done ()
113 {
114 :
115 }
116 fi
117
118 check_for_hard_links () {
119 set +e
120
121 printf '' > "${MH_TEST_DIR}/$$-1"
122 if link "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev/null; then
123 hard_links_supported=1
124 else
125 hard_links_supported=0
126 fi
127 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
128
129 set -e
130 }
131
132 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
133 #### but sufficient for our purpose.
134 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
135 squeeze_lines() {
136 sed '/^$/N;/\n$/D'
137 }
138
139 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
140 prepare_space() {
141 sed 's/'"`printf '\\302\\240'`"'/ /g'
142 }
143
144 #### check() requires two arguments, each the name of a file to be
145 #### diff'ed.
146 #### If the contents are same, the second file is removed. And the
147 #### first file is removed unless there's an optional argument with
148 #### a value of 'keep first'.
149 #### If different, global variable "failed" is incremented.
150 #### If there's an optional 'ignore space' argument, spacing differences
151 #### will not be considered signficant, emulating GNU diff -w. It
152 #### is assumed that the first file has already been run through
153 #### prepare_space.
154 check() {
155 first=$1; shift
156 second=$1; shift
157 keepfirst=
158 ignorespace=
159 while [ $# -gt 0 ]; do
160 case $1 in
161 'keep first') keepfirst=1 ;;
162 'ignore space') ignorespace=1 ;;
163 *) echo "$0: invalid check() argument \"$1\" in test suite" >&2 ;;
164 esac
165 shift
166 done
167
168 success=
169 if [ "$ignorespace" ]; then
170 #### POSIX diff should support -b.
171 prepare_space <"$second" | diff -b "$first" - >/dev/null && success=1
172 else
173 cmp -s "$first" "$second" && success=1
174 fi
175
176 if [ "$success" ]; then
177 [ "$keepfirst" ] || rm -f "$first"
178 rm -f "$second"
179 else
180 echo
181 #### POSIX diff should support -c.
182 diff -c "$first" "$second" || true
183 echo
184 echo "$0: test failed, outputs are in $first and $second."
185 failed=`expr ${failed:-0} + 1`
186 #### Set return status of the function.
187 [ $failed -eq 0 ]
188 fi
189 }
190
191
192 #### Shortcut to enable use of valgrind: set NMH_VALGRIND environment
193 #### variable (to anything) so run_* will use valgrind.
194 if [ "${NMH_VALGRIND}" -a -z "${NMH_TEST_PREFIX}" ]; then
195 #### Need absolute path to valgrind.supp in case the test does a cd.
196 NMH_TEST_PREFIX="valgrind --quiet --error-exitcode=1 \
197 --suppressions=`cd ${srcdir} && pwd`/test/valgrind.supp"
198 fi
199
200 #### Run test under another program by setting NMH_TEST_PREFIX
201 #### environment variable to, e.g., 'valgrind --quiet'.
202 run_prog() {
203 case $1 in
204 #### Don't run valgrind on shell built-in.
205 eval\ *) "$@" ;;
206 *) ${NMH_TEST_PREFIX} "$@" ;;
207 esac
208 }
209
210
211 #### run_test() requires two arguments, the first is a program and
212 #### arguments, the second is its expected one-line output string.
213 #### If the actual output does not match that string:
214 #### an error message is printed and global variable "failed" is incremented;
215 #### if there is an optional third argument, it is used in the error message.
216 run_test() {
217 set +e
218 case $1 in
219 #### Don't run valgrind on shell built-in.
220 eval\ *) actual_output=`$1 2>&1` ;;
221 *) actual_output=`${NMH_TEST_PREFIX} $1 2>&1` ;;
222 esac
223 set -e
224 if test x"$actual_output" != x"$2"; then
225 echo "$0: ${3:-\"$1\"} expected:" 1>&2
226 echo " '$2'" 1>&2
227 echo "but instead got:" 1>&2
228 echo " '$actual_output'" 1>&2
229 failed=`expr ${failed:-0} + 1`
230 fi
231 }
232
233 setup_test ()
234 {
235 MH="${MH_TEST_DIR}/Mail/.mh_profile"
236 MHMTSCONF="${MH_INST_DIR}${sysconfdir}/mts.conf"
237 MH_LIB_DIR="${MH_INST_DIR}${auxexecdir}"
238 export MH MHMTSCONF MH_LIB_DIR
239
240 #
241 # Only install once
242 #
243 if [ -d "${MH_INST_DIR}${bindir}" ]; then
244 :
245 else
246 (cd "${MH_OBJ_DIR}" &&
247 make DESTDIR="${MH_INST_DIR}" SETGID_MAIL= install) ||
248 exit 1
249
250 #### Don't test with sendmail because it would really send the
251 #### mail. If configured to use sendmail, change to smtp instead
252 #### so that we use fakesmtp.
253 #### And set up the maildrop in the test directory so tests don't
254 #### use the user's real maildrop.
255 #### test-slocal needs to look at the original mts.conf, so save it.
256 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
257 sed -e 's/mts: *.*/mts: smtp/' \
258 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
259 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
260 "${MHMTSCONF}.old" >"${MHMTSCONF}"
261 fi
262
263 #### On Solaris, must set PATH after the install!
264 PATH="${MH_INST_DIR}${bindir}:${PATH}"
265 export PATH
266
267 # clean old test data
268 trap "rm -rf '$MH_TEST_DIR/Mail'" 0
269 # setup test data
270 mkdir -p "$MH_TEST_DIR/Mail" || exit 1
271 cat > "$MH" <<EOF || exit 1
272 Path: ${MH_TEST_DIR}/Mail
273 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
274 fileproc: ${MH_INST_DIR}${bindir}/refile
275 libdir: ${MH_LIB_DIR}
276 mhbuild: -nocontentid
277 mhlproc: ${MH_LIB_DIR}/mhl
278 moreproc: cat
279 postproc: ${MH_LIB_DIR}/post
280 showproc: ${MH_LIB_DIR}/mhl
281 EOF
282
283 for f in MailAliases components digestcomps distcomps forwcomps mhl.body \
284 mhl.digest mhl.format mhl.forward mhl.headers mhl.reply \
285 mhn.defaults rcvdistcomps replcomps replgroupcomps scan.MMDDYY \
286 scan.YYYYMMDD scan.curses scan.default scan.highlighted scan.mailx \
287 scan.nomime scan.size scan.time scan.timely scan.unseen
288 do
289 cp "${MH_INST_DIR}${sysconfdir}/${f}" "${MH_TEST_DIR}/Mail" || exit 1
290 done
291
292 folder -create +inbox > /dev/null
293 # create 10 basic messages
294 for i in 1 2 3 4 5 6 7 8 9 10;
295 do
296 cat > $MH_TEST_DIR/Mail/inbox/$i <<EOF || exit 1
297 From: Test$i <test$i@example.com>
298 To: Some User <user@example.com>
299 Date: Fri, 29 Sep 2006 00:00:00
300 Message-Id: $i@test.nmh
301 Subject: Testing message $i
302
303 This is message number $i
304 EOF
305 done
306 }