You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello @lczyk and thank you for opening this issue. I must admit that I'm struggling to figure out what's really happening with this set -e and what the impact, as is, or when changed.
I'm trying to find a test we can add to test_core.sh to exhibit the exact behavior we want.
yeah, the set -e behavior is weird. have a look at the following example:
#!/bin/bash# +e is the default, but let's be unambiguousset +e
(
echo"subshell 1"set -e
falseecho"subshell 1 should not reach this point"
)
[ $?-eq 0 ] ||echo"subshell 1 failed"
(
echo"subshell 2"set -e
falseecho"subshell 2 should not reach this point"
) ||echo"subshell 2 failed"functionf() {
echo"function $1"set -e
falseecho"function $1 should not reach this point"
}
f 1
[ $?-eq 0 ] ||echo"function 1 failed"
f 2 ||echo"function 2 failed"
which prints
subshell 1
subshell 1 failed
subshell 2
subshell 2 should not reach this point
function 1
For explanation, see this post on unix stackexchange. tldr; subshell 2 is the unintuitive one. from man bash: "The shell does not exit if the command that fails is... part of any command executed in a && or || list". this includes subshells. whereas function 1 just runs in top-level scope and so it exits that immediately after its set -e
so my proposed patches just treat that case a bit more carefully, swapping some ||'s for local status=$?'s, and then using trap ... EXIT to ensure the same behavior around teardown.
posting things here as diffs for now, but if you see my case and agree with the general direction of changes i'm more than happy to just make it a PR. i just wanted to make sure that the behavior that i'm expecting from the tests (aka for the test_exit_code_not_0_in_case_of_non_zero_exit_code_followed_by_zero_exit_code above to fail) is, in fact, the behavior which bash_unit ought to have. if so, i think my patches are warranted
Activity
lczyk commentedon Aug 22, 2025
i believe something like the following might be closer to what was intended
will use a version with this patch for a while and test
EDIT: not quite. will tinker and report backEDIT: a couple more changes and now it works.
pgrange commentedon Aug 29, 2025
Hello @lczyk and thank you for opening this issue. I must admit that I'm struggling to figure out what's really happening with this
set -eand what the impact, as is, or when changed.I'm trying to find a test we can add to
test_core.shto exhibit the exact behavior we want.pgrange commentedon Aug 29, 2025
Related to this issue, I've noticed that, when setup_suite fails, bash_unit terminates in success and that's not cool.
So I fixed that simple case and added a test for that.
lczyk commentedon Sep 1, 2025
yeah, the
set -ebehavior is weird. have a look at the following example:which prints
For explanation, see this post on unix stackexchange. tldr; subshell 2 is the unintuitive one. from man bash: "The shell does not exit if the command that fails is... part of any command executed in a && or || list". this includes subshells. whereas function 1 just runs in top-level scope and so it exits that immediately after its set -e
lczyk commentedon Sep 1, 2025
so my proposed patches just treat that case a bit more carefully, swapping some
||'s forlocal status=$?'s, and then usingtrap ... EXITto ensure the same behavior around teardown.lczyk commentedon Sep 1, 2025
these are the tests i've added to check for this:
specifically the, artfully named,
test_exit_code_not_0_in_case_of_non_zero_exit_code_followed_by_zero_exit_codelczyk commentedon Sep 1, 2025
posting things here as diffs for now, but if you see my case and agree with the general direction of changes i'm more than happy to just make it a PR. i just wanted to make sure that the behavior that i'm expecting from the tests (aka for the
test_exit_code_not_0_in_case_of_non_zero_exit_code_followed_by_zero_exit_codeabove to fail) is, in fact, the behavior whichbash_unitought to have. if so, i think my patches are warrantedpgrange commentedon Sep 3, 2025
Sure, feel free to make a PR, I definitely see your case.
lczyk commentedon Sep 13, 2025
@pgrange #138 is up 👍
pgrange commentedon Sep 28, 2025
Fixed in #138 , Thanks to @lczyk