#!/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/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 exports these for every login; the tests start from the # state the manual describes, where only `set -o` puts them there. unset _BPX_SHAREAS _BPX_SPAWN_SCRIPT 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. zos_skip="posix/printf-ext zash/ash-differences" # 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/$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" "$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 if [ "$fail" -gt 0 ]; then echo "$fail failed, $pass passed" >&2 exit 1 fi [ "$verbose" = 1 ] && echo "$pass passed, $skip skipped" exit 0