diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 6f5d811..e873e81 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -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 @@ -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 @@ -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 } @@ -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 @@ -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 diff --git a/test/bats.bats b/test/bats.bats index 9994433..d9f6d91 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -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 ] @@ -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 ] @@ -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)" ] -} diff --git a/test/fixtures/bats/failing_setup.bats b/test/fixtures/bats/failing_setup.bats new file mode 100644 index 0000000..9a35587 --- /dev/null +++ b/test/fixtures/bats/failing_setup.bats @@ -0,0 +1,7 @@ +setup() { + false +} + +@test "truth" { + true +} diff --git a/test/fixtures/bats/failing_teardown.bats b/test/fixtures/bats/failing_teardown.bats new file mode 100644 index 0000000..0b59e26 --- /dev/null +++ b/test/fixtures/bats/failing_teardown.bats @@ -0,0 +1,7 @@ +teardown() { + false +} + +@test "truth" { + [ "$PASS" = "1" ] +}