Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Correctly log errors in setup and teardown functions
Browse files Browse the repository at this point in the history
Fixes #30
  • Loading branch information
sstephenson committed Nov 4, 2013
1 parent bfa4ebc commit c8d63dd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
24 changes: 19 additions & 5 deletions libexec/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ bats_capture_stack_trace() {
BATS_LINE_NUMBER_="$2"

local test_pattern=" $BATS_TEST_NAME $BATS_TEST_SOURCE"
local setup_pattern=" setup $BATS_TEST_SOURCE"
local teardown_pattern=" teardown $BATS_TEST_SOURCE"

local index=0
local frame

while frame="$(caller "$index")"; do
BATS_STACK_TRACE[$index]="$frame"
if [[ "$frame" = *"$test_pattern" ]]; then
if [[ "$frame" = *"$test_pattern" || \
"$frame" = *"$setup_pattern" || \
"$frame" = *"$teardown_pattern" ]]; then
break
else
let index+=1
Expand All @@ -117,12 +122,16 @@ bats_print_stack_trace() {
echo -n "# "
fi

local fn="$(bats_frame_function "$frame")"
if [ "$fn" != "$BATS_TEST_NAME" ]; then
echo -n "from function \`$fn' "
fi

if [ $index -eq $count ]; then
echo "in test file $BATS_TEST_FILENAME, line $line)"
else
local fn="$(bats_frame_function "$frame")"
local filename="$(bats_frame_filename "$frame")"
echo "from function \`$fn' in file $filename, line $line,"
echo "in file $filename, line $line,"
fi

let index+=1
Expand Down Expand Up @@ -160,7 +169,11 @@ bats_error_trap() {

bats_teardown_trap() {
trap bats_exit_trap exit
teardown >>"$BATS_OUT" 2>&1
if teardown >>"$BATS_OUT" 2>&1; then
BATS_TEARDOWN_COMPLETED=1
elif [ -n "$BATS_TEST_COMPLETED" ]; then
BATS_LINE_NUMBER="$BATS_LINE_NUMBER_"
fi
bats_exit_trap
}

Expand All @@ -177,7 +190,7 @@ bats_exit_trap() {
fi
fi

if [ -z "$BATS_TEST_COMPLETED" ]; then
if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
bats_print_stack_trace >&3
sed -e "s/^/# /" < "$BATS_OUT" >&3
Expand Down Expand Up @@ -212,6 +225,7 @@ bats_perform_test() {
fi

BATS_TEST_COMPLETED=""
BATS_TEARDOWN_COMPLETED=""
BATS_ERROR_LINE=""
trap "bats_capture_stack_trace \"\$BASH_SOURCE\" \$LINENO" debug
trap "bats_error_trap" err
Expand Down
37 changes: 29 additions & 8 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ fixtures bats
[ ${lines[3]} = "ok 2 a passing test" ]
}

@test "failing helper function logs the test case's line number" {
run bats "$FIXTURE_ROOT/failing_helper.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = "not ok 1 failing helper function" ]
[ "${lines[2]}" = "# (from function \`failing_helper' in file $FIXTURE_ROOT/test_helper.bash, line 6," ]
[ "${lines[3]}" = "# in test file $FIXTURE_ROOT/failing_helper.bats, line 5)" ]
}

@test "test environments are isolated" {
run bats "$FIXTURE_ROOT/environment.bats"
[ $status -eq 0 ]
Expand All @@ -78,6 +86,27 @@ fixtures bats
[ ${#lines[@]} -eq 3 ]
}

@test "setup failure should reuslt in test failure" {
run bats "$FIXTURE_ROOT/failing_setup.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = "not ok 1 truth" ]
[ "${lines[2]}" = "# (from function \`setup' in test file $FIXTURE_ROOT/failing_setup.bats, line 2)" ]
}

@test "passing test with teardown failure should result in test failure" {
PASS=1 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = "not ok 1 truth" ]
[ "${lines[2]}" = "# (from function \`teardown' in test file $FIXTURE_ROOT/failing_teardown.bats, line 2)" ]
}

@test "failing test with teardown failure should result in test failure" {
PASS=0 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = "not ok 1 truth" ]
[ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing_teardown.bats, line 6)" ]
}

@test "load sources scripts relative to the current test file" {
run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ]
Expand Down Expand Up @@ -157,11 +186,3 @@ fixtures bats
[ "${lines[0]}" = "This isn't TAP!" ]
[ "${lines[1]}" = "Good day to you" ]
}

@test "failing helper function logs the test case's line number" {
run bats "$FIXTURE_ROOT/failing_helper.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = "not ok 1 failing helper function" ]
[ "${lines[2]}" = "# (from function \`failing_helper' in file $FIXTURE_ROOT/test_helper.bash, line 6," ]
[ "${lines[3]}" = "# in test file $FIXTURE_ROOT/failing_helper.bats, line 5)" ]
}
7 changes: 7 additions & 0 deletions test/fixtures/bats/failing_setup.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
setup() {
false
}

@test "truth" {
true
}
7 changes: 7 additions & 0 deletions test/fixtures/bats/failing_teardown.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
teardown() {
false
}

@test "truth" {
[ "$PASS" = "1" ]
}

0 comments on commit c8d63dd

Please sign in to comment.