`output' expectation handler now supports stdout

This requires a little bit of file descriptor wizardry, but works; I will
need to abstract it for other handlers that will need to do the same thing.
stderr
Mike Gerwitz 2014-05-12 21:39:26 -04:00
parent 0914d27000
commit a8d39919d7
2 changed files with 66 additions and 13 deletions

View File

@ -60,20 +60,57 @@ _expect--fail() { test "$1" -ne 0; }
_expect--output() _expect--output()
{ {
shift "$2" local -ri nshift="$2"
local -r stderr="$3"
shift "$nshift"
# output-specific clauses
local -r cmp="$1" local -r cmp="$1"
shift shift
local -r clause="$*" local -ar clause=("$@")
local nl= local nl
if [ -n "$clause" ]; then local intype
[ "$clause" == 'without newline' ] \ {
&& nl=-n \ __expect--output-clause "${clause[@]}" | {
|| _bail_clause output "$clause" IFS=\| read nl intype
if [ "$intype" == stderr ]; then
__chk-nshift 3 "$nshift"
exec 99<"$stderr"
fi fi
# we will eventually be interested in this output # we will eventually be interested in this output
diff <( echo $nl "$cmp" ) - &>/dev/null # TODO: fast check first, diff if non-match
diff <( echo $nl "$cmp" ) - <&99 &>/dev/null
}
} 99<&0
# long version in case pipefail isn't supported on the system
[ "${PIPESTATUS[0]}" -eq 0 -a "${PIPESTATUS[1]}" -eq 0 ]
}
__expect--output-clause()
{
[ $# -gt 0 ] || return 0
local nl=
local input=
if [ "$1 $2" == 'without newline' ]; then
nl=-n
shift 2
fi
if [ $# -gt 0 ]; then
if [[ "$1 $2" =~ ^on\ std(err|out) ]]; then
[ "$2" == stderr ] && input="$2"
else
_bail_clause output "$*"
fi
fi
echo "$nl|$input"
} }

View File

@ -52,16 +52,32 @@ describe output
to succeed to succeed
end end
# by convention, stderr file passed as third argument
it can assert against stderr output
{
expect _expect--output 0 3 /dev/fd/3 \
"test stderr" on stderr
to succeed
} 3< <( echo "test stderr" )
end
it fails on stdout mismatch
expect _expect--output 0 2 "foo" <<< "bar"
to fail
end
it fails on stderr mismatch
{
expect _expect--output 0 3 /dev/fd/3 "foo" on stderr
to fail
} 3< <( echo bar )
end
it ignores exit code it ignores exit code
expect _expect--output 1 2 "foo" <<< "foo" expect _expect--output 1 2 "foo" <<< "foo"
to succeed to succeed
end end
it fails on output mismatch
expect _expect--output 0 2 "foo" <<< "bar"
to fail
end
# as is good practice for Unix utilities # as is good practice for Unix utilities
it expects trailing newline by default it expects trailing newline by default
expect _expect--output 0 2 "foo" < <( echo -n foo ) expect _expect--output 0 2 "foo" < <( echo -n foo )