|
| 1 | +#! /usr/bin/env bash |
| 2 | +# |
| 3 | +# Utilities for writing helper functions for Bats tests |
| 4 | + |
| 5 | +# The first line of every Bats helper function must call `set` with this |
| 6 | +# argument. See the function comments for `restore_bats_shell_options`. |
| 7 | +export DISABLE_BATS_SHELL_OPTIONS='+eET' |
| 8 | + |
| 9 | +# Restore shell options disabled with `set "$DISABLE_BATS_SHELL_OPTIONS"`. |
| 10 | +# |
| 11 | +# Ensures a Bats helper function failure points to its call, not its internals. |
| 12 | +# This is critical for writing Bats assertion functions. |
| 13 | +# |
| 14 | +# You must ensure that `set "$DISABLE_BATS_SHELL_OPTIONS"` is in effect prior to |
| 15 | +# calling this function, and that your Bats helper function calls this function |
| 16 | +# directly through every return path (i.e. you can't delegate the call to |
| 17 | +# another helper function). See the comments at the top of `lib/bats/assertions` |
| 18 | +# for usage instructions and patterns. |
| 19 | +# |
| 20 | +# Notice that each public assertion starts with: |
| 21 | +# |
| 22 | +# set "$DISABLE_BATS_SHELL_OPTIONS"` |
| 23 | +# |
| 24 | +# and that `DISABLE_BATS_SHELL_OPTIONS` is set to `+eET`. Bats uses `set -e`, |
| 25 | +# `set -E`, and `set -T` (in `tests/bats/libexec/bats-exec-test`) to make sure |
| 26 | +# failing commands are pinpointed and their stack traces are collected. This is |
| 27 | +# almost always the desired behavior. |
| 28 | +# |
| 29 | +# When it comes to test assertions, however, we want the stack trace to point to |
| 30 | +# the assertion call itself, not the line within its implementation at which a |
| 31 | +# condition triggered the failure. Otherwise, it produces a bit of mental strain |
| 32 | +# when reviewing test failures to identify the location of the failing assertion |
| 33 | +# in the test case itself. |
| 34 | +# |
| 35 | +# Starting an assertion with `set "$DISABLE_BATS_SHELL_OPTIONS"` (i.e. `set |
| 36 | +# +eET`) disables the `set -e`, `set -E`, and `set -T` shell options, which |
| 37 | +# prevents the functions and commands it calls from updating the Bats stack |
| 38 | +# traces. However, by itself, this still leaves the function, file, and line |
| 39 | +# number where the assertion was defined in the Bats stack traces. It's also |
| 40 | +# important to reinstate `set -eET` upon returning, but we want to make it easy |
| 41 | +# to write new assertions composed from existing assertions by reinstating these |
| 42 | +# options only when returning from the outermost assertion. |
| 43 | +# |
| 44 | +# This function solves both aspects of the problem by removing the immediate |
| 45 | +# caller from the Bats stack traces and reinstating `set -eET` if it is the |
| 46 | +# outermost assertion function, which will be the only one pushed onto the Bats |
| 47 | +# stacks prior to calling `set "$DISABLE_BATS_SHELL_OPTIONS"`. |
| 48 | +# |
| 49 | +# Arguments: |
| 50 | +# result: Return value of the calling assertion; defaults to 0 |
| 51 | +restore_bats_shell_options() { |
| 52 | + local result="${1:-0}" |
| 53 | + local target_stack_item_pattern=" ${FUNCNAME[1]} ${BASH_SOURCE[1]}$" |
| 54 | + |
| 55 | + # After removing our caller from BATS_CURRENT_STACK_TRACE and restoring the |
| 56 | + # Bats shell options, the `return` call at the end of the function will fire |
| 57 | + # `bats_debug_trap`, which sets BATS_CURRENT_STACK_TRACE to |
| 58 | + # BATS_PREVIOUS_STACK_TRACE. |
| 59 | + # |
| 60 | + # If `result` is nonzero, `bats_error_trap` will fire and set |
| 61 | + # BATS_ERROR_STACK_TRACE to BATS_PREVIOUS_STACK_TRACE and fail the test. |
| 62 | + if [[ "${BATS_CURRENT_STACK_TRACE[0]}" =~ $target_stack_item_pattern ]]; then |
| 63 | + unset 'BATS_CURRENT_STACK_TRACE[0]' |
| 64 | + set -eET |
| 65 | + fi |
| 66 | + return "$result" |
| 67 | +} |
| 68 | +export -f restore_bats_shell_options |
0 commit comments