1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-11-17 03:32:27 +01:00

Refactor the stack trace-capturing debug trap

This commit is contained in:
Sam Stephenson 2014-06-02 19:28:31 -05:00
parent bd23c3831c
commit f193ddbe4e

View File

@ -91,20 +91,18 @@ bats_test_function() {
} }
bats_capture_stack_trace() { bats_capture_stack_trace() {
if [ "$BASH_SOURCE" != "$1" ]; then BATS_PREVIOUS_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" )
BATS_STACK_TRACE=() BATS_CURRENT_STACK_TRACE=()
BATS_LINE_NUMBER="$BATS_LINE_NUMBER_"
BATS_LINE_NUMBER_="$2"
local test_pattern=" $BATS_TEST_NAME $BATS_TEST_SOURCE" local test_pattern=" $BATS_TEST_NAME $BATS_TEST_SOURCE"
local setup_pattern=" setup $BATS_TEST_SOURCE" local setup_pattern=" setup $BATS_TEST_SOURCE"
local teardown_pattern=" teardown $BATS_TEST_SOURCE" local teardown_pattern=" teardown $BATS_TEST_SOURCE"
local index=0
local frame local frame
local index=1
while frame="$(caller "$index")"; do while frame="$(caller "$index")"; do
BATS_STACK_TRACE[$index]="$frame" BATS_CURRENT_STACK_TRACE["${#BATS_CURRENT_STACK_TRACE[@]}"]="$frame"
if [[ "$frame" = *"$test_pattern" || \ if [[ "$frame" = *"$test_pattern" || \
"$frame" = *"$setup_pattern" || \ "$frame" = *"$setup_pattern" || \
"$frame" = *"$teardown_pattern" ]]; then "$frame" = *"$teardown_pattern" ]]; then
@ -113,20 +111,22 @@ bats_capture_stack_trace() {
let index+=1 let index+=1
fi fi
done done
fi
BATS_SOURCE="$(bats_frame_filename "${BATS_CURRENT_STACK_TRACE[0]}")"
BATS_LINENO="$(bats_frame_lineno "${BATS_CURRENT_STACK_TRACE[0]}")"
} }
bats_print_stack_trace() { bats_print_stack_trace() {
local frame
local lineno
local index=1 local index=1
local count="${#BATS_STACK_TRACE[@]}" local count="${#@}"
local line
for frame in "${BATS_STACK_TRACE[@]}"; do for frame in "$@"; do
lineno="$(bats_frame_lineno "$frame")"
if [ $index -eq 1 ]; then if [ $index -eq 1 ]; then
line="$BATS_LINE_NUMBER"
echo -n "# (" echo -n "# ("
else else
line="$(bats_frame_line "$frame")"
echo -n "# " echo -n "# "
fi fi
@ -136,20 +136,20 @@ bats_print_stack_trace() {
fi fi
if [ $index -eq $count ]; then if [ $index -eq $count ]; then
echo "in test file $BATS_TEST_FILENAME, line $line)" echo "in test file $BATS_TEST_FILENAME, line $lineno)"
else else
local filename="$(bats_frame_filename "$frame")" local filename="$(bats_frame_filename "$frame")"
echo "in file $filename, line $line," echo "in file $filename, line $lineno,"
fi fi
let index+=1 let index+=1
done done
} }
bats_frame_line() { bats_frame_lineno() {
local frame="$1" local frame="$1"
local line="${frame%% *}" local lineno="${frame%% *}"
echo "$line" echo "$lineno"
} }
bats_frame_function() { bats_frame_function() {
@ -171,16 +171,23 @@ bats_frame_filename() {
fi fi
} }
bats_debug_trap() {
if [ "$BASH_SOURCE" != "$1" ]; then
bats_capture_stack_trace
fi
}
bats_error_trap() { bats_error_trap() {
BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" )
trap - debug trap - debug
} }
bats_teardown_trap() { bats_teardown_trap() {
trap bats_exit_trap exit trap "bats_exit_trap" exit
if teardown >>"$BATS_OUT" 2>&1; then if teardown >>"$BATS_OUT" 2>&1; then
BATS_TEARDOWN_COMPLETED=1 BATS_TEARDOWN_COMPLETED=1
elif [ -n "$BATS_TEST_COMPLETED" ]; then elif [ -n "$BATS_TEST_COMPLETED" ]; then
BATS_LINE_NUMBER="$BATS_LINE_NUMBER_" BATS_ERROR_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" )
fi fi
bats_exit_trap bats_exit_trap
} }
@ -200,7 +207,7 @@ bats_exit_trap() {
if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
bats_print_stack_trace >&3 bats_print_stack_trace "${BATS_ERROR_STACK_TRACE[@]}" >&3
sed -e "s/^/# /" < "$BATS_OUT" >&3 sed -e "s/^/# /" < "$BATS_OUT" >&3
status=1 status=1
else else
@ -234,7 +241,7 @@ bats_perform_test() {
BATS_TEST_COMPLETED="" BATS_TEST_COMPLETED=""
BATS_TEARDOWN_COMPLETED="" BATS_TEARDOWN_COMPLETED=""
trap "bats_capture_stack_trace \"\$BASH_SOURCE\" \$LINENO" debug trap "bats_debug_trap \"\$BASH_SOURCE\"" debug
trap "bats_error_trap" err trap "bats_error_trap" err
trap "bats_teardown_trap" exit trap "bats_teardown_trap" exit
"$BATS_TEST_NAME" >>"$BATS_OUT" 2>&1 "$BATS_TEST_NAME" >>"$BATS_OUT" 2>&1
@ -259,7 +266,7 @@ BATS_OUT="${BATS_TMPNAME}.out"
bats_preprocess_source() { bats_preprocess_source() {
BATS_TEST_SOURCE="${BATS_TMPNAME}.src" BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
{ tr -d '\r' < "$BATS_TEST_FILENAME"; echo; } | bats-preprocess > "$BATS_TEST_SOURCE" { tr -d '\r' < "$BATS_TEST_FILENAME"; echo; } | bats-preprocess > "$BATS_TEST_SOURCE"
trap bats_cleanup_preprocessed_source err exit trap "bats_cleanup_preprocessed_source" err exit
trap "bats_cleanup_preprocessed_source; exit 1" int trap "bats_cleanup_preprocessed_source; exit 1" int
} }