1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-11-17 11:42:33 +01:00

exec-test: Work around Bash 3.2.57 ERR trap bug

When running under Bash 3.2.57(1)-release on macOS, the following tests
would fail because `BATS_ERROR_STACK_TRACE` would be empty, and hence no
information about the actual error would get printed:

- one failing test
- failing test with significant status
- failing test file outside of BATS_CWD

This is because each of these cases use `FIXTURE_ROOT/failing.bats`, and
the `ERR` trap would not fire for its `eval "( exit ${STATUS:-1} )"`
line. Changing it to `exit ${STATUS:-1}` produced the same effect, and
changing it to `return ${STATUS:-1}` would cause the output to point to
the previous line, which executes `true`.

However, the correct status would be reported to the `EXIT` trap, so now
we call `bats_error_trap` at the very beginning of `bats_teardown_trap`.

All the existing tests now pass under Bash 3.2.57(1)-release, Bash
4.2.25(1)-release (the version from the default Ubuntu 12.04.5/Precise
image on Travis CI), and Bash 4.4.12(1)-release.
This commit is contained in:
Mike Bland 2017-02-14 23:06:59 -05:00
parent 918714dd4d
commit 0f6dde530e
No known key found for this signature in database
GPG Key ID: 5121C73A6E07384B

View File

@ -218,13 +218,22 @@ bats_debug_trap() {
fi fi
} }
# When running under Bash 3.2.57(1)-release on macOS, the `ERR` trap may not
# always fire, but the `EXIT` trap will. For this reason we call it at the very
# beginning of `bats_teardown_trap` (the `DEBUG` trap for the call will move
# `BATS_CURRENT_STACK_TRACE` to `BATS_PREVIOUS_STACK_TRACE`) and check the value
# of `$?` before taking other actions.
bats_error_trap() { bats_error_trap() {
BATS_ERROR_STATUS="$?" local status="$?"
BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" ) if [[ "$status" -ne '0' ]]; then
trap - debug BATS_ERROR_STATUS="$status"
BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" )
trap - debug
fi
} }
bats_teardown_trap() { bats_teardown_trap() {
bats_error_trap
trap "bats_exit_trap" exit trap "bats_exit_trap" exit
local status=0 local status=0
teardown >>"$BATS_OUT" 2>&1 || status="$?" teardown >>"$BATS_OUT" 2>&1 || status="$?"