diff --git a/src/expect-core b/src/expect-core index 5f2adfa..64d36ff 100644 --- a/src/expect-core +++ b/src/expect-core @@ -131,3 +131,20 @@ _expect--match() [[ "$(cat)" =~ $pat ]] } + +## +# Expects that both stdin and stderr (if available) are empty +_expect--silent() +{ + local -r stderr="${3:-/dev/null}" + shift "$2" + + # we accept no arguments + test $# -eq 0 || _bail_clause silent "$*" + + # quick read using builtins; if we find any single byte, then we know that + # it is non-empty + read -N1 || read -N1 <"$stderr" || return 0 + return 1 +} + diff --git a/test/test-expect-core b/test/test-expect-core index 5856adf..28d86b4 100644 --- a/test/test-expect-core +++ b/test/test-expect-core @@ -121,3 +121,37 @@ describe match end end + +describe silent + it succeeds when both stdout and stderr are empty + # omitting stderr arg + expect _expect--silent 0 2 < <(:) + to succeed + + # empty stderr + expect _expect--silent 0 3 <(:) < <(:) + to succeed + end + + it fails when stdout is empty but stderr is not + expect _expect--silent 0 3 <( echo ) < <(:) + to fail + end + + it fails when stderr is empty but stdout is not + # omitting stderr arg + expect _expect--silent 0 2 <<< "" + to fail + + # empty stderr + expect _expect--silent 0 3 <(:) <<< "" + to fail + end + + # no arguments within context of the DSL, that is + it accepts no arguments + expect _expect--silent 0 2 foo < <(:) + to fail + end +end +