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

Print failed statement in backtrace

Update tests accordingly.
This commit is contained in:
Andrey Mazo 2014-06-28 23:16:32 +04:00
parent 64adaa02c3
commit bbaf0f50df
2 changed files with 52 additions and 26 deletions

View File

@ -120,11 +120,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
local failed_cmd_line
local failed_cmd_filename
local failed_cmd
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 -n "# ("
failed_cmd_line="${line}"
failed_cmd_filename="${filename}"
else else
line="$(bats_frame_line "$frame")" line="$(bats_frame_line "$frame")"
echo -n "# " echo -n "# "
@ -136,9 +149,15 @@ 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)"
# 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 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,29 @@ 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]}" = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ]
[ "${lines[3]}" = "# \`false' failed" ]
} }
@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]}" = "# (in test file $FIXTURE_ROOT/failing_and_passing.bats, line 2)" ]
[ ${lines[3]} = "ok 2 a passing test" ] [ "${lines[3]}" = "# \`false' failed" ]
[ "${lines[4]}" = '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]}" = "# (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[3]}" = "# in test file $FIXTURE_ROOT/failing_helper.bats, line 5)" ]
[ "${lines[4]}" = "# \`false' failed" ]
} }
@test "test environments are isolated" { @test "test environments are isolated" {
@ -89,22 +92,25 @@ 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]}" = "# (from function \`setup' in test file $FIXTURE_ROOT/failing_setup.bats, line 2)" ]
[ "${lines[3]}" = "# \`false' failed" ]
} }
@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]}" = "# (from function \`teardown' in test file $FIXTURE_ROOT/failing_teardown.bats, line 2)" ]
[ "${lines[3]}" = "# \`false' failed" ]
} }
@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]}" = "# (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" { @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" { @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[6]}" = '# failure stdout 1' ]
[ "${lines[6]}" = "# failure stdout 2" ] [ "${lines[7]}" = '# failure stdout 2' ]
[ "${lines[9]}" = "# failure stderr" ] [ "${lines[11]}" = '# failure stderr' ]
} }
@test "-c prints the number of tests" { @test "-c prints the number of tests" {
@ -172,10 +178,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[5]}" = 'begin 2 a passing test' ]
[ "${lines[5]}" = "ok 2 a passing test" ] [ "${lines[6]}" = 'ok 2 a passing test' ]
} }
@test "pretty and tap formats" { @test "pretty and tap formats" {
@ -200,9 +206,10 @@ 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]}" = "# (in test file $FIXTURE_ROOT/single_line.bats, line 9)" ]
[ "${lines[6]}" = $'# `@test "failing" { false; }\' failed' ]
} }