1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#!/bin/sh
# Run the zash test suite.
#
# sh tests/run.sh ./zash [name-pattern]
#
# tests/posix/*.sh behaviour every POSIX shell agrees on; the .exp files
# were produced by a reference shell (see tests/regen.sh)
# tests/scripts/*.sh integration: realistic scripts that combine many
# features; expected output from the reference shell
# tests/zash/*.sh behaviour specific to zash
# tests/proc/*.sh process accounting: how many spawns and forks a script
# costs (the last line of .exp is the expected count)
# tests/perf/*.sh performance guards: a start-up cost target, and others
# that fail when the measured cost is gone (see each file)
# tests/linux/*.sh run only on Linux (error text, /dev/full)
# tests/zos/*.sh run only on z/OS (spawn() and BPX environment)
#
# Tests named *.ascii.sh depend on ASCII code points or collation and are
# skipped when the shell under test is an EBCDIC build.
#
# This runner checks behaviour only. The sanitizer and Valgrind runs are
# separate make targets (asan, valgrind) and exist only on Linux.
#
# $TSH is the shell under test and $TESTS this directory. A test that
# cannot run on this system exits with status 77 and is counted as skipped
# (the interactive tests need python3 for a pseudo-terminal).
#
# Each test runs in an empty scratch directory. Its stdout and stderr,
# followed by "[status N]", must equal the .exp file. ZASH_TEST_WRAP may
# name a wrapper such as valgrind, and ZASH_TEST_SKIP a list of test names
# to leave out. Nothing is printed for a passing test
# unless -v is given.
verbose=0
if [ "$1" = -v ]; then
verbose=1
shift
fi
shell=${1:?usage: run.sh [-v] shell [pattern]}
pattern=${2:-}
case $shell in
/*) ;;
*) shell=$PWD/$shell ;;
esac
TSH=$shell
export TSH
here=$(cd "$(dirname "$0")" && pwd)
TESTS=$here
export TESTS
work=${TMPDIR:-/tmp}/zash-test.$$
trap 'rm -rf "$work"' EXIT
trap 'exit 1' INT TERM
mkdir -p "$work" || exit 1
case $(uname -s) in
Linux) platform=linux ;;
OS/390) platform=zos ;;
*) platform=none ;;
esac
# The .exp files were recorded with GNU tools. z/OS puts its own versions
# first on PATH, so give the tests the GNU ones where they differ. They are
# links, not shell scripts: a script found on PATH loses its stdin pipe
# inside a z/OS spawn-script.
if [ "$platform" = zos ]; then
# /etc/profile and zopen export these; the tests start from the state
# the manual describes, where only `set -o` or the test sets them.
unset _BPX_SHAREAS _BPX_SPAWN_SCRIPT _TAG_REDIR_IN _TAG_REDIR_OUT _TAG_REDIR_ERR
mkdir -p "$work/gnu"
for t in sleep wc sort tr od; do
g=$(command -v "g$t") && ln -s "$g" "$work/gnu/$t"
done
PATH=$work/gnu:$PATH
export PATH
fi
# z/OS also adds to stderr what Linux never prints: the LE banner for every
# process killed by a signal, and EDC message ids (with their full stop) in
# strerror() text.
drop=
if [ "$platform" = zos ]; then
drop='/^CEE5[0-9][0-9][0-9]S The signal SIG.* was received\.$/d;/EDC[0-9][0-9][0-9][0-9]I /{s/EDC[0-9][0-9][0-9][0-9]I //;s/\.$//;}'
fi
# Tests whose .exp records what GNU od printed on Linux. On z/OS the GNU
# tools are ASCII programs and the shell writes IBM-1047 into the pipe, so
# those bytes reach od as octal escapes -- even /bin/sh cannot produce the
# recorded text, and the character set cannot be fixed from the shell side.
# zash/script-errors: a directory reads as empty input and bit mode does not
# decide access here, so its two POSIX cases cannot happen; tests/zos/
# script-errors records what does.
zos_skip="posix/printf-ext zash/ash-differences zash/script-errors"
# IBM cmp and diff give up on output holding NUL bytes, which posix/printf-ext
# produces on purpose.
cmpcmd=$(command -v gcmp || echo cmp)
diffcmd=$(command -v gdiff || echo diff)
# "\101" is "A" only if the shell was built for ASCII.
if [ "$("$shell" -c "printf '\\101'")" = A ]; then
ascii=1
else
ascii=0
fi
pass=0
fail=0
skip=0
for t in "$here"/posix/*.sh "$here"/scripts/*.sh "$here"/zash/*.sh \
"$here"/proc/*.sh "$here"/perf/*.sh "$here/$platform"/*.sh; do
[ -f "$t" ] || continue
case $t in
*.ascii.sh) [ "$ascii" = 1 ] || continue ;;
esac
name=${t#"$here"/}
name=${name%.sh}
case $name in
*"$pattern"*) ;;
*) continue ;;
esac
case " $ZASH_TEST_SKIP " in
*" $name "*) continue ;;
esac
case " $zos_skip " in
*" $name "*)
if [ "$platform" = zos ]; then
skip=$((skip + 1))
[ "$verbose" = 1 ] && echo "skip $name (character set)"
continue
fi
;;
esac
dir=$work/d
rm -rf "$dir"
mkdir "$dir"
case $name in
proc/*) ZASH_PROCSTAT=1 && export ZASH_PROCSTAT ;;
*) unset ZASH_PROCSTAT ;;
esac
(
cd "$dir" || exit 1
# shellcheck disable=SC2086
$ZASH_TEST_WRAP "$shell" "$t" </dev/null >"$work/out" 2>&1
echo "[status $?]" >>"$work/out"
)
# An EBCDIC build's transcript is EBCDIC: convert before comparing, so
# the .ebcdic.exp files can be ordinary text.
if [ "$ZASH_EXP_SUFFIX" = ".ebcdic" ] &&
iconv -f IBM-1047 -t ISO8859-1 "$work/out" >"$work/conv" 2>/dev/null
then
mv "$work/conv" "$work/out"
fi
# A test that cannot run here (no pseudo-terminal driver) exits 77.
if [ "$(tail -n 1 "$work/out")" = "[status 77]" ]; then
skip=$((skip + 1))
[ "$verbose" = 1 ] && echo "skip $name"
continue
fi
# Error messages name the shell and script; make them comparable.
sed -e "s|$t|SCRIPT|g" -e "s|$shell|SHELL|g" -e "$drop" "$work/out" >"$work/got"
exp="${t%.sh}.exp"
# A test whose answer depends on the shell's character set can record
# one per mode: tests/zos/ebcdic.ebcdic.exp, named by the caller.
if [ -n "$ZASH_EXP_SUFFIX" ] && [ -f "${t%.sh}$ZASH_EXP_SUFFIX.exp" ]; then
exp="${t%.sh}$ZASH_EXP_SUFFIX.exp"
fi
if "$cmpcmd" -s "$work/got" "$exp"; then
pass=$((pass + 1))
[ "$verbose" = 1 ] && echo "ok $name"
else
fail=$((fail + 1))
echo "FAIL $name" >&2
"$diffcmd" "$exp" "$work/got" | sed 's/^/ /' >&2
fi
done
# One machine-readable line per pass: zopen_check_results sums them.
echo "zash tests: $pass passed, $skip skipped, $fail failed"
[ "$fail" -eq 0 ] || exit 1
exit 0
|