-
Couldn't load subscription status.
- Fork 450
Fix multiple error trap firing weirdness (Bash 4.2) #110
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
Fix multiple error trap firing weirdness (Bash 4.2) #110
Conversation
On Bash 4.2, when bats_error_trap fires multiple times, a non-zero return code gets "lost", causing the "failure with significant status" test to fail. This correction allows that test to pass by only having the error trap run inside the teardown trap if BATS_ERROR_STATUS is not set. Tested on Bash 4.2.46(2)-release and 4.3.33(1)-release.
|
The build failure has to be OSX specific, because I compiled and tested 3.2.57(1)-release on Ubuntu and could not reproduce the failure. |
|
Weird. 3.2.57(1)-release on OSX in Travis is now failing with the same errors I saw on 4.2.48(1)-release on Amazon Linux. I'll look at this more tomorrow, when I've had a bit more sleep. |
|
So I have some news, some bad news, and some good news. 😉 If the explanation of the bad news is too boring, you can skip straight to the good news at the very end. The news is, you can use The bad news is, you're encountering the phenomenon that led me to abandon the pursuit of an alternative to #24 for detecting $ if [[ 'foo' == 'foo' ]]; then printf 'TRUE: %d\n' "$?"; else printf 'FALSE: %d\n' "$?"; fi
TRUE: 0
$ if [[ 'foo' == 'bar' ]]; then printf 'TRUE: %d\n' "$?"; else printf 'FALSE: %d\n' "$?"; fi
FALSE: 1In other words, while conditional expression failures within an This screwed up my idea that The good news is I've reproduced the problem locally, and the solution is trivial. First, revert to the original code. First, add BATS_TEST_COMPLETED=""
BATS_TEARDOWN_COMPLETED=""
BATS_ERROR_STATUS=Then update the following line in BATS_ERROR_STATUS="$status"to: BATS_ERROR_STATUS="${BATS_ERROR_STATUS:-$status}"Setting Extra notes explaining why we shouldn't short-circuit on BATS_ERROR_STATUSBefore posting this, I also tried updating the conditional in if [[ -z "$BATS_TEST_COMPLETED" ]]; thento: if [[ -z "$BATS_TEST_COMPLETED" && -z "$BATS_ERROR_STATUS" ]]; thenBy itself this caused "referencing unset parameter in setup produces error output" and "referencing unset parameter in test produces error output" to fail, because All the Bats tests passed, but then when I ran my mbland/go-script-bash tests, a bunch of assertions failed. Short version: My assertion library was broken, because the The back story on the assertion library: I figured out how to clean up The bottom line is, there's a real use case for enabling |
|
Thanks for the detailed write-up! That's definitely what I was trying for, but I was doing it on fumes after my kid had a really rough meltdown from lack of sleep. And thanks for the follow-up regarding the need for multiple firings of the error trap. I don't have enough test suites locally to have encountered that (none of mine goes deep enough to have assertions that rely on other assertions). The changes you suggested do indeed resolve the issue on Amazon Linux. I've been giving some thought to how to test multiple Bash versions, and will see about putting something together for improved test coverage in the near future. Also, I'm going to try setting up Travis and AppVeyor to work on my fork of the repo so I can stop cluttering the project's commit history with my monkeying around. |
See bats-core#110 for an in-depth explanation on the rationale for these changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for this, and for the great commit message in the final commit!
Also, the Travis build clearly passed when you click on the icon; somehow the hook didn't fire to register success with GitHub. Oh well. Merging anyway.
|
Before, I merge, one last comment: I just ran the current test suite again under 4.1.17, 4.2, 4.2.46, 4.2.53, and 4.3. Only the 4.2 versions triggered the "failure with significant status" failure. I also instrumented the Bats code to observe the value of It's not obvious to me from https://tiswww.case.edu/php/chet/bash/CHANGES or from diffing the sources of 4.2.53 and 4.3 what might've been the root cause and fix. Were the bug fixed at 4.2.53 or earlier, it'd be easy to narrow down the patch file from https://ftp.gnu.org/gnu/bash/. Sadly, there are no individual patch files between the last patch level and the next minor version, and neither does https://git.savannah.gnu.org/cgit/bash.git appear to show the changes, either. Just wanted that on the record first. |
The previous implementation had two bugs: - Recursive function calls wouldn't appear in the trace output as they should've. - The `$?` check by itself meant that the first line of an `else` clause wouldn't get printed. For more discussion of this phenemonon: #110 (comment) The new test case reproduced both of these bugs, and the new implementation fixes them. However, there is a slight but measurable performance degradation with this update. I'm going to see if I can't tweak it some more before merging this PR.
Bats 1.1.0 - 2018-07-08 This is the first release with new features relative to the original Bats 0.4.0. Added: * The `-r, --recursive` flag to scan directory arguments recursively for `*.bats` files (bats-core#109) * The `contrib/rpm/bats.spec` file to build RPMs (bats-core#111) Changed: * Travis exercises latest versions of Bash from 3.2 through 4.4 (bats-core#116, bats-core#117) * Error output highlights invalid command line options (bats-core#45, bats-core#46, bats-core#118) * Replaced `echo` with `printf` (bats-core#120) Fixed: * Fixed `BATS_ERROR_STATUS` getting lost when `bats_error_trap` fired multiple times under Bash 4.2.x (bats-core#110) * Updated `bin/bats` symlink resolution, handling the case on CentOS where `/bin` is a symlink to `/usr/bin` (bats-core#113, bats-core#115)
On Bash 4.2, when bats_error_trap fires multiple times, a non-zero
return code gets "lost", causing the "failure with significant status"
test to fail. This correction allows that test to pass by only having
the error trap run inside the teardown trap if BATS_ERROR_STATUS is not
set.
Tested on Bash 4.2.46(2)-release and 4.3.33(1)-release.