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