Skip to content

Commit

Permalink
Get bats_print_stack_trace() working
Browse files Browse the repository at this point in the history
Line numbers and command excerpts were wrong, because the
stack-tracing magic has exceptions for tracing inside
BATS helper files. Added a workaround for this, such
that xrun and xpect will log the calling function.
Added excerpt and line-number tests.

Signed-off-by: Ed Santiago <santiago@redhat.com>
  • Loading branch information
edsantiago committed Nov 8, 2020
1 parent 261d49b commit 8102a0f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
32 changes: 22 additions & 10 deletions lib/bats-core/tracing.bash
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env bash

bats_capture_stack_trace() {
local internal_offset=${1:-0}
local test_file
local funcname
local i

BATS_STACK_TRACE=()

for ((i = 2; i != ${#FUNCNAME[@]}; ++i)); do
for ((i = $((2 + internal_offset)); i != ${#FUNCNAME[@]}; ++i)); do
# Use BATS_TEST_SOURCE if necessary to work around Bash < 4.4 bug whereby
# calling an exported function erases the test file's BASH_SOURCE entry.
test_file="${BASH_SOURCE[$i]:-$BATS_TEST_SOURCE}"
Expand Down Expand Up @@ -119,15 +120,26 @@ bats_trim_filename() {

bats_debug_trap() {
# don't update the trace within library functions or we get backtraces from inside traps
if [[ "$1" != $BATS_ROOT/lib/* && "$1" != $BATS_ROOT/libexec/* ]]; then
# The last entry in the stack trace is not useful when en error occured:
# It is either duplicated (kinda correct) or has wrong line number (Bash < 4.4)
# Therefore we capture the stacktrace but use it only after the next debug
# trap fired.
# Expansion is required for empty arrays which otherwise error
BATS_CURRENT_STACK_TRACE=("${BATS_STACK_TRACE[@]+"${BATS_STACK_TRACE[@]}"}")
bats_capture_stack_trace
fi
local stack_trace_offset=
if [[ "$1" == $BATS_ROOT/lib/* || "$1" == $BATS_ROOT/libexec/* ]]; then
if [[ "${FUNCNAME[1]}" != "xrun" && "${FUNCNAME[1]}" != "xpect" ]]; then
return
fi

# We are in xrun or xpect, which are internal functions that
# can throw test failures. Before they do, we need to make sure
# that we log the calling function (presumably in user's
# .bats file) so we can emit proper line numbers in messages.
stack_trace_offset=1
fi

# The last entry in the stack trace is not useful when en error occured:
# It is either duplicated (kinda correct) or has wrong line number (Bash < 4.4)
# Therefore we capture the stacktrace but use it only after the next debug
# trap fired.
# Expansion is required for empty arrays which otherwise error
BATS_CURRENT_STACK_TRACE=("${BATS_STACK_TRACE[@]+"${BATS_STACK_TRACE[@]}"}")
bats_capture_stack_trace $stack_trace_offset
}

# For some versions of Bash, the `ERR` trap may not always fire for every
Expand Down
13 changes: 9 additions & 4 deletions test/xpect.bats
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ function _run_test() {

@test "run: unexpected fail" {
local -a commands=("xrun false")
_run_test "\[ rc=1 \(\*\* EXPECTED 0 \*\*\) \]" \
_run_test "\(in test file .*, line 2\)" \
"\`xrun false' failed" \
"\[ rc=1 \(\*\* EXPECTED 0 \*\*\) \]" \
"/vvvvvvvvvvvvvv" \
"\| FAIL: exit code is 1; expected 0"
}
Expand All @@ -97,7 +99,8 @@ function _run_test() {
'xrun echo abc'
'xpect "cde"'
)
_run_test "\`xrun echo abc' failed" \
_run_test "\(in test file .*\.bats, line 3\)" \
"\`xpect \"cde\"' failed" \
"# run> echo abc" \
"# abc" \
"#\| FAIL: output from 'echo abc'" \
Expand All @@ -107,10 +110,12 @@ function _run_test() {

@test "xpect: mismatch, substring" {
local -a commands=(
'true'
'xrun echo abcdefg'
'xpect "$output" =~ "ghi"'
)
_run_test "\`xrun echo abcdefg' failed" \
_run_test "\(in test file .*\.bats, line 4\)" \
"\`xpect \"\\\$output\" =~ \"ghi\"' failed" \
"# run> echo abcdefg" \
"# abcdefg" \
"#\| FAIL: output from 'echo abcdefg'" \
Expand All @@ -132,7 +137,7 @@ function _run_test() {
'xrun echo abc'
'xpect "cde" "my test name"'
)
_run_test "\`xrun echo abc' failed" \
_run_test "\`xpect \"cde\" \"my test name\"' failed" \
"run> echo abc" \
"# abc"\
"#\| FAIL: my test name" \
Expand Down

0 comments on commit 8102a0f

Please sign in to comment.