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