Skip to content
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 load in teardown after failure exits test #612

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
directories (#603)
* also add `--clean-and-gather-test-outpust-in <directory>` for improved UX
* double slashes in paths derived from TMPDIR on MacOS (#607)
* fix `load` in `teardown` marking failed tests as not run (#612)

#### Documentation

Expand Down
13 changes: 7 additions & 6 deletions lib/bats-core/test_functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ find_in_bats_lib_path() { # <return-var> <library-name>
if [[ -f "$path/$library_name" ]]; then
printf -v "$return_var" "%s" "$path/$library_name"
# A library load path was found, return
return
return 0
elif [[ -f "$path/$library_name/load.bash" ]]; then
printf -v "$return_var" "%s" "$path/$library_name/load.bash"
# A library load path was found, return
return
return 0
fi
done

Expand Down Expand Up @@ -65,7 +65,7 @@ bats_internal_load() {
printf "Error while sourcing library loader at '%s'\n" "$library_load_path" >&2
return 1
fi
return
return 0
fi

printf "Passed library load path is neither a library loader nor library directory: %s\n" "$library_load_path" >&2
Expand Down Expand Up @@ -99,17 +99,17 @@ bats_load_safe() {

if [[ -f "$slug.bash" ]]; then
bats_internal_load "$slug.bash"
return
return $?
elif [[ -f "$slug" ]]; then
bats_internal_load "$slug"
return
return $?
fi

# loading from PATH (retained for backwards compatibility)
if [[ ! -f "$1" ]] && type -P "$1" >/dev/null; then
# shellcheck disable=SC1090
source "$1"
return
return $?
fi

# No library load path can be found
Expand Down Expand Up @@ -160,6 +160,7 @@ bats_load_library() { # <slug>
# load acts like bats_load_safe but exits the shell instead of returning 1.
load() {
if ! bats_load_safe "$@"; then
echo "${FUNCNAME[0]} $LINENO" >&3
exit 1
fi
}
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/load/load_in_teardown_after_failure.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
teardown() {
load 'test_helper'
}

@test failed {
false
}
8 changes: 8 additions & 0 deletions test/load.bats
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,12 @@ setup() {
# shellcheck disable=SC2030,SC2031
export BATS_LIB_PATH="$path_dir"
run -1 bats "$FIXTURE_ROOT/failing_bats_load_library.bats"
}

@test "load in teardown after failure does not prevent test from being counted (see #609)" {
run -1 bats "$FIXTURE_ROOT/load_in_teardown_after_failure.bats"
[ "${lines[0]}" = 1..1 ]
[ "${lines[1]}" = "not ok 1 failed" ]
[ "${lines[3]}" = "# \`false' failed" ]
[ ${#lines[@]} -eq 4 ]
}