From 77311c265f366661c032352ee90f512e34b782d7 Mon Sep 17 00:00:00 2001 From: Justin Ledford Date: Mon, 8 Jan 2024 13:04:35 -0800 Subject: [PATCH 1/3] add option to show diffs in assert_output and assert_equal --- src/assert_equal.bash | 28 +++++++++++++++++++++++----- src/assert_output.bash | 21 ++++++++++++++++----- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/assert_equal.bash b/src/assert_equal.bash index 4ef1297..74f1ca0 100644 --- a/src/assert_equal.bash +++ b/src/assert_equal.bash @@ -8,6 +8,7 @@ # Options: # The value being compared. # The value to compare against. +# -d, --diff Show diff between `expected` and `$output` # # ```bash # @test 'assert_equal()' { @@ -31,12 +32,29 @@ # actual : have # -- # ``` +# +# If the `--diff` option is set, a diff between the expected and actual output is shown. assert_equal() { + local -i show_diff=0 + + while (( $# > 0 )); do + case "$1" in + -d|--diff) show_diff=1; shift ;; + *) break ;; + esac + done + if [[ $1 != "$2" ]]; then - batslib_print_kv_single_or_multi 8 \ - 'expected' "$2" \ - 'actual' "$1" \ - | batslib_decorate 'values do not equal' \ - | fail + if (( show_diff )); then + diff <(echo "$2") <(echo "$1") \ + | batslib_decorate 'values do not equal' \ + | fail + else + batslib_print_kv_single_or_multi 8 \ + 'expected' "$2" \ + 'actual' "$1" \ + | batslib_decorate 'values do not equal' \ + | fail + fi fi } diff --git a/src/assert_output.bash b/src/assert_output.bash index 82d0a0c..15e45cf 100644 --- a/src/assert_output.bash +++ b/src/assert_output.bash @@ -8,6 +8,7 @@ # Options: # -p, --partial Match if `expected` is a substring of `$output` # -e, --regexp Treat `expected` as an extended regular expression +# -d, --diff Show diff between `expected` and `$output` # -, --stdin Read `expected` value from STDIN # The expected value, substring or regular expression # @@ -57,6 +58,8 @@ # -- # ``` # +# If the `--diff` option is set, a diff between the expected and actual output is shown. +# # ## Existence # # To assert that any output exists at all, omit the `expected` argument. @@ -126,6 +129,7 @@ assert_output() { local -i is_mode_regexp=0 local -i is_mode_nonempty=0 local -i use_stdin=0 + local -i show_diff=0 : "${output?}" # Handle options. @@ -137,6 +141,7 @@ assert_output() { case "$1" in -p|--partial) is_mode_partial=1; shift ;; -e|--regexp) is_mode_regexp=1; shift ;; + -d|--diff) show_diff=1; shift ;; -|--stdin) use_stdin=1; shift ;; --) shift; break ;; *) break ;; @@ -187,11 +192,17 @@ assert_output() { fi else if [[ $output != "$expected" ]]; then - batslib_print_kv_single_or_multi 8 \ - 'expected' "$expected" \ - 'actual' "$output" \ - | batslib_decorate 'output differs' \ - | fail + if (( show_diff )); then + diff <(echo "$expected") <(echo "$output") \ + | batslib_decorate 'output differs' \ + | fail + else + batslib_print_kv_single_or_multi 8 \ + 'expected' "$expected" \ + 'actual' "$output" \ + | batslib_decorate 'output differs' \ + | fail + fi fi fi } From 0db2e8b338d33483d1320e44d50215cd68b1a1b2 Mon Sep 17 00:00:00 2001 From: Justin Ledford Date: Tue, 4 Jun 2024 18:40:45 -0700 Subject: [PATCH 2/3] check number of parameters with diff and use diff unified format --- src/assert_equal.bash | 6 +++--- src/assert_output.bash | 28 ++++++++++++++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/assert_equal.bash b/src/assert_equal.bash index 74f1ca0..c6e5cc0 100644 --- a/src/assert_equal.bash +++ b/src/assert_equal.bash @@ -3,7 +3,7 @@ # # Summary: Fail if the actual and expected values are not equal. # -# Usage: assert_equal +# Usage: assert_equal [-d] # # Options: # The value being compared. @@ -39,14 +39,14 @@ assert_equal() { while (( $# > 0 )); do case "$1" in - -d|--diff) show_diff=1; shift ;; + -d|--diff) show_diff=$(( $# > 1 )); shift ;; *) break ;; esac done if [[ $1 != "$2" ]]; then if (( show_diff )); then - diff <(echo "$2") <(echo "$1") \ + diff -u <(echo "$1") <(echo "$2") | tail -n +3 \ | batslib_decorate 'values do not equal' \ | fail else diff --git a/src/assert_output.bash b/src/assert_output.bash index 15e45cf..8e9739c 100644 --- a/src/assert_output.bash +++ b/src/assert_output.bash @@ -3,7 +3,7 @@ # # Summary: Fail if `$output' does not match the expected output. # -# Usage: assert_output [-p | -e] [- | [--] ] +# Usage: assert_output [-p | -e | -d] [- | [--] ] # # Options: # -p, --partial Match if `expected` is a substring of `$output` @@ -190,19 +190,23 @@ assert_output() { | batslib_decorate 'output does not contain substring' \ | fail fi + elif (( show_diff )); then + if (( $# == 0 )); then + echo "Missing expected output" \ + | batslib_decorate 'ERROR: assert_output' \ + | fail + elif [[ $output != "$expected" ]]; then + diff -u <(echo "$output") <(echo "$expected") | tail -n +3 \ + | batslib_decorate 'output differs' \ + | fail + fi else if [[ $output != "$expected" ]]; then - if (( show_diff )); then - diff <(echo "$expected") <(echo "$output") \ - | batslib_decorate 'output differs' \ - | fail - else - batslib_print_kv_single_or_multi 8 \ - 'expected' "$expected" \ - 'actual' "$output" \ - | batslib_decorate 'output differs' \ - | fail - fi + batslib_print_kv_single_or_multi 8 \ + 'expected' "$expected" \ + 'actual' "$output" \ + | batslib_decorate 'output differs' \ + | fail fi fi } From 3eee62f2bef769c055a7a2658dc6fe500769b111 Mon Sep 17 00:00:00 2001 From: Justin Ledford Date: Tue, 4 Jun 2024 18:41:06 -0700 Subject: [PATCH 3/3] add tests for diff option --- test/assert_equal.bats | 18 ++++++++++++++++++ test/assert_output.bats | 21 ++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/test/assert_equal.bats b/test/assert_equal.bats index 7d3fc86..f4ab3b5 100755 --- a/test/assert_equal.bats +++ b/test/assert_equal.bats @@ -60,3 +60,21 @@ actual : a -- ERR_MSG } + +@test 'assert_equal() -d : returns 0 if equals ' { + run assert_equal -d 'a' 'a' + assert_test_pass +} + +@test 'assert_equal() -d : returns 1 and displays details if does not equal ' { + run assert_equal -d 'a' 'b' + + assert_test_fail <<'ERR_MSG' + +-- values do not equal -- +@@ -1 +1 @@ +-a ++b +-- +ERR_MSG +} diff --git a/test/assert_output.bats b/test/assert_output.bats index e9609e0..8c79342 100755 --- a/test/assert_output.bats +++ b/test/assert_output.bats @@ -13,6 +13,12 @@ load test_helper assert_test_pass } +@test "assert_output() -d : returns 0 if equals \`\$output'" { + run echo 'a' + run assert_output -d 'a' + assert_test_pass +} + @test "assert_output() : returns 1 and displays details if does not equal \`\$output'" { run echo 'b' run assert_output 'a' @@ -26,6 +32,20 @@ actual : b ERR_MSG } +@test "assert_output() -d : returns 1 and displays details if does not equal \`\$output'" { + run echo 'b' + run assert_output -d 'a' + + assert_test_fail <<'ERR_MSG' + +-- output differs -- +@@ -1 +1 @@ +-b ++a +-- +ERR_MSG +} + @test 'assert_output(): succeeds if output is non-empty' { run echo 'a' run assert_output @@ -262,7 +282,6 @@ Invalid extended regular expression: `[.*' ERR_MSG } - # # Common #