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