From 1a758415cc546fd1794030d88b87a20f493df8fc Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Thu, 16 Jun 2016 14:48:33 -0400 Subject: [PATCH 1/7] 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/7] 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/7] 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 ae447cd8de08f58c38370522d8b111e5f8049845 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Tue, 8 Nov 2016 16:40:21 -0500 Subject: [PATCH 4/7] Update code comments --- src/assert.bash | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/assert.bash b/src/assert.bash index 5dba42c..202a8f1 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -172,8 +172,9 @@ assert_failure() { # Options: # -p, --partial - partial matching # -e, --regexp - extended regular expression matching +# - - read expected output from the standard input # Arguments: -# $1 - [=STDIN] expected output +# $1 - expected output # Returns: # 0 - expected matches the actual output # 1 - otherwise @@ -269,8 +270,9 @@ assert_output() { # Options: # -p, --partial - partial matching # -e, --regexp - extended regular expression matching +# - - read unexpected output from the standard input # Arguments: -# $1 - [=STDIN] unexpected output +# $1 - unexpected output # Returns: # 0 - unexpected matches the actual output # 1 - otherwise From 0dc0b49165b9ad57a7327ad043c97f73f4d219ea Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Tue, 8 Nov 2016 16:50:56 -0500 Subject: [PATCH 5/7] Allow longform --stdin option as well --- src/assert.bash | 8 ++++---- test/50-assert-15-assert_output.bats | 10 ++++++++++ test/50-assert-16-refute_output.bats | 9 +++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/assert.bash b/src/assert.bash index 202a8f1..1aac74d 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -172,7 +172,7 @@ assert_failure() { # Options: # -p, --partial - partial matching # -e, --regexp - extended regular expression matching -# - - read expected output from the standard input +# -, --stdin - read expected output from the standard input # Arguments: # $1 - expected output # Returns: @@ -193,7 +193,7 @@ assert_output() { case "$1" in -p|--partial) is_mode_partial=1; shift ;; -e|--regexp) is_mode_regexp=1; shift ;; - -) use_stdin=1; shift ;; + -|--stdin) use_stdin=1; shift ;; --) shift; break ;; *) break ;; esac @@ -270,7 +270,7 @@ assert_output() { # Options: # -p, --partial - partial matching # -e, --regexp - extended regular expression matching -# - - read unexpected output from the standard input +# -, --stdin - read unexpected output from the standard input # Arguments: # $1 - unexpected output # Returns: @@ -291,7 +291,7 @@ refute_output() { case "$1" in -p|--partial) is_mode_partial=1; shift ;; -e|--regexp) is_mode_regexp=1; shift ;; - -) use_stdin=1; shift ;; + -|--stdin) use_stdin=1; shift ;; --) shift; break ;; *) break ;; esac diff --git a/test/50-assert-15-assert_output.bats b/test/50-assert-15-assert_output.bats index 7efee71..0da7d04 100755 --- a/test/50-assert-15-assert_output.bats +++ b/test/50-assert-15-assert_output.bats @@ -36,6 +36,16 @@ echo "$output" [ "${#lines[@]}" -eq 0 ] } +@test 'assert_output() --stdin : reads from STDIN' { + run echo 'a' + run assert_output --stdin <: displays details in multi-line format if \`\$output' is longer than one line" { run printf 'b 0\nb 1' diff --git a/test/50-assert-16-refute_output.bats b/test/50-assert-16-refute_output.bats index a289717..05f2995 100755 --- a/test/50-assert-16-refute_output.bats +++ b/test/50-assert-16-refute_output.bats @@ -34,6 +34,15 @@ INPUT [ "${#lines[@]}" -eq 0 ] } +@test 'refute_output() --stdin : reads from STDIN' { + run echo '--stdin' + run refute_output --stdin <: displays details in multi-line format if necessary' { run printf 'a 0\na 1' From 581aa706ad3f7d1c4ed57113acdb82b7190ad1b3 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Tue, 22 Nov 2016 16:23:46 -0500 Subject: [PATCH 6/7] Document stdin option in readme --- README.md | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 6eee99c..4842e5b 100644 --- a/README.md +++ b/README.md @@ -189,16 +189,6 @@ By default, literal matching is performed. The assertion fails if } ``` -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 - -} -``` - On failure, the expected and actual output are displayed. ``` @@ -267,6 +257,23 @@ invalid. This option and partial matching (`--partial` or `-p`) are mutually exclusive. An error is displayed when used simultaneously. +#### Standard Input, HereDocs and HereStrings + +The expected output can be specified via standard input (also +heredoc/herestring) with the `-`/`--stdin` option. + +```bash +@test 'assert_output() with pipe' { + run echo 'hello' + echo 'hello' | assert_output - +} + +@test 'assert_output() with herestring' { + run echo 'hello' + assert_output - <<< hello +} +``` + ### `refute_output` @@ -288,16 +295,6 @@ By default, literal matching is performed. The assertion fails if } ``` -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 - -} -``` - On failure, the output is displayed. ``` @@ -365,6 +362,23 @@ invalid. This option and partial matching (`--partial` or `-p`) are mutually exclusive. An error is displayed when used simultaneously. +#### Standard Input, HereDocs and HereStrings + +The unexpected output can be specified via standard input (also +heredoc/herestring) with the `-`/`--stdin` option. + +```bash +@test 'refute_output() with pipe' { + run echo 'hello' + echo 'world' | refute_output - +} + +@test 'refute_output() with herestring' { + run echo 'hello' + refute_output - <<< world +} +``` + ### `assert_line` From adc1c7bacf66f7af8c201402fb1de69ab79cc4ae Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Tue, 22 Nov 2016 16:27:27 -0500 Subject: [PATCH 7/7] Remove leftover debugging lines --- test/50-assert-15-assert_output.bats | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/50-assert-15-assert_output.bats b/test/50-assert-15-assert_output.bats index 0da7d04..90c2056 100755 --- a/test/50-assert-15-assert_output.bats +++ b/test/50-assert-15-assert_output.bats @@ -31,7 +31,6 @@ load test_helper run assert_output - <