From 716d2d62ed90905e19cce50b468cdc5a48d58d40 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 12 Aug 2014 16:22:43 -0500 Subject: [PATCH] Print the failing status code, if it's significant --- libexec/bats-exec-test | 18 +++++++++++++++--- test/bats.bats | 16 ++++++++++++++-- test/fixtures/bats/failing.bats | 2 +- test/fixtures/bats/failing_teardown.bats | 2 +- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 4ec78ea..225cdd8 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -148,13 +148,19 @@ bats_print_stack_trace() { bats_print_failed_command() { local frame="$1" + local status="$2" local filename="$(bats_frame_filename "$frame")" local lineno="$(bats_frame_lineno "$frame")" local failed_line="$(bats_extract_line "$filename" "$lineno")" 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() { @@ -200,17 +206,23 @@ bats_debug_trap() { } bats_error_trap() { + BATS_ERROR_STATUS="$?" BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" ) trap - debug } bats_teardown_trap() { 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 elif [ -n "$BATS_TEST_COMPLETED" ]; then + BATS_ERROR_STATUS="$status" BATS_ERROR_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" ) fi + bats_exit_trap } @@ -230,7 +242,7 @@ bats_exit_trap() { if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&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 status=1 else diff --git a/test/bats.bats b/test/bats.bats index 3603ee6..f867922 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -46,7 +46,7 @@ fixtures bats [ "${lines[0]}" = '1..1' ] [ "${lines[1]}" = 'not ok 1 a failing test' ] [ "${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" { @@ -59,6 +59,12 @@ fixtures bats [ "${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" { run bats "$FIXTURE_ROOT/failing_helper.bats" [ $status -eq 1 ] @@ -102,7 +108,7 @@ fixtures 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)" ] - [ "${lines[3]}" = "# \`false' failed" ] + [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ] } @test "failing test with teardown failure" { @@ -113,6 +119,12 @@ fixtures bats [ "${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" { run bats "$FIXTURE_ROOT/load.bats" [ $status -eq 0 ] diff --git a/test/fixtures/bats/failing.bats b/test/fixtures/bats/failing.bats index 518b490..13be78c 100644 --- a/test/fixtures/bats/failing.bats +++ b/test/fixtures/bats/failing.bats @@ -1,5 +1,5 @@ @test "a failing test" { true true - false + eval "( exit ${STATUS:-1} )" } diff --git a/test/fixtures/bats/failing_teardown.bats b/test/fixtures/bats/failing_teardown.bats index 0b59e26..28eebf6 100644 --- a/test/fixtures/bats/failing_teardown.bats +++ b/test/fixtures/bats/failing_teardown.bats @@ -1,5 +1,5 @@ teardown() { - false + eval "( exit ${STATUS:-1} )" } @test "truth" {