From f2e9d72c709374ab31ff58fb0b1f8f9e7f0e37d0 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 23 Dec 2019 21:40:53 -0600 Subject: Add "texpect" and add a couple simple examples to sh.test. --- scripts/runtest.sh | 94 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 9 deletions(-) (limited to 'scripts') diff --git a/scripts/runtest.sh b/scripts/runtest.sh index 25c8e104..9c19811c 100644 --- a/scripts/runtest.sh +++ b/scripts/runtest.sh @@ -95,6 +95,26 @@ wrong_args() fi } +# Announce failure and handle fallout +do_fail() +{ + FAILCOUNT=$(($FAILCOUNT+1)) + printf "%s\n" "$SHOWFAIL: $NAME" + if [ -n "$VERBOSE" ] + then + [ ! -z "$4" ] && printf "%s\n" "echo -ne \"$4\" > input" + printf "%s\n" "echo -ne '$5' |$EVAL $2" + printf "%s\n" "$DIFF" + [ "$VERBOSE" == fail ] && exit 1 + fi +} + +# Announce success +do_pass() +{ + [ "$VERBOSE" != "nopass" ] && printf "%s\n" "$SHOWPASS: $NAME" +} + # The testing function testing() @@ -124,15 +144,7 @@ testing() DIFF="$(diff -au${NOSPACE:+w} expected actual)" if [ ! -z "$DIFF" ] then - FAILCOUNT=$(($FAILCOUNT+1)) - printf "%s\n" "$SHOWFAIL: $NAME" - if [ -n "$VERBOSE" ] - then - [ ! -z "$4" ] && printf "%s\n" "echo -ne \"$4\" > input" - printf "%s\n" "echo -ne '$5' |$EVAL $2" - printf "%s\n" "$DIFF" - [ "$VERBOSE" == fail ] && exit 1 - fi + do_fail else [ "$VERBOSE" != "nopass" ] && printf "%s\n" "$SHOWPASS: $NAME" fi @@ -152,6 +164,70 @@ testcmd() testing "$X" "\"$C\" $2" "$3" "$4" "$5" } +# txpect NAME COMMAND I/O/E/Xstring +# Run COMMAND and interact with it: send I strings to input, read O or E +# strings from stdout or stderr (empty string is "any nonzero string here"), +# X means close stdin/stdout/stderr and match return code (blank means nonzero) +txpect() +{ + # Run command with redirection through fifos + NAME="$1" + + if [ $# -lt 2 ] || ! mkfifo in-$$ out-$$ err-$$ + then + do_fail + return + fi + $2 out-$$ 2>err-$$ & + shift 2 + : {IN}>in-$$ {OUT}&$IN || { do_fail;return;} ;; + + # check output from child + [OE]) + [ $LEN == 0 ] && LARG="" || LARG="-rN $LEN" + O=$OUT + [ ${1::1} == 'E' ] && O=$ERR + read -t2 $LARG A <&$O + if [ $LEN -eq 0 ] + then + [ -z "$A" ] && { do_fail;return;} + else + [ "$A" != "${1:1}" ] && { do_fail;return;} + fi + ;; + + # close I/O and wait for exit + X) + exec {IN}<&- {OUT}<&- {ERR}<&- + wait + X=$? + if [ -z "$LEN" ] + then + [ $X -eq 0 ] && { do_fail;return;} # any error + else + [ $X != "${1:1}" ] && { do_fail;return;} # specific value + fi + ;; + esac + shift + done + # In case we already closed it + exec {IN}<&- {OUT}<&- {ERR}<&- + + do_pass +} + # Recursively grab an executable and all the libraries needed to run it. # Source paths beginning with / will be copied into destpath, otherwise # the file is assumed to already be there and only its library dependencies -- cgit v1.2.3