Skip to content

Commit 662f9cf

Browse files
dschogitster
authored andcommitted
tests: when run in Bash, annotate test failures with file name/line number
When a test fails, it is nice to see where the corresponding code lives in the worktree. Sadly, it seems that only Bash allows us to infer this information. Let's do it when we detect that we're running in a Bash. This will come in handy in the next commit, where we teach the GitHub Actions workflow to annotate failed test runs with this information. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Đoàn Trần Công Danh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6081d38 commit 662f9cf

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

t/test-lib.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,18 @@ die () {
642642
fi
643643
}
644644

645+
file_lineno () {
646+
test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0
647+
local i
648+
for i in ${!BASH_SOURCE[*]}
649+
do
650+
case $i,"${BASH_SOURCE[$i]##*/}" in
651+
0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;;
652+
*,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;;
653+
esac
654+
done
655+
}
656+
645657
GIT_EXIT_OK=
646658
trap 'die' EXIT
647659
# Disable '-x' tracing, because with some shells, notably dash, it
@@ -687,7 +699,7 @@ test_failure_ () {
687699
write_junit_xml_testcase "$1" " $junit_insert"
688700
fi
689701
test_failure=$(($test_failure + 1))
690-
say_color error "not ok $test_count - $1"
702+
say_color error "$(file_lineno error)not ok $test_count - $1"
691703
shift
692704
printf '%s\n' "$*" | sed -e 's/^/# /'
693705
test "$immediate" = "" || { finalize_junit_xml; GIT_EXIT_OK=t; exit 1; }

0 commit comments

Comments
 (0)