2 # The line above is just for convenience. Normally tests will be run using
3 # a specified version of zsh. With dynamic loading, any required libraries
4 # must already have been installed in that case.
6 # Takes one argument: the name of the test file. Currently only one such
7 # file will be processed each time ztst.zsh is run. This is slower, but
8 # much safer in terms of preserving the correct status.
9 # To avoid namespace pollution, all functions and parameters used
10 # only by the script begin with ZTST_.
12 # Options (without arguments) may precede the test file argument; these
13 # are interpreted as shell options to set. -x is probably the most useful.
15 # Produce verbose messages if non-zero.
16 # If 1, produce reports of tests executed; if 2, also report on progress.
17 # Defined in such a way that any value from the environment is used.
20 # We require all options to be reset, not just emulation options.
21 # Unfortunately, due to the crud which may be in /etc/zshenv this might
22 # still not be good enough. Maybe we should trick it somehow.
25 # Ensure the locale does not screw up sorting. Don't supply a locale
26 # unless there's one set, to minimise problems.
27 [[ -n $LC_ALL ]] && LC_ALL
=C
28 [[ -n $LC_COLLATE ]] && LC_COLLATE
=C
29 [[ -n $LC_NUMERIC ]] && LC_NUMERIC
=C
30 [[ -n $LC_MESSAGES ]] && LC_MESSAGES
=C
31 [[ -n $LANG ]] && LANG
=C
33 # Don't propagate variables that are set by default in the shell.
36 # Set the module load path to correspond to this build of zsh.
37 # This Modules directory should have been created by "make check".
38 [[ -d Modules
/zsh
]] && module_path
=( $PWD/Modules
)
39 # Allow this to be passed down.
42 # We need to be able to save and restore the options used in the test.
43 # We use the $options variable of the parameter module for this.
44 zmodload
-i zsh
/parameter
46 # Note that both the following are regular arrays, since we only use them
47 # in whole array assignments to/from $options.
48 # Options set in test code (i.e. by default all standard options)
49 ZTST_testopts
=(${(kv)options})
51 setopt extendedglob nonomatch
52 while [[ $1 = [-+]* ]]; do
56 # Options set in main script
57 ZTST_mainopts
=(${(kv)options})
59 # We run in the current directory, so remember it.
63 integer ZTST_testfailed
65 # This is POSIX nonsense. Because of the vague feeling someone, somewhere
66 # may one day need to examine the arguments of "tail" using a standard
67 # option parser, every Unix user in the world is expected to switch
68 # to using "tail -n NUM" instead of "tail -NUM". Older versions of
69 # tail don't support this.
73 if [[ -z $TAIL_SUPPORTS_MINUS_N ]]; then
75 test=$
(echo "foo\nbar" |
command tail -n 1 2>/dev
/null
)
76 if [[ $test = bar
]]; then
77 TAIL_SUPPORTS_MINUS_N
=1
79 TAIL_SUPPORTS_MINUS_N
=0
83 integer argi
=${argv[(i)-<->]}
85 if [[ $argi -le $# && $TAIL_SUPPORTS_MINUS_N = 1 ]]; then
86 argv
[$argi]=(-n ${argv[$argi][2,-1]})
89 command tail "$argv[@]"
92 # The source directory is not necessarily the current directory,
93 # but if $0 doesn't contain a `/' assume it is.
94 if [[ $0 = */* ]]; then
99 [[ $ZTST_srcdir = /* ]] || ZTST_srcdir
="$ZTST_testdir/$ZTST_srcdir"
101 # Set the function autoload paths to correspond to this build of zsh.
102 fpath
=( $ZTST_srcdir/..
/Functions
/*~
*/CVS
(/)
103 $ZTST_srcdir/..
/Completion
104 $ZTST_srcdir/..
/Completion
/*/*~
*/CVS
(/) )
106 : ${TMPPREFIX:=/tmp/zsh}
107 # Temporary files for redirection inside tests.
108 ZTST_in
=${TMPPREFIX}.ztst.
in.$$
109 # hold the expected output
110 ZTST_out
=${TMPPREFIX}.ztst.out.$$
111 ZTST_err
=${TMPPREFIX}.ztst.err.$$
112 # hold the actual output from the test
113 ZTST_tout
=${TMPPREFIX}.ztst.tout.$$
114 ZTST_terr
=${TMPPREFIX}.ztst.terr.$$
118 rm -rf $ZTST_testdir/dummy.tmp
$ZTST_testdir/*.tmp
(N
) \
119 ${TMPPREFIX}.ztst
*$$
(N
)
122 # This cleanup always gets performed, even if we abort. Later,
123 # we should try and arrange that any test-specific cleanup
124 # always gets called as well.
125 ##trap 'print cleaning up...
126 ##ZTST_cleanup' INT QUIT TERM
127 # Make sure it's clean now.
128 rm -rf dummy.tmp
*.tmp
130 # Report failure. Note that all output regarding the tests goes to stdout.
131 # That saves an unpleasant mixture of stdout and stderr to sort out.
133 print
-r "Test $ZTST_testname failed: $1"
134 if [[ -n $ZTST_message ]]; then
135 print
-r "Was testing: $ZTST_message"
137 print
-r "$ZTST_testname: test failed."
138 if [[ -n $ZTST_failmsg ]]; then
139 print
-r "The following may (or may not) help identifying the cause:
146 # Print messages if $ZTST_verbose is non-empty
150 if [[ -n $ZTST_verbose && $ZTST_verbose -ge $lev ]]; then
151 print
-r -u $ZTST_fd -- $
*
155 if [[ ZTST_verbose
-le 0 && -t $ZTST_fd ]]; then
156 print
-n -u$ZTST_fd -- ${(pl:SECONDS::\#::\#\r:)}
158 (( SECONDS
> COLUMNS
+1 && (SECONDS
-= COLUMNS
) ))
161 if [[ ! -r $ZTST_testname ]]; then
162 ZTST_testfailed
"can't read test file."
167 exec {ZTST_input
}<$ZTST_testname
169 # The current line read from the test file.
171 # The current section being run
174 # Get a new input line. Don't mangle spaces; set IFS locally to empty.
175 # We shall skip comments at this level.
179 read -u $ZTST_input -r ZTST_curline ||
return 1
180 [[ $ZTST_curline == \
#* ]] || return 0
184 # Get the name of the section. It may already have been read into
185 # $curline, or we may have to skip some initial comments to find it.
186 # If argument present, it's OK to skip the reset of the current section,
187 # so no error if we find garbage.
189 local match mbegin mend
191 while [[ $ZTST_curline != '%'(#b)([[:alnum:]]##)* ]]; do
192 ZTST_getline ||
return 1
193 [[ $ZTST_curline = [[:blank
:]]# ]] && continue
194 if [[ $# -eq 0 && $ZTST_curline != '%'[[:alnum
:]]##* ]]; then
195 ZTST_testfailed
"bad line found before or after section:
200 # have the next line ready waiting
202 ZTST_cursect
=${match[1]}
203 ZTST_verbose
2 "ZTST_getsect: read section name: $ZTST_cursect"
207 # Read in an indented code chunk for execution
209 # Code chunks are always separated by blank lines or the
210 # end of a section, so if we already have a piece of code,
211 # we keep it. Currently that shouldn't actually happen.
213 # First find the chunk.
214 while [[ $ZTST_curline = [[:blank
:]]# ]]; do
215 ZTST_getline ||
break
217 while [[ $ZTST_curline = [[:blank
:]]##[^[:blank:]]* ]]; do
218 ZTST_code
="${ZTST_code:+${ZTST_code}
220 ZTST_getline ||
break
222 ZTST_verbose
2 "ZTST_getchunk: read code chunk:
227 # Read in a piece for redirection.
229 local char
=${ZTST_curline[1]} fn
230 ZTST_redir
=${ZTST_curline[2,-1]}
231 while ZTST_getline
; do
232 [[ $ZTST_curline[1] = $char ]] ||
break
233 ZTST_redir
="${ZTST_redir}
234 ${ZTST_curline[2,-1]}"
236 ZTST_verbose
2 "ZTST_getredir: read redir for '$char':
246 (*) ZTST_testfailed
"bad redir operator: $char"
250 if [[ $ZTST_flags = *q
* && $char = '<' ]]; then
251 # delay substituting output until variables are set
252 print
-r -- "${(e)ZTST_redir}" >>$fn
254 print
-r -- "$ZTST_redir" >>$fn
260 # Execute an indented chunk. Redirections will already have
261 # been set up, but we need to handle the options.
263 options
=($ZTST_testopts)
266 # careful... ksh_arrays may be in effect.
267 ZTST_testopts
=(${(kv)options[*]})
268 options
=(${ZTST_mainopts[*]})
269 ZTST_verbose
2 "ZTST_execchunk: status $ZTST_status"
273 # Functions for preparation and cleaning.
274 # When cleaning up (non-zero string argument), we ignore status.
276 # Execute indented code chunks.
277 while ZTST_getchunk
; do
278 ZTST_execchunk
>/dev
/null ||
[[ -n $1 ]] ||
{
279 [[ -n "$ZTST_unimplemented" ]] ||
280 ZTST_testfailed
"non-zero status from preparation code:
281 $ZTST_code" && return 0
288 local diff_out diff_ret
290 diff_out
=$
(diff "$@")
292 if [[ "$diff_ret" != "0" ]]; then
300 local last match mbegin mend found substlines
303 rm -f $ZTST_in $ZTST_out $ZTST_err
304 touch $ZTST_in $ZTST_out $ZTST_err
309 ZTST_verbose
2 "ZTST_test: looking for new test"
312 ZTST_verbose
2 "ZTST_test: examining line:
314 case $ZTST_curline in
315 (%*) if [[ $found = 0 ]]; then
323 if [[ $found = 0 ]]; then
324 ZTST_getline ||
break 2
330 ([[:space
:]]##[^[:space:]]*) ZTST_getchunk
331 if [[ $ZTST_curline == (#b)([-0-9]##)([[:alpha:]]#)(:*)# ]]; then
332 ZTST_xstatus
=$match[1]
334 ZTST_message
=${match[3]:+${match[3][2,-1]}}
336 ZTST_testfailed
"expecting test status at:
343 ('<'*) ZTST_getredir ||
return 1
346 ('>'*) ZTST_getredir ||
return 1
349 ('?'*) ZTST_getredir ||
return 1
352 ('F:'*) ZTST_failmsg
="${ZTST_failmsg:+${ZTST_failmsg}
353 } ${ZTST_curline[3,-1]}"
357 (*) ZTST_testfailed
"bad line in test block:
364 # If we found some code to execute...
365 if [[ -n $ZTST_code ]]; then
367 ZTST_verbose
1 "Running test: $ZTST_message"
368 ZTST_verbose
2 "ZTST_test: expecting status: $ZTST_xstatus"
369 ZTST_verbose
2 "Input: $ZTST_in, output: $ZTST_out, error: $ZTST_terr"
371 ZTST_execchunk
<$ZTST_in >$ZTST_tout 2>$ZTST_terr
373 # First check we got the right status, if specified.
374 if [[ $ZTST_xstatus != - && $ZTST_xstatus != $ZTST_status ]]; then
375 ZTST_testfailed
"bad status $ZTST_status, expected $ZTST_xstatus from:
376 $ZTST_code${$(<$ZTST_terr):+
382 ZTST_verbose
2 "ZTST_test: test produced standard output:
384 ZTST_test: and standard error:
387 # Now check output and error.
388 if [[ $ZTST_flags = *q
* && -s $ZTST_out ]]; then
389 substlines
="$(<$ZTST_out)"
391 print
-r -- "${(e)substlines}" >$ZTST_out
393 if [[ $ZTST_flags != *d
* ]] && ! ZTST_diff
-c $ZTST_out $ZTST_tout; then
394 ZTST_testfailed
"output differs from expected as shown above for:
395 $ZTST_code${$(<$ZTST_terr):+
400 if [[ $ZTST_flags = *q
* && -s $ZTST_err ]]; then
401 substlines
="$(<$ZTST_err)"
403 print
-r -- "${(e)substlines}" >$ZTST_err
405 if [[ $ZTST_flags != *D
* ]] && ! ZTST_diff
-c $ZTST_err $ZTST_terr; then
406 ZTST_testfailed
"error output differs from expected as shown above for:
411 ZTST_verbose
1 "Test successful."
412 [[ -n $last ]] && break
415 ZTST_verbose
2 "ZTST_test: all tests successful"
417 # reset message to keep ZTST_testfailed output correct
422 # Remember which sections we've done.
423 typeset
-A ZTST_sects
424 ZTST_sects
=(prep
0 test 0 clean
0)
426 print
"$ZTST_testname: starting."
428 # Now go through all the different sections until the end.
429 # prep section may set ZTST_unimplemented, in this case the actual
430 # tests will be skipped
433 while [[ -z "$ZTST_unimplemented" ]] && ZTST_getsect
$ZTST_skipok; do
434 case $ZTST_cursect in
435 (prep
) if (( ${ZTST_sects[prep]} + ${ZTST_sects[test]} + \
436 ${ZTST_sects[clean]} )); then
437 ZTST_testfailed
"\`prep' section must come first"
444 if (( ${ZTST_sects[test]} + ${ZTST_sects[clean]} )); then
445 ZTST_testfailed
"bad placement of \`test' section"
448 # careful here: we can't execute ZTST_test before || or &&
449 # because that affects the behaviour of traps in the tests.
451 (( $?
)) && ZTST_skipok
=1
455 if (( ${ZTST_sects[test]} == 0 ||
${ZTST_sects[clean]} )); then
456 ZTST_testfailed
"bad use of \`clean' section"
463 *) ZTST_testfailed
"bad section name: $ZTST_cursect"
468 if [[ -n "$ZTST_unimplemented" ]]; then
469 print
"$ZTST_testname: skipped ($ZTST_unimplemented)"
471 elif (( ! $ZTST_testfailed )); then
472 print
"$ZTST_testname: all tests successful."
475 exit $
(( ZTST_testfailed
))