diff --git a/lib/internal/use b/lib/internal/use index 6c774fb..6ada2fb 100644 --- a/lib/internal/use +++ b/lib/internal/use @@ -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 diff --git a/tests/modules/use.bats b/tests/modules/use.bats index f568d3c..cef5d25 100644 --- a/tests/modules/use.bats +++ b/tests/modules/use.bats @@ -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" { @@ -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[*]}" }