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

feature request: assert_stderr

Open landure opened this issue 3 years ago • 2 comments

In bats-core documentation Writing tests, it is said that:

Additionally, you can use --separate-stderr to split stdout and stderr into $output/$stderr and ${lines[@]}/${stderr_lines[@]}.

Unfortunately, bats-assert does not provide a method to test $stderr contents.

Please consider a refactor of assert_output to allow for stderr contents testing.

landure avatar Nov 01 '22 08:11 landure

This is not even a bats-assert issue...

Per: https://github.com/bats-core/bats-core/blob/990d8e285e7564ff61ea2c93a6da12856d337d83/lib/bats-core/test_functions.bash#L362C3-L362C9

It works out of the box :

    run sh -c ">&2 echo 'have'"
    assert_output "want"

Merged is default, so it works by default. There is no $stderr by default (see below). So you must use separate.

Separate Example:

    run --separate-stderr sh -c "echo 'have' 1>&2"
    output=$stderr assert_output "want"

The above code works.

The key part your looking for is: "output=$stderr assert_output ..."

Or make your own function in setup():

setup(){
    assert_stderr(){
        output=$stderr assert_output "$@"
    }
}
@test "bats-assert is not the cause" {
    run --separate-stderr sh -c ">&2 echo 'have'"
    assert_stderr "want"
}

You must modify run with "--separate-stderr" anyways.

    run sh -c ">&2 echo 'have'"
    echo "$stderr"

results in:

stderr: unbound variable

TL;DR;

You should "output=$stderr assert_output ..." if your going to "--separate-stderr" anyways. This feature request is not a good idea.

adamflorizone avatar Jan 29 '24 15:01 adamflorizone

Thank you for the precision

landure avatar Jan 30 '24 07:01 landure