Skip to content

Commit cd57da0

Browse files
committed
print-stack-trace: Add go-core stack test helper
Since other tests (specifically the updated `@go.log` tests) will need to compare the stack trace output as well, decided a reusable helper function was in order rather than duplicating or contorting logic in the new tests.
1 parent 8424338 commit cd57da0

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

tests/core/helpers.bash

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#! /bin/bash
2+
#
3+
# Helper functions for core library tests
4+
5+
declare -x _GO_CORE_STACK_TRACE_COMPONENTS=()
6+
7+
set_go_core_stack_trace_components() {
8+
local go_core_file="$_GO_CORE_DIR/go-core.bash"
9+
local stack_item
10+
local IFS=$'\n'
11+
12+
if [[ "${#_GO_CORE_STACK_TRACE_COMPONENTS[@]}" -ne '0' ]]; then
13+
return
14+
fi
15+
16+
create_test_go_script '@go "$@"'
17+
create_test_command_script 'print-stack-trace' '@go.print_stack_trace'
18+
19+
for stack_item in $("$TEST_GO_SCRIPT" 'print-stack-trace'); do
20+
if [[ "$stack_item" =~ $go_core_file ]]; then
21+
_GO_CORE_STACK_TRACE_COMPONENTS+=("$stack_item")
22+
elif [[ "${#_GO_CORE_STACK_TRACE_COMPONENTS[@]}" -ne '0' ]]; then
23+
return
24+
fi
25+
done
26+
}

tests/core/print-stack-trace.bats

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! /usr/bin/env bats
22

33
load ../environment
4+
load helpers
45

56
teardown() {
67
remove_test_go_rootdir
@@ -88,15 +89,14 @@ teardown() {
8889
'bar_func'
8990

9091
run "$TEST_GO_SCRIPT" foo
91-
92-
local go_core_pattern="$_GO_CORE_DIR/go-core.bash:[0-9]+"
9392
assert_success
94-
assert_line_equals 0 " $TEST_GO_SCRIPTS_DIR/foo.d/bar:5 source"
95-
assert_line_matches 1 " $go_core_pattern _@go.run_command_script"
96-
assert_line_matches 2 " $go_core_pattern @go"
97-
assert_line_equals 3 " $TEST_GO_SCRIPTS_DIR/foo:3 foo_func"
98-
assert_line_equals 4 " $TEST_GO_SCRIPTS_DIR/foo:5 source"
99-
assert_line_matches 5 " $go_core_pattern _@go.run_command_script"
100-
assert_line_matches 6 " $go_core_pattern @go"
101-
assert_line_equals 7 " $TEST_GO_SCRIPT:3 main"
93+
set_go_core_stack_trace_components
94+
95+
local IFS=$'\n'
96+
assert_lines_equal " $TEST_GO_SCRIPTS_DIR/foo.d/bar:5 source" \
97+
"${_GO_CORE_STACK_TRACE_COMPONENTS[@]}" \
98+
" $TEST_GO_SCRIPTS_DIR/foo:3 foo_func" \
99+
" $TEST_GO_SCRIPTS_DIR/foo:5 source" \
100+
"${_GO_CORE_STACK_TRACE_COMPONENTS[@]}" \
101+
" $TEST_GO_SCRIPT:3 main"
102102
}

0 commit comments

Comments
 (0)