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