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

Commit bc72b85

Browse files
committed
Print test file path and line number after a failing test
1 parent 1be500e commit bc72b85

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

libexec/bats-exec-test

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22
set -e
3+
set -E
4+
set -T
35

46
BATS_COUNT_ONLY=""
57
if [ "$1" = "-c" ]; then
@@ -32,13 +34,19 @@ load() {
3234
}
3335

3436
run() {
35-
local e
37+
local e E T
3638
[[ ! "$-" =~ e ]] || e=1
39+
[[ ! "$-" =~ E ]] || E=1
40+
[[ ! "$-" =~ T ]] || T=1
3741
set +e
42+
set +E
43+
set +T
3844
output="$("$@" 2>&1)"
3945
status="$?"
4046
IFS=$'\n' lines=($output)
4147
[ -z "$e" ] || set -e
48+
[ -z "$E" ] || set -E
49+
[ -z "$T" ] || set -T
4250
}
4351

4452
setup() {
@@ -51,16 +59,26 @@ teardown() {
5159

5260
bats_test_info() {
5361
BATS_TEST_DESCRIPTION="$1"
54-
BATS_LINE_NUMBER="$2"
5562
}
5663

5764
bats_test_function() {
5865
local test_name="$1"
5966
BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name"
6067
}
6168

69+
bats_debug_trap() {
70+
if [ "$BASH_SOURCE" != "$1" ]; then
71+
BATS_LINE_NUMBER="$BATS_LINE_NUMBER_"
72+
BATS_LINE_NUMBER_="$2"
73+
fi
74+
}
75+
76+
bats_error_trap() {
77+
trap - debug
78+
}
79+
6280
bats_teardown_trap() {
63-
trap bats_exit_trap err exit
81+
trap bats_exit_trap exit
6482
teardown >>"$BATS_OUT" 2>&1
6583
bats_exit_trap
6684
}
@@ -71,7 +89,8 @@ bats_exit_trap() {
7189

7290
if [ -z "$BATS_TEST_COMPLETED" ]; then
7391
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
74-
sed -e "s/^/ /" < "$BATS_OUT" >&3
92+
echo "# $BATS_TEST_FILENAME:$BATS_LINE_NUMBER" >&3
93+
sed -e "s/^/# /" < "$BATS_OUT" >&3
7594
status=1
7695
else
7796
echo "ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
@@ -103,7 +122,10 @@ bats_perform_test() {
103122
fi
104123

105124
BATS_TEST_COMPLETED=""
106-
trap bats_teardown_trap err exit
125+
BATS_ERROR_LINE=""
126+
trap "bats_debug_trap \"\$BASH_SOURCE\" \$LINENO" debug
127+
trap "bats_error_trap" err
128+
trap "bats_teardown_trap" exit
107129
{ setup
108130
"$BATS_TEST_NAME"
109131
} >>"$BATS_OUT" 2>&1

test/bats.bats

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ fixtures bats
3939
[ $status -eq 1 ]
4040
[ ${lines[0]} = "1..1" ]
4141
[ ${lines[1]} = "not ok 1 a failing test" ]
42+
[ ${lines[2]} = "# $FIXTURE_ROOT/failing.bats:4" ]
4243
}
4344

4445
@test "one failing and one passing test" {
4546
run bats "$FIXTURE_ROOT/failing_and_passing.bats"
4647
[ $status -eq 1 ]
4748
[ ${lines[0]} = "1..2" ]
4849
[ ${lines[1]} = "not ok 1 a failing test" ]
49-
[ ${lines[2]} = "ok 2 a passing test" ]
50+
[ ${lines[2]} = "# $FIXTURE_ROOT/failing_and_passing.bats:2" ]
51+
[ ${lines[3]} = "ok 2 a passing test" ]
5052
}
5153

5254
@test "test environments are isolated" {
@@ -83,9 +85,9 @@ fixtures bats
8385
@test "output is discarded for passing tests and printed for failing tests" {
8486
run bats "$FIXTURE_ROOT/output.bats"
8587
[ $status -eq 1 ]
86-
[ "${lines[4]}" = " failure stdout 1" ]
87-
[ "${lines[5]}" = " failure stdout 2" ]
88-
[ "${lines[7]}" = " failure stderr" ]
88+
[ "${lines[5]}" = "# failure stdout 1" ]
89+
[ "${lines[6]}" = "# failure stdout 2" ]
90+
[ "${lines[9]}" = "# failure stderr" ]
8991
}
9092

9193
@test "-c prints the number of tests" {

test/fixtures/bats/failing.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
@test "a failing test" {
2+
true
3+
true
24
false
35
}

0 commit comments

Comments
 (0)