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

Print failed statement in backtrace

Update tests accordingly.
This commit is contained in:
Andrey Mazo 2014-05-21 00:14:06 -04:00
parent bd812090eb
commit e99dedd6c6
2 changed files with 54 additions and 32 deletions

View File

@ -151,15 +151,24 @@ bats_print_stack_trace() {
local index=1 local index=1
local count="${#BATS_STACK_TRACE[@]}" local count="${#BATS_STACK_TRACE[@]}"
local line local line
local frame
local filename
for frame in "${BATS_STACK_TRACE[@]}"; do 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 if [ $index -eq 1 ]; then
line="$BATS_LINE_NUMBER" line="$BATS_LINE_NUMBER"
echo -n "# (" echo "# (command"
echo "# \`$(sed -e "${line}!d" -e "${line}s:^[ \t]\+::" -e "${line}q" "${filename}")'"
else else
line="$(bats_frame_line "$frame")" line="$(bats_frame_line "$frame")"
echo -n "# "
fi fi
echo -n "# "
local fn="$(bats_frame_function "$frame")" local fn="$(bats_frame_function "$frame")"
if [ "$fn" != "$BATS_TEST_NAME" ]; then if [ "$fn" != "$BATS_TEST_NAME" ]; then
@ -167,9 +176,8 @@ bats_print_stack_trace() {
fi fi
if [ $index -eq $count ]; then if [ $index -eq $count ]; then
echo "in test file $BATS_TEST_FILENAME, line $line)" echo "in test file $filename, line $line)"
else else
local filename="$(bats_frame_filename "$frame")"
echo "in file $filename, line $line," echo "in file $filename, line $line,"
fi fi

View File

@ -43,26 +43,32 @@ fixtures bats
@test "one failing test" { @test "one failing test" {
run bats "$FIXTURE_ROOT/failing.bats" run bats "$FIXTURE_ROOT/failing.bats"
[ $status -eq 1 ] [ $status -eq 1 ]
[ ${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]}" = '# (command' ]
[ "${lines[3]}" = "# \`false'" ]
[ "${lines[4]}" = "# in test file $FIXTURE_ROOT/failing.bats, line 4)" ]
} }
@test "one failing and one passing test" { @test "one failing and one passing test" {
run bats "$FIXTURE_ROOT/failing_and_passing.bats" run bats "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 1 ] [ $status -eq 1 ]
[ ${lines[0]} = "1..2" ] [ "${lines[0]}" = '1..2' ]
[ ${lines[1]} = "not ok 1 a failing test" ] [ "${lines[1]}" = 'not ok 1 a failing test' ]
[ ${lines[2]} = "# (in test file $FIXTURE_ROOT/failing_and_passing.bats, line 2)" ] [ "${lines[2]}" = '# (command' ]
[ ${lines[3]} = "ok 2 a passing test" ] [ "${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" { @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 ]
[ "${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[2]}" = '# (command' ]
[ "${lines[3]}" = "# in test file $FIXTURE_ROOT/failing_helper.bats, line 5)" ] [ "${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" { @test "test environments are isolated" {
@ -89,22 +95,28 @@ fixtures bats
@test "setup failure" { @test "setup failure" {
run bats "$FIXTURE_ROOT/failing_setup.bats" run bats "$FIXTURE_ROOT/failing_setup.bats"
[ $status -eq 1 ] [ $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[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" { @test "passing test with teardown failure" {
PASS=1 run bats "$FIXTURE_ROOT/failing_teardown.bats" PASS=1 run bats "$FIXTURE_ROOT/failing_teardown.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]}" = '# (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" { @test "failing test with teardown failure" {
PASS=0 run bats "$FIXTURE_ROOT/failing_teardown.bats" PASS=0 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ] [ $status -eq 1 ]
[ "${lines[1]}" = "not ok 1 truth" ] [ "${lines[1]}" = 'not ok 1 truth' ]
[ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing_teardown.bats, line 6)" ] [ "${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" { @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" { @test "output is discarded for passing tests and printed for failing tests" {
run bats "$FIXTURE_ROOT/output.bats" run bats "$FIXTURE_ROOT/output.bats"
[ $status -eq 1 ] [ $status -eq 1 ]
[ "${lines[5]}" = "# failure stdout 1" ] [ "${lines[7]}" = '# failure stdout 1' ]
[ "${lines[6]}" = "# failure stdout 2" ] [ "${lines[8]}" = '# failure stdout 2' ]
[ "${lines[9]}" = "# failure stderr" ] [ "${lines[13]}" = '# failure stderr' ]
} }
@test "-c prints the number of tests" { @test "-c prints the number of tests" {
@ -172,10 +184,10 @@ fixtures bats
@test "extended syntax" { @test "extended syntax" {
run bats-exec-test -x "$FIXTURE_ROOT/failing_and_passing.bats" run bats-exec-test -x "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 1 ] [ $status -eq 1 ]
[ "${lines[1]}" = "begin 1 a failing test" ] [ "${lines[1]}" = 'begin 1 a failing test' ]
[ "${lines[2]}" = "not ok 1 a failing test" ] [ "${lines[2]}" = 'not ok 1 a failing test' ]
[ "${lines[4]}" = "begin 2 a passing test" ] [ "${lines[6]}" = 'begin 2 a passing test' ]
[ "${lines[5]}" = "ok 2 a passing test" ] [ "${lines[7]}" = 'ok 2 a passing test' ]
} }
@test "pretty and tap formats" { @test "pretty and tap formats" {
@ -200,9 +212,11 @@ fixtures bats
@test "single-line tests" { @test "single-line tests" {
run bats "$FIXTURE_ROOT/single_line.bats" run bats "$FIXTURE_ROOT/single_line.bats"
[ $status -eq 1 ] [ $status -eq 1 ]
[ "${lines[1]}" = "ok 1 empty" ] [ "${lines[1]}" = 'ok 1 empty' ]
[ "${lines[2]}" = "ok 2 passing" ] [ "${lines[2]}" = 'ok 2 passing' ]
[ "${lines[3]}" = "ok 3 input redirection" ] [ "${lines[3]}" = 'ok 3 input redirection' ]
[ "${lines[4]}" = "not ok 4 failing" ] [ "${lines[4]}" = 'not ok 4 failing' ]
[ "${lines[5]}" = "# (in test file $FIXTURE_ROOT/single_line.bats, line 9)" ] [ "${lines[5]}" = '# (command' ]
[ "${lines[6]}" = $'# `@test "failing" { false; }\'' ]
[ "${lines[7]}" = "# in test file $FIXTURE_ROOT/single_line.bats, line 9)" ]
} }