Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Print test file path and line number after a failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
sstephenson committed May 23, 2013
1 parent 1be500e commit bc72b85
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
32 changes: 27 additions & 5 deletions libexec/bats-exec-test
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash
set -e
set -E
set -T

BATS_COUNT_ONLY=""
if [ "$1" = "-c" ]; then
Expand Down Expand Up @@ -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() {
Expand All @@ -51,16 +59,26 @@ teardown() {

bats_test_info() {
BATS_TEST_DESCRIPTION="$1"
BATS_LINE_NUMBER="$2"
}

bats_test_function() {
local test_name="$1"
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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ 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" {
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]} = "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" {
Expand Down Expand Up @@ -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" {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/bats/failing.bats
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@test "a failing test" {
true
true
false
}

0 comments on commit bc72b85

Please sign in to comment.