From 1a758415cc546fd1794030d88b87a20f493df8fc Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Thu, 16 Jun 2016 14:48:33 -0400 Subject: [PATCH 1/9] stdin must be specified explicitly with `-` --- src/assert.bash | 16 ++++++++++++++-- test/50-assert-15-assert_output.bats | 4 ++-- test/50-assert-16-refute_output.bats | 6 +++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/assert.bash b/src/assert.bash index 1194753..3c11fb7 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -185,12 +185,14 @@ assert_failure() { assert_output() { local -i is_mode_partial=0 local -i is_mode_regexp=0 + local -i use_stdin=0 # Handle options. while (( $# > 0 )); do case "$1" in -p|--partial) is_mode_partial=1; shift ;; -e|--regexp) is_mode_regexp=1; shift ;; + -) use_stdin=1; shift ;; --) shift; break ;; *) break ;; esac @@ -205,7 +207,11 @@ assert_output() { # Arguments. local expected - (( $# == 0 )) && expected="$(cat -)" || expected="$1" + if (( use_stdin )); then + expected="$(cat -)" + else + expected="$1" + fi # Matching. if (( is_mode_regexp )); then @@ -278,12 +284,14 @@ assert_output() { refute_output() { local -i is_mode_partial=0 local -i is_mode_regexp=0 + local -i use_stdin=0 # Handle options. while (( $# > 0 )); do case "$1" in -p|--partial) is_mode_partial=1; shift ;; -e|--regexp) is_mode_regexp=1; shift ;; + -) use_stdin=1; shift ;; --) shift; break ;; *) break ;; esac @@ -298,7 +306,11 @@ refute_output() { # Arguments. local unexpected - (( $# == 0 )) && unexpected="$(cat -)" || unexpected="$1" + if (( use_stdin )); then + unexpected="$(cat -)" + else + unexpected="$1" + fi if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then echo "Invalid extended regular expression: \`$unexpected'" \ diff --git a/test/50-assert-15-assert_output.bats b/test/50-assert-15-assert_output.bats index cca79cc..7efee71 100755 --- a/test/50-assert-15-assert_output.bats +++ b/test/50-assert-15-assert_output.bats @@ -26,9 +26,9 @@ load test_helper [ "${lines[3]}" == '--' ] } -@test 'assert_output(): reads from STDIN' { +@test 'assert_output() - : reads from STDIN' { run echo 'a' - run assert_output < from STDIN' { - run echo 'a' - run refute_output < from STDIN' { + run echo '-' + run refute_output - < Date: Thu, 16 Jun 2016 15:49:13 -0400 Subject: [PATCH 2/9] drop early return --- src/assert.bash | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/assert.bash b/src/assert.bash index 3c11fb7..5dba42c 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -219,9 +219,7 @@ assert_output() { echo "Invalid extended regular expression: \`$expected'" \ | batslib_decorate 'ERROR: assert_output' \ | fail - return $? - fi - if ! [[ $output =~ $expected ]]; then + elif ! [[ $output =~ $expected ]]; then batslib_print_kv_single_or_multi 6 \ 'regexp' "$expected" \ 'output' "$output" \ From 0fb9d2aff2bc3d009f650dbc4c3fa200e8438758 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Sat, 18 Jun 2016 08:39:43 -0400 Subject: [PATCH 3/9] docs --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2e60e92..6eee99c 100644 --- a/README.md +++ b/README.md @@ -189,12 +189,13 @@ By default, literal matching is performed. The assertion fails if } ``` -The expected output can be specified with a heredoc or standard input as well. +The expected output can be specified with a heredoc or standard input as well, +by providing `-` as an option. ```bash @test 'assert_output() with pipe' { run echo 'have' - echo 'want' | assert_output + echo 'want' | assert_output - } ``` @@ -287,12 +288,13 @@ By default, literal matching is performed. The assertion fails if } ``` --The unexpected output can be specified with a heredoc or standard input as well. +The unexpected output can be specified with a heredoc or standard input as well, +by providing `-` as an option. ```bash @test 'refute_output() with pipe' { run echo 'want' - echo 'want' | refute_output + echo 'want' | refute_output - } ``` From 204d3b73b2a94c912ea0e5354b995c2623e813a1 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Thu, 16 Jun 2016 15:45:53 -0400 Subject: [PATCH 4/9] assert_output without args asserts output is non-empty --- src/assert.bash | 13 ++++++++++++- test/50-assert-15-assert_output.bats | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) 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_failure() { 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 @@ assert_output() { 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 @@ load test_helper [ "${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 from STDIN' { run echo 'a' run assert_output - < Date: Fri, 17 Jun 2016 16:03:27 -0400 Subject: [PATCH 5/9] refute_output without args asserts output is empty --- src/assert.bash | 13 ++++++++++++- test/50-assert-16-refute_output.bats | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/assert.bash b/src/assert.bash index b680fad..e1ffc4b 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -293,9 +293,14 @@ assert_output() { refute_output() { local -i is_mode_partial=0 local -i is_mode_regexp=0 + local -i is_mode_empty=0 local -i use_stdin=0 # Handle options. + if (( $# == 0 )); then + is_mode_empty=1 + fi + while (( $# > 0 )); do case "$1" in -p|--partial) is_mode_partial=1; shift ;; @@ -329,7 +334,13 @@ refute_output() { fi # Matching. - if (( is_mode_regexp )); then + if (( is_mode_empty )); then + if [ -n "$output" ]; then + echo 'expected no output, but output was non-empty' \ + | batslib_decorate 'unexpected output' \ + | fail + fi + elif (( is_mode_regexp )); then if [[ $output =~ $unexpected ]] || (( $? == 0 )); then batslib_print_kv_single_or_multi 6 \ 'regexp' "$unexpected" \ diff --git a/test/50-assert-16-refute_output.bats b/test/50-assert-16-refute_output.bats index a289717..7983750 100755 --- a/test/50-assert-16-refute_output.bats +++ b/test/50-assert-16-refute_output.bats @@ -25,6 +25,25 @@ load test_helper [ "${lines[2]}" == '--' ] } +@test 'refute_output(): succeeds if output is empty' { + run echo '' + run refute_output +echo "$output" + [ "$status" -eq 0 ] + [ "${#lines[@]}" -eq 0 ] +} + +@test 'refute_output(): fails if output is non-empty' { + run echo 'a' + run refute_output +echo "$output" + [ "$status" -eq 1 ] + [ "${#lines[@]}" -eq 3 ] + [ "${lines[0]}" == '-- unexpected output --' ] + [ "${lines[1]}" == 'expected no output, but output was non-empty' ] + [ "${lines[2]}" == '--' ] +} + @test 'refute_output() - : reads from STDIN' { run echo '-' run refute_output - < Date: Sat, 18 Jun 2016 11:45:55 -0400 Subject: [PATCH 6/9] docs --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index 6eee99c..fdec491 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,26 @@ actual : have If either value is longer than one line both are displayed in *multi-line* format. +#### Existence + +To assert that any (non-empty) output exists at all, simply omit the matching +argument. + +```bash +@test 'assert_output()' { + run echo 'have' + assert_output +} +``` + +On failure, an error message is displayed. + +``` +-- no output -- +expected non-empty output, but output was empty +-- +``` + #### Partial matching Partial matching can be enabled with the `--partial` option (`-p` for @@ -309,6 +329,25 @@ output : want If output is longer than one line it is displayed in *multi-line* format. +#### Existence + +To assert that there is no output at all, simply omit the matching argument. + +```bash +@test 'refute_output()' { + run foo --silent + refute_output +} +``` + +On failure, an error message is displayed. + +``` +-- unexpected output -- +expected no output, but output was non-empty +-- +``` + #### Partial matching Partial matching can be enabled with the `--partial` option (`-p` for From 473890bbfa3899a2215b1dd8f30c3aa01a9c6607 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Mon, 26 Sep 2016 09:48:18 -0400 Subject: [PATCH 7/9] indentation --- src/assert.bash | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/assert.bash b/src/assert.bash index e1ffc4b..a52454a 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -222,8 +222,8 @@ assert_output() { if (( is_mode_nonempty )); then if [ -z "$output" ]; then echo 'expected non-empty output, but output was empty' \ - | batslib_decorate 'no output' \ - | fail + | batslib_decorate 'no output' \ + | fail fi elif (( is_mode_regexp )); then if [[ '' =~ $expected ]] || (( $? == 2 )); then @@ -337,8 +337,8 @@ refute_output() { if (( is_mode_empty )); then if [ -n "$output" ]; then echo 'expected no output, but output was non-empty' \ - | batslib_decorate 'unexpected output' \ - | fail + | batslib_decorate 'unexpected output' \ + | fail fi elif (( is_mode_regexp )); then if [[ $output =~ $unexpected ]] || (( $? == 0 )); then From 2300d7b70f48cf90abe4aea2437068527dd99645 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Mon, 26 Sep 2016 09:48:37 -0400 Subject: [PATCH 8/9] remove debugging echos --- test/50-assert-15-assert_output.bats | 3 --- test/50-assert-16-refute_output.bats | 2 -- 2 files changed, 5 deletions(-) diff --git a/test/50-assert-15-assert_output.bats b/test/50-assert-15-assert_output.bats index 16ced75..88f1278 100755 --- a/test/50-assert-15-assert_output.bats +++ b/test/50-assert-15-assert_output.bats @@ -29,7 +29,6 @@ load test_helper @test 'assert_output(): succeeds if output is non-empty' { run echo 'a' run assert_output -echo "$output" [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @@ -37,7 +36,6 @@ echo "$output" @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 --' ] @@ -50,7 +48,6 @@ echo "$output" run assert_output - < Date: Mon, 26 Sep 2016 09:58:10 -0400 Subject: [PATCH 9/9] Improve failure message --- src/assert.bash | 5 +++-- test/50-assert-16-refute_output.bats | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/assert.bash b/src/assert.bash index a52454a..178e299 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -336,8 +336,9 @@ refute_output() { # Matching. if (( is_mode_empty )); then if [ -n "$output" ]; then - echo 'expected no output, but output was non-empty' \ - | batslib_decorate 'unexpected output' \ + batslib_print_kv_single_or_multi 6 \ + 'output' "$output" \ + | batslib_decorate 'output non-empty, but expected no output' \ | fail fi elif (( is_mode_regexp )); then diff --git a/test/50-assert-16-refute_output.bats b/test/50-assert-16-refute_output.bats index 10c8fc4..35e6fa9 100755 --- a/test/50-assert-16-refute_output.bats +++ b/test/50-assert-16-refute_output.bats @@ -37,8 +37,8 @@ load test_helper run refute_output [ "$status" -eq 1 ] [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- unexpected output --' ] - [ "${lines[1]}" == 'expected no output, but output was non-empty' ] + [ "${lines[0]}" == '-- output non-empty, but expected no output --' ] + [ "${lines[1]}" == 'output : a' ] [ "${lines[2]}" == '--' ] }