1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-11-17 03:32:27 +01:00

Print the failing status code, if it's significant

This commit is contained in:
Sam Stephenson 2014-08-12 16:22:43 -05:00
parent 81be444b3b
commit 716d2d62ed
4 changed files with 31 additions and 7 deletions

View File

@ -148,13 +148,19 @@ bats_print_stack_trace() {
bats_print_failed_command() { bats_print_failed_command() {
local frame="$1" local frame="$1"
local status="$2"
local filename="$(bats_frame_filename "$frame")" local filename="$(bats_frame_filename "$frame")"
local lineno="$(bats_frame_lineno "$frame")" local lineno="$(bats_frame_lineno "$frame")"
local failed_line="$(bats_extract_line "$filename" "$lineno")" local failed_line="$(bats_extract_line "$filename" "$lineno")"
local failed_command="$(bats_strip_string "$failed_line")" local failed_command="$(bats_strip_string "$failed_line")"
echo -n "# \`${failed_command}' "
echo "# \`${failed_command}' failed" if [ $status -eq 1 ]; then
echo "failed"
else
echo "failed with status $status"
fi
} }
bats_frame_lineno() { bats_frame_lineno() {
@ -200,17 +206,23 @@ bats_debug_trap() {
} }
bats_error_trap() { bats_error_trap() {
BATS_ERROR_STATUS="$?"
BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" ) BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" )
trap - debug trap - debug
} }
bats_teardown_trap() { bats_teardown_trap() {
trap "bats_exit_trap" exit trap "bats_exit_trap" exit
if teardown >>"$BATS_OUT" 2>&1; then local status=0
teardown >>"$BATS_OUT" 2>&1 || status="$?"
if [ $status -eq 0 ]; then
BATS_TEARDOWN_COMPLETED=1 BATS_TEARDOWN_COMPLETED=1
elif [ -n "$BATS_TEST_COMPLETED" ]; then elif [ -n "$BATS_TEST_COMPLETED" ]; then
BATS_ERROR_STATUS="$status"
BATS_ERROR_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" ) BATS_ERROR_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" )
fi fi
bats_exit_trap bats_exit_trap
} }
@ -230,7 +242,7 @@ bats_exit_trap() {
if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
bats_print_stack_trace "${BATS_ERROR_STACK_TRACE[@]}" >&3 bats_print_stack_trace "${BATS_ERROR_STACK_TRACE[@]}" >&3
bats_print_failed_command "${BATS_ERROR_STACK_TRACE[@]}" >&3 bats_print_failed_command "${BATS_ERROR_STACK_TRACE[0]}" "$BATS_ERROR_STATUS" >&3
sed -e "s/^/# /" < "$BATS_OUT" >&3 sed -e "s/^/# /" < "$BATS_OUT" >&3
status=1 status=1
else else

View File

@ -46,7 +46,7 @@ fixtures bats
[ "${lines[0]}" = '1..1' ] [ "${lines[0]}" = '1..1' ]
[ "${lines[1]}" = 'not ok 1 a failing test' ] [ "${lines[1]}" = 'not ok 1 a failing test' ]
[ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ] [ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ]
[ "${lines[3]}" = "# \`false' failed" ] [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ]
} }
@test "one failing and one passing test" { @test "one failing and one passing test" {
@ -59,6 +59,12 @@ fixtures bats
[ "${lines[4]}" = 'ok 2 a passing test' ] [ "${lines[4]}" = 'ok 2 a passing test' ]
} }
@test "failing test with significant status" {
STATUS=2 run bats "$FIXTURE_ROOT/failing.bats"
[ $status -eq 1 ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ]
}
@test "failing helper function logs the test case's line number" { @test "failing helper function logs the test case's line number" {
run bats "$FIXTURE_ROOT/failing_helper.bats" run bats "$FIXTURE_ROOT/failing_helper.bats"
[ $status -eq 1 ] [ $status -eq 1 ]
@ -102,7 +108,7 @@ fixtures bats
[ $status -eq 1 ] [ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 truth' ] [ "${lines[1]}" = 'not ok 1 truth' ]
[ "${lines[2]}" = "# (from function \`teardown' in test file $FIXTURE_ROOT/failing_teardown.bats, line 2)" ] [ "${lines[2]}" = "# (from function \`teardown' in test file $FIXTURE_ROOT/failing_teardown.bats, line 2)" ]
[ "${lines[3]}" = "# \`false' failed" ] [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ]
} }
@test "failing test with teardown failure" { @test "failing test with teardown failure" {
@ -113,6 +119,12 @@ fixtures bats
[ "${lines[3]}" = $'# `[ "$PASS" = "1" ]\' failed' ] [ "${lines[3]}" = $'# `[ "$PASS" = "1" ]\' failed' ]
} }
@test "teardown failure with significant status" {
PASS=1 STATUS=2 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ]
}
@test "load sources scripts relative to the current test file" { @test "load sources scripts relative to the current test file" {
run bats "$FIXTURE_ROOT/load.bats" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ] [ $status -eq 0 ]

View File

@ -1,5 +1,5 @@
@test "a failing test" { @test "a failing test" {
true true
true true
false eval "( exit ${STATUS:-1} )"
} }

View File

@ -1,5 +1,5 @@
teardown() { teardown() {
false eval "( exit ${STATUS:-1} )"
} }
@test "truth" { @test "truth" {