Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -55,6 +55,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
* fix `load` not being available in `setup_suite` (#644)
* fix RPM spec, add regression test (#648)
* fix handling of `IFS` by `run` (#650)
* only print `setup_file`'s stderr on errors (#649)

#### Documentation

Expand Down
2 changes: 1 addition & 1 deletion lib/bats-core/tracing.bash
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bats_print_stack_trace() {
bats_print_failed_command() {
local stack_trace=("${@}")
if [[ ${#stack_trace[@]} -eq 0 ]]; then
return
return 0
fi
local frame="${stack_trace[${#stack_trace[@]} - 1]}"
local filename
Expand Down
23 changes: 18 additions & 5 deletions libexec/bats-core/bats-exec-suite
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ filter_tags_list=()
flags=('--dummy-flag') # add a dummy flag to prevent unset variable errors on empty array expansion in old bash versions
setup_suite_file=''
BATS_TRACE_LEVEL="${BATS_TRACE_LEVEL:-0}"
BATS_SHOW_OUTPUT_OF_SUCCEEDING_TESTS=

abort() {
printf 'Error: %s\n' "$1" >&2
exit 1
}

# shellcheck source=lib/bats-core/common.bash
# shellcheck source=lib/bats-core/common.bash disable=SC2153
source "$BATS_ROOT/lib/bats-core/common.bash"

while [[ "$#" -ne 0 ]]; do
Expand Down Expand Up @@ -75,6 +76,7 @@ while [[ "$#" -ne 0 ]]; do
;;
--show-output-of-passing-tests)
flags+=(--show-output-of-passing-tests)
BATS_SHOW_OUTPUT_OF_SUCCEEDING_TESTS=1
;;
--verbose-run)
flags+=(--verbose-run)
Expand Down Expand Up @@ -324,7 +326,10 @@ bats_setup_tracing

trap bats_suite_exit_trap EXIT

exec 3<&1

bats_suite_exit_trap() {
local print_bats_out="${BATS_SHOW_OUTPUT_OF_SUCCEEDING_TESTS}"
if [[ -z "${BATS_SETUP_SUITE_COMPLETED}" || -z "${BATS_TEARDOWN_SUITE_COMPLETED}" ]]; then
if [[ -z "${BATS_SETUP_SUITE_COMPLETED}" ]]; then
printf "not ok 1 setup_suite\n"
Expand All @@ -335,8 +340,14 @@ bats_suite_exit_trap() {
bats_get_failure_stack_trace stack_trace
bats_print_stack_trace "${stack_trace[@]}"
bats_print_failed_command "${stack_trace[@]}"
print_bats_out=1
bats_exec_suite_status=1
fi

if [[ -n "$print_bats_out" ]]; then
bats_prefix_lines_for_tap_output < "$BATS_OUT"
fi

if [[ ${BATS_INTERRUPTED-NOTSET} != NOTSET ]]; then
printf "\n# Received SIGINT, aborting ...\n\n"
fi
Expand All @@ -346,15 +357,15 @@ bats_suite_exit_trap() {
rm "$BATS_RUNLOG_FILE"
fi
exit "$bats_exec_suite_status"
}
} >&3

bats_run_teardown_suite() {
local bats_teardown_suite_status=0
# avoid being called twice, in case this is not called through bats_teardown_suite_trap
# but from the end of file
trap bats_suite_exit_trap EXIT
BATS_TEARDOWN_SUITE_COMPLETED=
teardown_suite 2>&1 || bats_teardown_suite_status=$?
teardown_suite >>"$BATS_OUT" 2>&1 || bats_teardown_suite_status=$?
if (( bats_teardown_suite_status == 0 )); then
BATS_TEARDOWN_SUITE_COMPLETED=1
elif [[ -n "${BATS_SETUP_SUITE_COMPLETED:-}" ]]; then
Expand Down Expand Up @@ -382,7 +393,7 @@ trap bats_teardown_suite_trap EXIT
if [[ -n "$setup_suite_file" ]]; then
setup_suite() {
printf "%s does not define \`setup_suite()\`\n" "$setup_suite_file" >&2
exit 1
return 1
}

# shellcheck disable=SC2034 # will be used in the sourced file below
Expand All @@ -394,9 +405,11 @@ if [[ -n "$setup_suite_file" ]]; then
source "$setup_suite_file"
fi

BATS_OUT="$BATS_RUN_TMPDIR/suite.out"

set -eET
BATS_SETUP_SUITE_COMPLETED=
setup_suite 2>&1
setup_suite >>"$BATS_OUT" 2>&1
BATS_SETUP_SUITE_COMPLETED=1
set +ET

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
setup_suite() {
echo setup_suite
}

teardown_suite() {
echo teardown_suite
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@test passing {
:
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
setup_suite() {
echo setup_suite
false
}

teardown_suite() {
echo teardown_suite
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@test passing {
:
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ setup_suite() {
teardown_suite() {
echo teardown_suite stdout
echo teardown_suite stderr >&2
false
}
60 changes: 42 additions & 18 deletions test/suite_setup_teardown.bats
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ setup() {

@test "setup_suite.bash without setup_suite() is an error" {
reentrant_run ! bats "$FIXTURE_ROOT/no_setup_suite_function/"
[ "${lines[1]}" == "$FIXTURE_ROOT/no_setup_suite_function/setup_suite.bash does not define \`setup_suite()\`" ]
[ "${lines[0]}" == "1..1" ]
[ "${lines[1]}" == "not ok 1 setup_suite" ]
[ "${lines[2]}" == "# $FIXTURE_ROOT/no_setup_suite_function/setup_suite.bash does not define \`setup_suite()\`" ]
[ "${#lines[@]}" -eq 3 ]
}

@test "exported variables from setup_suite are visible in setup_file, setup and @test" {
Expand All @@ -73,50 +76,71 @@ setup() {

@test "errors in setup_suite reported correctly" {
LANG=C reentrant_run ! bats "$FIXTURE_ROOT/error_in_setup_suite/"
[ "${lines[1]}" == "$FIXTURE_ROOT/error_in_setup_suite/setup_suite.bash: line 2: call-to-undefined-command: command not found" ]
[ "${lines[4]}" == "# $FIXTURE_ROOT/error_in_setup_suite/setup_suite.bash: line 2: call-to-undefined-command: command not found" ]
}

@test "errors in teardown_suite reported correctly" {
LANG=C reentrant_run ! bats "$FIXTURE_ROOT/error_in_teardown_suite/"
[ "${lines[2]}" == "$FIXTURE_ROOT/error_in_teardown_suite/setup_suite.bash: line 6: call-to-undefined-command: command not found" ]
[ "${lines[5]}" == "# $FIXTURE_ROOT/error_in_teardown_suite/setup_suite.bash: line 6: call-to-undefined-command: command not found" ]
}

@test "failure in setup_suite skips further setup and suite but runs teardown_suite" {
reentrant_run ! bats "$FIXTURE_ROOT/failure_in_setup_suite/"
[ "${lines[1]}" == "setup_suite before" ] # <- only setup_suite code before failure is run
[ "${lines[2]}" == "teardown_suite" ] # <- teardown is run
[ "${lines[0]}" == "1..1" ]
# get a nice error message
[ "${lines[3]}" == "not ok 1 setup_suite" ]
[ "${lines[4]}" == "# (from function \`setup_suite' in test file $RELATIVE_FIXTURE_ROOT/failure_in_setup_suite/setup_suite.bash, line 3)" ]
[ "${lines[5]}" == "# \`false' failed" ]
[ "${lines[1]}" == "not ok 1 setup_suite" ]
[ "${lines[2]}" == "# (from function \`setup_suite' in test file $RELATIVE_FIXTURE_ROOT/failure_in_setup_suite/setup_suite.bash, line 3)" ]
[ "${lines[3]}" == "# \`false' failed" ]
[ "${lines[4]}" == "# setup_suite before" ] # <- only setup_suite code before failure is run
[ "${lines[5]}" == "# teardown_suite" ] # <- teardown is run
[ ${#lines[@]} -eq 6 ]
}

@test "midway failure in teardown_suite does not fail test suite, remaining code is executed" {
reentrant_run -0 bats "$FIXTURE_ROOT/failure_in_teardown_suite/"
[ "${lines[2]}" == "teardown_suite before" ]
[ "${lines[3]}" == "teardown_suite after" ]
reentrant_run -0 bats --show-output-of-passing-tests "$FIXTURE_ROOT/failure_in_teardown_suite/"
[ "${lines[2]}" == "# teardown_suite before" ]
[ "${lines[3]}" == "# teardown_suite after" ]
[ "${#lines[@]}" -eq 4 ]
}

@test "nonzero return in teardown_suite does fails test suite" {
reentrant_run -1 bats "$FIXTURE_ROOT/return_nonzero_in_teardown_suite/"
[ "${lines[2]}" == "teardown_suite before" ]
[ "${lines[3]}" == "not ok 2 teardown_suite" ]
[ "${lines[4]}" == "# (from function \`teardown_suite' in test file $RELATIVE_FIXTURE_ROOT/return_nonzero_in_teardown_suite/setup_suite.bash, line 7)" ]
[ "${lines[5]}" == "# \`return 1' failed" ]
[ "${lines[0]}" == "1..1" ]
[ "${lines[1]}" == "ok 1 test" ]
[ "${lines[2]}" == "not ok 2 teardown_suite" ]
[ "${lines[3]}" == "# (from function \`teardown_suite' in test file $RELATIVE_FIXTURE_ROOT/return_nonzero_in_teardown_suite/setup_suite.bash, line 7)" ]
[ "${lines[4]}" == "# \`return 1' failed" ]
[ "${lines[5]}" == "# teardown_suite before" ]
[ "${lines[6]}" == "# bats warning: Executed 2 instead of expected 1 tests" ]
[ "${#lines[@]}" -eq 7 ]
}

@test "stderr from setup/teardown_suite does not overtake stdout" {
reentrant_run -0 --separate-stderr bats "$FIXTURE_ROOT/stderr_in_setup_teardown_suite/"
[[ "$output" == *$'setup_suite stdout\nsetup_suite stderr'* ]] || false
[[ "$output" == *$'teardown_suite stdout\nteardown_suite stderr'* ]] || false
reentrant_run -1 --separate-stderr bats "$FIXTURE_ROOT/stderr_in_setup_teardown_suite/"
[[ "$output" == *$'setup_suite stdout\n'*'setup_suite stderr'* ]] || false
[[ "$output" == *$'teardown_suite stdout\n'*'teardown_suite stderr'* ]] || false
}

@test "load is available in setup_suite" {
reentrant_run -0 bats "$FIXTURE_ROOT/call_load/"
[ "${lines[0]}" = "1..1" ]
[ "${lines[1]}" = "ok 1 passing" ]
[ "${#lines[@]}" -eq 2 ]
}

@test "output frorm setup_suite is only visible on failure" {
reentrant_run -0 bats "$FIXTURE_ROOT/default_name/"
[ "${lines[0]}" = "1..1" ]
[ "${lines[1]}" = "ok 1 passing" ]
[ "${#lines[@]}" -eq 2 ]


reentrant_run -1 bats "$FIXTURE_ROOT/output_with_failure/"
[ "${lines[0]}" = "1..1" ]
[ "${lines[1]}" = "not ok 1 setup_suite" ]
[ "${lines[2]}" = "# (from function \`setup_suite' in test file $RELATIVE_FIXTURE_ROOT/output_with_failure/setup_suite.bash, line 3)" ]
[ "${lines[3]}" = "# \`false' failed" ]
[ "${lines[4]}" = "# setup_suite" ]
[ "${lines[5]}" = "# teardown_suite" ]
[ "${#lines[@]}" -eq 6 ]
}