bats-assert icon indicating copy to clipboard operation
bats-assert copied to clipboard

add option to show diff in assert_output and assert_equal

Open justinledford opened this issue 1 year ago • 3 comments

fixes #60

justinledford avatar Jan 08 '24 23:01 justinledford

Why an option? The diffing could happen automatically if diff is available.

gioele avatar May 27 '24 21:05 gioele

Is there an opportunity here instead of trying to bake this into the matcher, to instead have a form of assert_output | diff? (diff here being function we own, not the command)

Having a generic diff utility would be more composable to other existing and future matchers. And allow multiple types of formatters to coexist. (Rather than an explosion of matcher options.)

This would require having a structured format/spec the assertions can construct that "renders" can consume as an api.

jasonkarns avatar Jun 05 '24 15:06 jasonkarns

I'm just giving my 2c as a happy bats user

For TUI output, I find it way faster to reconcile the differences with a tool like github.com/dandavison/delta

So I know what you mean about colors and the meaning of red, but it would be very useful to customize the diff command in some way.

Then when it comes to comparing contents, I would welcome anything that makes it easier to write stuff like this. I am not proud but it makes tests more compact and readable. That's what you mean by matcher I suppose

# Compare ignoring the key order, and allow "expected" without quoted identifiers.
# Preserve the output variable in case the following commands require it.
assert_json() {
    local oldout="${output}"
    # validate actual, sort
    run -0 jq -Sen "${output}"
    local actual="${output}"

    # handle stdin, quote identifiers, sort
    local expected="$1"
    if [[ "${expected}" == "-" ]]; then
        expected="$(cat)"
    fi
    run -0 jq -Sn "${expected}"
    expected="${output}"

    #shellcheck disable=SC2016
    run jq -ne --argjson a "${actual}" --argjson b "${expected}" '$a == $b'
    #shellcheck disable=SC2154
    if [[ "${status}" -ne 0 ]]; then
        echo "expect: $(jq -c <<<"${expected}")"
        echo "actual: $(jq -c <<<"${actual}")"
        diff <(echo "${actual}") <(echo "${expected}")
        fail "json does not match"
    fi
    output="${oldout}"
}
export -f assert_json

mmetc avatar Jul 12 '24 08:07 mmetc