From bbaf0f50df896df63fbaaf82c5bf774907847a7a Mon Sep 17 00:00:00 2001 From: Andrey Mazo Date: Sat, 28 Jun 2014 23:16:32 +0400 Subject: [PATCH] Print failed statement in backtrace Update tests accordingly. --- libexec/bats-exec-test | 23 ++++++++++++++++-- test/bats.bats | 55 ++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 8242d17..b12d4c0 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -120,11 +120,24 @@ bats_print_stack_trace() { local index=1 local count="${#BATS_STACK_TRACE[@]}" local line + local frame + local filename + local failed_cmd_line + local failed_cmd_filename + local failed_cmd for frame in "${BATS_STACK_TRACE[@]}"; do + if [ $index -eq $count ]; then + filename="${BATS_TEST_FILENAME}" + else + filename="$(bats_frame_filename "$frame")" + fi + if [ $index -eq 1 ]; then line="$BATS_LINE_NUMBER" echo -n "# (" + failed_cmd_line="${line}" + failed_cmd_filename="${filename}" else line="$(bats_frame_line "$frame")" echo -n "# " @@ -136,9 +149,15 @@ bats_print_stack_trace() { fi if [ $index -eq $count ]; then - echo "in test file $BATS_TEST_FILENAME, line $line)" + echo "in test file $filename, line $line)" + # inspired by Gentoo Portage die() + failed_cmd="$(sed \ + -e "${failed_cmd_line}!d" \ + -e "${failed_cmd_line}s:^[ \t]\+::" \ + -e "${failed_cmd_line}q" \ + "${failed_cmd_filename}")" + echo "# \`${failed_cmd}' failed" else - local filename="$(bats_frame_filename "$frame")" echo "in file $filename, line $line," fi diff --git a/test/bats.bats b/test/bats.bats index d2efad8..3603ee6 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -43,26 +43,29 @@ fixtures bats @test "one failing test" { run bats "$FIXTURE_ROOT/failing.bats" [ $status -eq 1 ] - [ ${lines[0]} = "1..1" ] - [ ${lines[1]} = "not ok 1 a failing test" ] - [ ${lines[2]} = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ] + [ "${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" ] } @test "one failing and one passing test" { run bats "$FIXTURE_ROOT/failing_and_passing.bats" [ $status -eq 1 ] - [ ${lines[0]} = "1..2" ] - [ ${lines[1]} = "not ok 1 a failing test" ] - [ ${lines[2]} = "# (in test file $FIXTURE_ROOT/failing_and_passing.bats, line 2)" ] - [ ${lines[3]} = "ok 2 a passing test" ] + [ "${lines[0]}" = '1..2' ] + [ "${lines[1]}" = 'not ok 1 a failing test' ] + [ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing_and_passing.bats, line 2)" ] + [ "${lines[3]}" = "# \`false' failed" ] + [ "${lines[4]}" = '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[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)" ] + [ "${lines[4]}" = "# \`false' failed" ] } @test "test environments are isolated" { @@ -89,22 +92,25 @@ fixtures bats @test "setup failure" { run bats "$FIXTURE_ROOT/failing_setup.bats" [ $status -eq 1 ] - [ "${lines[1]}" = "not ok 1 truth" ] + [ "${lines[1]}" = 'not ok 1 truth' ] [ "${lines[2]}" = "# (from function \`setup' in test file $FIXTURE_ROOT/failing_setup.bats, line 2)" ] + [ "${lines[3]}" = "# \`false' failed" ] } @test "passing test with teardown failure" { PASS=1 run bats "$FIXTURE_ROOT/failing_teardown.bats" [ $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[3]}" = "# \`false' failed" ] } @test "failing test with teardown 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)" ] + [ "${lines[1]}" = 'not ok 1 truth' ] + [ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing_teardown.bats, line 6)" ] + [ "${lines[3]}" = $'# `[ "$PASS" = "1" ]\' failed' ] } @test "load sources scripts relative to the current test file" { @@ -130,9 +136,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[5]}" = "# failure stdout 1" ] - [ "${lines[6]}" = "# failure stdout 2" ] - [ "${lines[9]}" = "# failure stderr" ] + [ "${lines[6]}" = '# failure stdout 1' ] + [ "${lines[7]}" = '# failure stdout 2' ] + [ "${lines[11]}" = '# failure stderr' ] } @test "-c prints the number of tests" { @@ -172,10 +178,10 @@ fixtures bats @test "extended syntax" { run bats-exec-test -x "$FIXTURE_ROOT/failing_and_passing.bats" [ $status -eq 1 ] - [ "${lines[1]}" = "begin 1 a failing test" ] - [ "${lines[2]}" = "not ok 1 a failing test" ] - [ "${lines[4]}" = "begin 2 a passing test" ] - [ "${lines[5]}" = "ok 2 a passing test" ] + [ "${lines[1]}" = 'begin 1 a failing test' ] + [ "${lines[2]}" = 'not ok 1 a failing test' ] + [ "${lines[5]}" = 'begin 2 a passing test' ] + [ "${lines[6]}" = 'ok 2 a passing test' ] } @test "pretty and tap formats" { @@ -200,9 +206,10 @@ fixtures bats @test "single-line tests" { run bats "$FIXTURE_ROOT/single_line.bats" [ $status -eq 1 ] - [ "${lines[1]}" = "ok 1 empty" ] - [ "${lines[2]}" = "ok 2 passing" ] - [ "${lines[3]}" = "ok 3 input redirection" ] - [ "${lines[4]}" = "not ok 4 failing" ] - [ "${lines[5]}" = "# (in test file $FIXTURE_ROOT/single_line.bats, line 9)" ] + [ "${lines[1]}" = 'ok 1 empty' ] + [ "${lines[2]}" = 'ok 2 passing' ] + [ "${lines[3]}" = 'ok 3 input redirection' ] + [ "${lines[4]}" = 'not ok 4 failing' ] + [ "${lines[5]}" = "# (in test file $FIXTURE_ROOT/single_line.bats, line 9)" ] + [ "${lines[6]}" = $'# `@test "failing" { false; }\' failed' ] }