]> diplodocus.org Git - nmh/blob - test/common.sh.in
Update manpages to use .TP for tagged paragraphs (part I).
[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 "$MULTIBYTE_ENABLED" && MULTIBYTE_ENABLED="@MULTIBYTE_ENABLED@"
17 export MH_TEST_DIR auxexecdir bindir mandir sysconfdir
18 export MULTIBYTE_ENABLED
19
20 test -z "$MH_INST_DIR" && MH_INST_DIR="${MH_TEST_DIR}/inst"
21 export MH_INST_DIR
22
23 unset MHBUILD MHCONTEXT MHMTSUSERCONF MHN MHSHOW MHSTORE MHTMPDIR
24 unset MHLDEBUG MHPDEBUG MHWDEBUG MM_CHARSET PAGER
25
26 output_md5()
27 {
28 #### Output just the checksum. If the filename needs to appear on
29 #### the same line, the caller needs to add it. This avoids
30 #### differences due to a leading '*' binary file indicator, for
31 #### text files, on some platforms (Cygwin).
32 @MD5SUM@ $* | @MD5FMT@ | cut -d ' ' -f 1
33 }
34
35 #### Use built-in $((...)) in test suite if shell supports it.
36 #### Borrowed from configure's as_fn_arith. The result is placed
37 #### in global arith_val.
38 #### Detected at run-time instead of by configure to allow testing
39 #### with different shells.
40 if (eval "test \$(( 1 + 1 )) = 2" 2>/dev/null); then
41 eval 'arith_eval () { arith_val=$(( $* )); }'
42 else
43 arith_eval () { arith_val=`expr "$@" || test $? -eq 1`; }
44 fi
45
46 test_skip ()
47 {
48 WHY="$1"
49 echo "$Test $0 SKIP ($WHY)"
50 exit 77
51 }
52
53 # portable implementation of 'which' utility
54 findprog()
55 {
56 PROG="$1"
57 #### Don't need to save current IFS because this function is run in
58 #### a subshell.
59 IFS=:
60 for D in $PATH; do
61 if [ -z "$D" ]; then
62 D=.
63 fi
64 if [ -f "$D/$PROG" -a -x "$D/$PROG" ]; then
65 printf '%s\n' "$D/$PROG"
66 break
67 fi
68 done
69 }
70
71 require_prog ()
72 {
73 if [ -z "`findprog $1`" ]; then
74 test_skip "missing $1"
75 fi
76 }
77
78 # Some stuff for doing silly progress indicators
79 if [ -t 1 ] ; then
80 progress_update ()
81 {
82 THIS="$1"
83 FIRST="$2"
84 LAST="$3"
85 arith_eval $LAST - $FIRST; RANGE=$arith_val
86 arith_eval $THIS - $FIRST; PROG=$arith_val
87 # this automatically rounds to nearest integer
88 arith_eval 100 \* $PROG / $RANGE; PERC=$arith_val
89 # note \r so next update will overwrite
90 printf '%3d%%\r' $PERC
91 }
92
93 progress_done ()
94 {
95 printf '100%%\n'
96 }
97 else
98 # don't emit anything if stdout is not connected to a tty.
99 progress_update ()
100 {
101 :
102 }
103 progress_done ()
104 {
105 :
106 }
107 fi
108
109 check_for_hard_links () {
110 set +e
111
112 printf '' > "${MH_TEST_DIR}/$$-1"
113 if link "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2" 2>/dev/null; then
114 hard_links_supported=1
115 else
116 hard_links_supported=0
117 fi
118 rm -f "${MH_TEST_DIR}/$$-1" "${MH_TEST_DIR}/$$-2"
119
120 set -e
121 }
122
123 #### check() requires two arguments, each the name of a file to be
124 #### diff'ed.
125 #### If the same, the second file is removed. And the first file is
126 #### removed unless the optional third argument has a value of
127 #### 'keep first'.
128 #### If different, global variable "failed" is incremented.
129 check() {
130 #### POSIX diff should support -c.
131 if cmp -s "$1" "$2"; then
132 test $# -lt 3 -o "$3" != 'keep first' && rm -f "$1"
133 rm -f "$2"
134 else
135 echo
136 diff -c "$1" "$2"
137 echo
138 echo "$0: test failed, outputs are in $1 and $2."
139 failed=`expr ${failed:-0} + 1`
140 fi
141 }
142
143 #### run_test() requires two arguments, the first is a program and
144 #### arguments, the second is its expected one-line output string.
145 #### If the actual output does not match that string:
146 #### an error message is printed and global variable "failed" is incremented;
147 #### if there is an optional third argument, it is used in the error message.
148 run_test() {
149 set +e
150 actual_output=`$1 2>&1`
151 set -e
152 if test x"$actual_output" != x"$2"; then
153 echo "$0: ${3:-\"$1\"} expected:" 1>&2
154 echo " '$2'" 1>&2
155 echo "but instead got:" 1>&2
156 echo " '$actual_output'" 1>&2
157 failed=`expr ${failed:-0} + 1`
158 fi
159 }
160
161 setup_test ()
162 {
163 MH="${MH_TEST_DIR}/Mail/.mh_profile"
164 MHMTSCONF="${MH_INST_DIR}${sysconfdir}/mts.conf"
165 PATH="${MH_INST_DIR}${bindir}:${PATH}"
166 MH_LIB_DIR="${MH_INST_DIR}${auxexecdir}"
167 export MH MHMTSCONF MH_LIB_DIR PATH
168
169 #
170 # Only install once
171 #
172 if [ -d "${MH_INST_DIR}${bindir}" ]; then
173 :
174 else
175 (cd "${MH_OBJ_DIR}" &&
176 make DESTDIR="${MH_INST_DIR}" SETGID_MAIL= install) ||
177 exit 1
178
179 #### Don't test with sendmail because it would really send the
180 #### mail. If configured to use sendmail, change to smtp instead
181 #### so that we use fakesmtp.
182 #### And set up the maildrop in the test directory so tests don't
183 #### use the user's real maildrop.
184 #### test-slocal needs to look at the original mts.conf, so save it.
185 mv -f "${MHMTSCONF}" "${MHMTSCONF}.old"
186 sed -e 's/mts: *.*/mts: smtp/' \
187 -e "s%mmdfldir: *.*%mmdfldir: ${MH_TEST_DIR}/Mail%" \
188 -e 's%mmdflfil: *.*%mmdflfil: maildrop%' \
189 "${MHMTSCONF}.old" >"${MHMTSCONF}"
190 fi
191
192 # clean old test data
193 trap "rm -rf '$MH_TEST_DIR/Mail'" 0
194 # setup test data
195 mkdir "$MH_TEST_DIR/Mail" || exit 1
196 cat > "$MH" <<EOF || exit 1
197 Path: ${MH_TEST_DIR}/Mail
198 buildmimeproc: ${MH_INST_DIR}${bindir}/mhbuild
199 fileproc: ${MH_INST_DIR}${bindir}/refile
200 libdir: ${MH_LIB_DIR}
201 mhbuild: -nocontentid
202 mhlproc: ${MH_LIB_DIR}/mhl
203 moreproc: cat
204 postproc: ${MH_LIB_DIR}/post
205 showproc: ${MH_LIB_DIR}/mhl
206 EOF
207
208 for f in MailAliases components digestcomps distcomps forwcomps mhl.body \
209 mhl.digest mhl.format mhl.forward mhl.headers mhl.reply \
210 mhn.defaults rcvdistcomps replcomps replgroupcomps scan.MMDDYY \
211 scan.YYYYMMDD scan.default scan.highlighted scan.mailx scan.nomime \
212 scan.size scan.time scan.timely scan.unseen
213 do
214 cp "${MH_INST_DIR}${sysconfdir}/${f}" "${MH_TEST_DIR}/Mail" || exit 1
215 done
216
217 folder -create +inbox > /dev/null
218 # create 10 basic messages
219 for i in 1 2 3 4 5 6 7 8 9 10;
220 do
221 cat > $MH_TEST_DIR/Mail/inbox/$i <<EOF || exit 1
222 From: Test$i <test$i@example.com>
223 To: Some User <user@example.com>
224 Date: Fri, 29 Sep 2006 00:00:00
225 Message-Id: $i@test.nmh
226 Subject: Testing message $i
227
228 This is message number $i
229 EOF
230 done
231 }