From bc72b8587183be3f973292d6bf547adf864e7f9b Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Thu, 23 May 2013 12:47:17 -0500 Subject: [PATCH] Print test file path and line number after a failing test --- libexec/bats-exec-test | 32 +++++++++++++++++++++++++++----- test/bats.bats | 10 ++++++---- test/fixtures/bats/failing.bats | 2 ++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index f427dcb..d4c21b3 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -1,5 +1,7 @@ #!/usr/bin/env bash set -e +set -E +set -T BATS_COUNT_ONLY="" if [ "$1" = "-c" ]; then @@ -32,13 +34,19 @@ load() { } run() { - local e + local e E T [[ ! "$-" =~ e ]] || e=1 + [[ ! "$-" =~ E ]] || E=1 + [[ ! "$-" =~ T ]] || T=1 set +e + set +E + set +T output="$("$@" 2>&1)" status="$?" IFS=$'\n' lines=($output) [ -z "$e" ] || set -e + [ -z "$E" ] || set -E + [ -z "$T" ] || set -T } setup() { @@ -51,7 +59,6 @@ teardown() { bats_test_info() { BATS_TEST_DESCRIPTION="$1" - BATS_LINE_NUMBER="$2" } bats_test_function() { @@ -59,8 +66,19 @@ bats_test_function() { BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name" } +bats_debug_trap() { + if [ "$BASH_SOURCE" != "$1" ]; then + BATS_LINE_NUMBER="$BATS_LINE_NUMBER_" + BATS_LINE_NUMBER_="$2" + fi +} + +bats_error_trap() { + trap - debug +} + bats_teardown_trap() { - trap bats_exit_trap err exit + trap bats_exit_trap exit teardown >>"$BATS_OUT" 2>&1 bats_exit_trap } @@ -71,7 +89,8 @@ bats_exit_trap() { if [ -z "$BATS_TEST_COMPLETED" ]; then echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 - sed -e "s/^/ /" < "$BATS_OUT" >&3 + echo "# $BATS_TEST_FILENAME:$BATS_LINE_NUMBER" >&3 + sed -e "s/^/# /" < "$BATS_OUT" >&3 status=1 else echo "ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 @@ -103,7 +122,10 @@ bats_perform_test() { fi BATS_TEST_COMPLETED="" - trap bats_teardown_trap err exit + BATS_ERROR_LINE="" + trap "bats_debug_trap \"\$BASH_SOURCE\" \$LINENO" debug + trap "bats_error_trap" err + trap "bats_teardown_trap" exit { setup "$BATS_TEST_NAME" } >>"$BATS_OUT" 2>&1 diff --git a/test/bats.bats b/test/bats.bats index 2ac5905..fe24cd7 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -39,6 +39,7 @@ fixtures bats [ $status -eq 1 ] [ ${lines[0]} = "1..1" ] [ ${lines[1]} = "not ok 1 a failing test" ] + [ ${lines[2]} = "# $FIXTURE_ROOT/failing.bats:4" ] } @test "one failing and one passing test" { @@ -46,7 +47,8 @@ fixtures bats [ $status -eq 1 ] [ ${lines[0]} = "1..2" ] [ ${lines[1]} = "not ok 1 a failing test" ] - [ ${lines[2]} = "ok 2 a passing test" ] + [ ${lines[2]} = "# $FIXTURE_ROOT/failing_and_passing.bats:2" ] + [ ${lines[3]} = "ok 2 a passing test" ] } @test "test environments are isolated" { @@ -83,9 +85,9 @@ fixtures bats @test "output is discarded for passing tests and printed for failing tests" { run bats "$FIXTURE_ROOT/output.bats" [ $status -eq 1 ] - [ "${lines[4]}" = " failure stdout 1" ] - [ "${lines[5]}" = " failure stdout 2" ] - [ "${lines[7]}" = " failure stderr" ] + [ "${lines[5]}" = "# failure stdout 1" ] + [ "${lines[6]}" = "# failure stdout 2" ] + [ "${lines[9]}" = "# failure stderr" ] } @test "-c prints the number of tests" { diff --git a/test/fixtures/bats/failing.bats b/test/fixtures/bats/failing.bats index 4e04302..518b490 100644 --- a/test/fixtures/bats/failing.bats +++ b/test/fixtures/bats/failing.bats @@ -1,3 +1,5 @@ @test "a failing test" { + true + true false }