1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-12-25 14:09:43 +01:00

Print test file path and line number after a failing test

This commit is contained in:
Sam Stephenson 2013-05-23 12:47:17 -05:00
parent 1be500e4ff
commit bc72b85871
3 changed files with 35 additions and 9 deletions

View File

@ -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

View File

@ -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" {

View File

@ -1,3 +1,5 @@
@test "a failing test" {
true
true
false
}