From e99dedd6c681a75f4bf6ded57bb57f692772364f Mon Sep 17 00:00:00 2001 From: Andrey Mazo Date: Wed, 21 May 2014 00:14:06 -0400 Subject: [PATCH] Print failed statement in backtrace Update tests accordingly. --- libexec/bats-exec-test | 16 +++++++--- test/bats.bats | 70 +++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 32 deletions(-) diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 249df74..3de9e4e 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -151,15 +151,24 @@ bats_print_stack_trace() { local index=1 local count="${#BATS_STACK_TRACE[@]}" local line + local frame + local filename 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 "# (" + echo "# (command" + echo "# \`$(sed -e "${line}!d" -e "${line}s:^[ \t]\+::" -e "${line}q" "${filename}")'" else line="$(bats_frame_line "$frame")" - echo -n "# " fi + echo -n "# " local fn="$(bats_frame_function "$frame")" if [ "$fn" != "$BATS_TEST_NAME" ]; then @@ -167,9 +176,8 @@ 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)" 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..e73f2fb 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -43,26 +43,32 @@ 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]}" = '# (command' ] + [ "${lines[3]}" = "# \`false'" ] + [ "${lines[4]}" = "# in test file $FIXTURE_ROOT/failing.bats, line 4)" ] } @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]}" = '# (command' ] + [ "${lines[3]}" = "# \`false'" ] + [ "${lines[4]}" = "# in test file $FIXTURE_ROOT/failing_and_passing.bats, line 2)" ] + [ "${lines[5]}" = '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)" ] + [ "${lines[1]}" = 'not ok 1 failing helper function' ] + [ "${lines[2]}" = '# (command' ] + [ "${lines[3]}" = "# \`false'" ] + [ "${lines[4]}" = "# from function \`failing_helper' in file $FIXTURE_ROOT/test_helper.bash, line 6," ] + [ "${lines[5]}" = "# in test file $FIXTURE_ROOT/failing_helper.bats, line 5)" ] } @test "test environments are isolated" { @@ -89,22 +95,28 @@ fixtures bats @test "setup 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)" ] + [ "${lines[1]}" = 'not ok 1 truth' ] + [ "${lines[2]}" = '# (command' ] + [ "${lines[3]}" = "# \`false'" ] + [ "${lines[4]}" = "# from function \`setup' in test file $FIXTURE_ROOT/failing_setup.bats, line 2)" ] } @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[2]}" = "# (from function \`teardown' in test file $FIXTURE_ROOT/failing_teardown.bats, line 2)" ] + [ "${lines[1]}" = 'not ok 1 truth' ] + [ "${lines[2]}" = '# (command' ] + [ "${lines[3]}" = "# \`false'" ] + [ "${lines[4]}" = "# from function \`teardown' in test file $FIXTURE_ROOT/failing_teardown.bats, line 2)" ] } @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]}" = '# (command' ] + [ "${lines[3]}" = $'# `[ "$PASS" = "1" ]\'' ] + [ "${lines[4]}" = "# in test file $FIXTURE_ROOT/failing_teardown.bats, line 6)" ] } @test "load sources scripts relative to the current test file" { @@ -130,9 +142,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[7]}" = '# failure stdout 1' ] + [ "${lines[8]}" = '# failure stdout 2' ] + [ "${lines[13]}" = '# failure stderr' ] } @test "-c prints the number of tests" { @@ -172,10 +184,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[6]}" = 'begin 2 a passing test' ] + [ "${lines[7]}" = 'ok 2 a passing test' ] } @test "pretty and tap formats" { @@ -200,9 +212,11 @@ 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]}" = '# (command' ] + [ "${lines[6]}" = $'# `@test "failing" { false; }\'' ] + [ "${lines[7]}" = "# in test file $FIXTURE_ROOT/single_line.bats, line 9)" ] }