Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: assert_stderr #22

Open
landure opened this issue Nov 1, 2022 · 2 comments
Open

feature request: assert_stderr #22

landure opened this issue Nov 1, 2022 · 2 comments

Comments

@landure
Copy link

landure commented Nov 1, 2022

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.

@adamflorizone
Copy link

adamflorizone commented Jan 29, 2024

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.

@landure
Copy link
Author

landure commented Jan 30, 2024

Thank you for the precision

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants