assert_output without args asserts output is non-empty
diff --git a/src/assert.bash b/src/assert.bash index 5dba42c..b680fad 100644 --- a/src/assert.bash +++ b/src/assert.bash
@@ -185,9 +185,14 @@ assert_output() { local -i is_mode_partial=0 local -i is_mode_regexp=0 + local -i is_mode_nonempty=0 local -i use_stdin=0 # Handle options. + if (( $# == 0 )); then + is_mode_nonempty=1 + fi + while (( $# > 0 )); do case "$1" in -p|--partial) is_mode_partial=1; shift ;; @@ -214,7 +219,13 @@ fi # Matching. - if (( is_mode_regexp )); then + if (( is_mode_nonempty )); then + if [ -z "$output" ]; then + echo 'expected non-empty output, but output was empty' \ + | batslib_decorate 'no output' \ + | fail + fi + elif (( is_mode_regexp )); then if [[ '' =~ $expected ]] || (( $? == 2 )); then echo "Invalid extended regular expression: \`$expected'" \ | batslib_decorate 'ERROR: assert_output' \
diff --git a/test/50-assert-15-assert_output.bats b/test/50-assert-15-assert_output.bats index 7efee71..16ced75 100755 --- a/test/50-assert-15-assert_output.bats +++ b/test/50-assert-15-assert_output.bats
@@ -26,6 +26,25 @@ [ "${lines[3]}" == '--' ] } +@test 'assert_output(): succeeds if output is non-empty' { + run echo 'a' + run assert_output +echo "$output" + [ "$status" -eq 0 ] + [ "${#lines[@]}" -eq 0 ] +} + +@test 'assert_output(): fails if output is empty' { + run echo '' + run assert_output +echo "$output" + [ "$status" -eq 1 ] + [ "${#lines[@]}" -eq 3 ] + [ "${lines[0]}" == '-- no output --' ] + [ "${lines[1]}" == 'expected non-empty output, but output was empty' ] + [ "${lines[2]}" == '--' ] +} + @test 'assert_output() - : reads <expected> from STDIN' { run echo 'a' run assert_output - <<STDIN