Skip to content

Commit

Permalink
Merge pull request #612 from martin-schulze-vireso/fix/issue-609-load…
Browse files Browse the repository at this point in the history
…-in-teardown-after-failure-exits

Fix load in teardown after failure exits test
  • Loading branch information
martin-schulze-vireso committed Jun 15, 2022
2 parents 61abd09 + e7cd5bf commit 26c9b18
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
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 ]
}

0 comments on commit 26c9b18

Please sign in to comment.