Skip to content
New issue

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

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

Already on GitHub? # to your account

Reporting problem failures related to Bash option -e #61

Closed
dodie opened this issue May 15, 2020 · 2 comments
Closed

Reporting problem failures related to Bash option -e #61

dodie opened this issue May 15, 2020 · 2 comments

Comments

@dodie
Copy link

dodie commented May 15, 2020

Hi!

I noticed a small glitch when I use set -e in my code. For example:

#!/usr/bin/env bash

set -euo pipefail

function hello() {
	echo "something" | grep "else" # this will exit at this point, because this line finishes with a non-zero exit status
	echo "hello"
}

# Make sure that I can use source in the test
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
	hello
fi

I have the following test:

function test_1() {
	...
}
function test_fail_due_to_e() {
	source hello.sh
	result=$(hello)

	assert_equals 0 $?
	assert_equals "hello" "${result}"
}
function test_3() {
	...
}

Executing this tests fails with an error code, which is expected, but the report is a bit scrambled as there's no status indicator for the test that fails due to -e:

Running test_1... SUCCESS
Running test_fail_due_to_e... Running test_3... SUCCESS

Also, if I extract the sourcing from the test_fail_due_to_e function, like:

source hello.sh

function test_1() {
	...
}
function test_fail_due_to_e() {
	result=$(hello)
	assert_equals 0 $?
	assert_equals "hello" "${result}"
}
function test_3() {
	...
}

Then the whole test file will be missing from the report.

Would it be possible to include such failures in the report?

Thanks,
David

@pgrange
Copy link
Owner

pgrange commented Mar 23, 2021

Hi @dodie,
Sorry for the insane delay here.

@pgrange
Copy link
Owner

pgrange commented Mar 23, 2021

I'm trying to reproduce your first case but I can't see the same result as you. I've create hello.sh and test_hello with the following code inside and you can see what is the output of bash_unit for me.

#> cat hello.sh 
#!/usr/bin/env bash

set -euo pipefail

function hello() {
	echo "something" | grep "else" # this will exit at this point, because this line finishes with a non-zero exit status
	echo "hello"
}

# Make sure that I can use source in the test
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
	hello
fi
#> cat test_hello 
function test_fail_due_to_e() {
	source hello.sh
	result=$(hello)

	assert_equals 0 $?
	assert_equals "hello" "${result}"
}
#> ./bash_unit test_hello 
Running tests in test_hello
	Running test_fail_due_to_e... SUCCESS

Could you reproduce this and confirm you see the same thing as me?
Could you correct my code above so that I reproduce the same issue as yours?

@pgrange pgrange closed this as completed Jul 30, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants