Skip to content

Commit

Permalink
use: Show stack trace when an import fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mbland committed Dec 13, 2016
1 parent 8563f4d commit 30790c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
7 changes: 5 additions & 2 deletions lib/internal/use
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ for __go_module_name in "$@"; do
__go_module_file="$_GO_SCRIPTS_DIR/lib/$__go_module_name"

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

if ! . "$__go_module_file"; then
@go.printf "ERROR: Module import failed for: $__go_module_file\n" >&2
@go.printf 'ERROR: Failed to import %s module from %s at:\n' \
"$__go_module_name" "$__go_module_file" >&2
@go.print_stack_trace omit_caller >&2
exit 1
fi
done
Expand Down
33 changes: 29 additions & 4 deletions tests/modules/use.bats
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ teardown() {

@test "$SUITE: error if nonexistent module specified" {
run "$TEST_GO_SCRIPT" 'bogus-test-module'
assert_failure 'ERROR: Unknown module: bogus-test-module'

local expected=('ERROR: Module bogus-test-module not found at:'
" $TEST_GO_SCRIPT:3 main")
local IFS=$'\n'
assert_failure "${expected[*]}"
}

@test "$SUITE: import modules successfully" {
Expand Down Expand Up @@ -73,12 +77,33 @@ teardown() {
}

@test "$SUITE: error if module contains errors" {
echo "This is a totally broken module." >> "${TEST_MODULES[1]}"
local module="${IMPORTS[1]}"
local module_file="${TEST_MODULES[2]}"

echo "This is a totally broken module." > "$module_file"
run "$TEST_GO_SCRIPT" "${IMPORTS[@]}"

local expected=("${IMPORTS[0]##*/} loaded"
"$module_file: line 1: This: command not found"
"ERROR: Failed to import $module module from $module_file at:"
" $TEST_GO_SCRIPT:3 main")
local IFS=$'\n'
assert_failure "${expected[*]}"
}

@test "$SUITE: error if module returns an error" {
local module="${IMPORTS[1]}"
local module_file="${TEST_MODULES[2]}"
local error_message='These violent delights have violent ends...'

echo "echo '$error_message' >&2" > "$module_file"
echo "return 1" >> "$module_file"
run "$TEST_GO_SCRIPT" "${IMPORTS[@]}"

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

0 comments on commit 30790c9

Please sign in to comment.