Skip to content

Commit

Permalink
Evaluate each test file in own shell during gather-tests to avoid bad…
Browse files Browse the repository at this point in the history
… interactions
  • Loading branch information
martin-schulze-vireso committed May 4, 2024
1 parent f94e695 commit 0775227
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
40 changes: 26 additions & 14 deletions libexec/bats-core/bats-gather-tests
Original file line number Diff line number Diff line change
Expand Up @@ -255,31 +255,37 @@ fi
# shellcheck source=lib/bats-core/tracing.bash
source "$BATS_ROOT/lib/bats-core/tracing.bash"


BATS_OUT="$BATS_RUN_TMPDIR/bats-gather-tests.out"
bats_gather_tests_exit_trap() {
local bats_gather_tests_exit_status=$?
trap - ERR EXIT DEBUG
if [[ ${BATS_ERROR_STATUS:-0} != 0 ]]; then
bats_gather_tests_exit_status=$BATS_ERROR_STATUS
if (( bats_gather_tests_exit_status != 0)); then
printf "1..1\nnot ok 1 bats-gather-tests\n"
bats_get_failure_stack_trace stack_trace
bats_print_stack_trace "${stack_trace[@]}"
bats_print_failed_command "${stack_trace[@]}"
# play back traces from test file evaluation
bats_replace_filename < "$BATS_TRACE"
bats_prefix_lines_for_tap_output <"$BATS_OUT" | bats_replace_filename
fi >&2
exit "$bats_gather_tests_exit_status"
}

trap bats_gather_tests_exit_trap EXIT

bats_set_stacktrace_limit
# prepare tracing for errors during test file evaluation
BATS_TRACE="$BATS_RUN_TMPDIR/bats-gather-tests.trace"
touch "$BATS_TRACE"

# override test_functions.bash's version to use it for test registration
load() {
# the standard error stream and exit code will be revealed when running the tests
if ! bats_load_safe "$@" 2>/dev/null; then
return 0
bats_gather_tests_source_exit_trap() {
local bats_gather_tests_source_exit_status=$?
trap - ERR EXIT DEBUG
if (( bats_gather_tests_source_exit_status != 0)); then
bats_get_failure_stack_trace stack_trace
bats_print_stack_trace "${stack_trace[@]}"
bats_print_failed_command "${stack_trace[@]}"
fi
}
exit "$bats_gather_tests_source_exit_status"
} >>"$BATS_TRACE"

bats_setup_tracing
focus_mode=0
for filename in "$@"; do
if [[ ! -f "$filename" ]]; then
Expand All @@ -295,7 +301,12 @@ for filename in "$@"; do
BATS_TEST_NAME=source
BATS_TEST_FILTER="$BATS_TEST_FILTER" bats_preprocess_source # uses BATS_TEST_FILENAME
# shellcheck disable=SC1090
BATS_TEST_DIRNAME="${filename%/*}" source "$BATS_TEST_SOURCE"
(
trap bats_gather_tests_source_exit_trap EXIT
bats_setup_tracing
bats_set_stacktrace_limit
BATS_TEST_DIRNAME="${filename%/*}" source "$BATS_TEST_SOURCE" 1>>"$BATS_OUT" 2>&1
)

if [[ "${#test_dupes[@]}" -ne 0 ]]; then
abort 'Duplicate test name(s) in file "%s": %s' "${filename}" "${test_dupes[*]#$filename$'\t'}"
Expand All @@ -313,6 +324,7 @@ if [[ -n "$filter_status" ]]; then
fi
fi

# communicate to the caller that we are running in focus mode
if (( focus_mode )); then
printf "focus_mode\n"
fi
21 changes: 7 additions & 14 deletions test/suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,13 @@ setup() {
@test "errors when loading common helper from multiple tests in a suite" {
reentrant_run bats "$FIXTURE_ROOT/errors_in_multiple_load"
[ $status -eq 1 ]
[ "${lines[0]}" = "1..3" ]
regex="test_helper\.bash: line 1: call-to-undefined-command: command not found"
[[ "${lines[1]}" =~ $regex ]]
regex="Error while sourcing library loader at '.*test_helper\.bash'"
[[ "${lines[2]}" =~ $regex ]]
[ "${lines[3]}" = "not ok 1 setup_file failed" ]
regex="test_helper\.bash: line 1: call-to-undefined-command: command not found"
[[ "${lines[8]}" =~ $regex ]]
regex="Error while sourcing library loader at '.*test_helper\.bash'"
[[ "${lines[9]}" =~ $regex ]]
[ "${lines[10]}" = "not ok 2 setup_file failed" ]
regex="bats_load_safe: Could not find '.*nonexistent'\[\.bash\]"
[[ "${lines[15]}" =~ $regex ]]
[ "${lines[16]}" = "not ok 3 setup_file failed" ]
[ "${lines[0]}" = "1..1" ]
[ "${lines[1]}" = "not ok 1 bats-gather-tests" ]
[ "${lines[2]}" = "# (in test file test/fixtures/suite/errors_in_multiple_load/a.bats, line 1)" ]
[ "${lines[3]}" = "# \`load test_helper' failed with status 0" ]
[ "${lines[4]}" = "# $FIXTURE_ROOT/errors_in_multiple_load/test_helper.bash: line 1: call-to-undefined-command: command not found" ]
[ "${lines[5]}" = "# Error while sourcing library loader at '$FIXTURE_ROOT/errors_in_multiple_load/test_helper.bash'" ]
[ "${#lines[@]}" -eq 6 ]
}

@test "running an ad-hoc suite by specifying multiple test files" {
Expand Down

0 comments on commit 0775227

Please sign in to comment.