Skip to content

Commit 30790c9

Browse files
committed
use: Show stack trace when an import fails
1 parent 8563f4d commit 30790c9

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

lib/internal/use

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,17 @@ for __go_module_name in "$@"; do
9090
__go_module_file="$_GO_SCRIPTS_DIR/lib/$__go_module_name"
9191

9292
if [[ ! -f "$__go_module_file" ]]; then
93-
@go.printf "ERROR: Unknown module: $__go_module_name\n" >&2
93+
@go.printf 'ERROR: Module %s not found at:\n' "$__go_module_name" >&2
94+
@go.print_stack_trace omit_caller >&2
9495
exit 1
9596
fi
9697
fi
9798
fi
9899

99100
if ! . "$__go_module_file"; then
100-
@go.printf "ERROR: Module import failed for: $__go_module_file\n" >&2
101+
@go.printf 'ERROR: Failed to import %s module from %s at:\n' \
102+
"$__go_module_name" "$__go_module_file" >&2
103+
@go.print_stack_trace omit_caller >&2
101104
exit 1
102105
fi
103106
done

tests/modules/use.bats

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ teardown() {
4545

4646
@test "$SUITE: error if nonexistent module specified" {
4747
run "$TEST_GO_SCRIPT" 'bogus-test-module'
48-
assert_failure 'ERROR: Unknown module: bogus-test-module'
48+
49+
local expected=('ERROR: Module bogus-test-module not found at:'
50+
" $TEST_GO_SCRIPT:3 main")
51+
local IFS=$'\n'
52+
assert_failure "${expected[*]}"
4953
}
5054

5155
@test "$SUITE: import modules successfully" {
@@ -73,12 +77,33 @@ teardown() {
7377
}
7478

7579
@test "$SUITE: error if module contains errors" {
76-
echo "This is a totally broken module." >> "${TEST_MODULES[1]}"
80+
local module="${IMPORTS[1]}"
81+
local module_file="${TEST_MODULES[2]}"
82+
83+
echo "This is a totally broken module." > "$module_file"
84+
run "$TEST_GO_SCRIPT" "${IMPORTS[@]}"
85+
86+
local expected=("${IMPORTS[0]##*/} loaded"
87+
"$module_file: line 1: This: command not found"
88+
"ERROR: Failed to import $module module from $module_file at:"
89+
" $TEST_GO_SCRIPT:3 main")
90+
local IFS=$'\n'
91+
assert_failure "${expected[*]}"
92+
}
93+
94+
@test "$SUITE: error if module returns an error" {
95+
local module="${IMPORTS[1]}"
96+
local module_file="${TEST_MODULES[2]}"
97+
local error_message='These violent delights have violent ends...'
98+
99+
echo "echo '$error_message' >&2" > "$module_file"
100+
echo "return 1" >> "$module_file"
77101
run "$TEST_GO_SCRIPT" "${IMPORTS[@]}"
78102

79103
local expected=("${IMPORTS[0]##*/} loaded"
80-
"${TEST_MODULES[1]}: line 2: This: command not found"
81-
"ERROR: Module import failed for: ${TEST_MODULES[1]}")
104+
"$error_message"
105+
"ERROR: Failed to import $module module from $module_file at:"
106+
" $TEST_GO_SCRIPT:3 main")
82107
local IFS=$'\n'
83108
assert_failure "${expected[*]}"
84109
}

0 commit comments

Comments
 (0)