Skip to content

Commit 18f686c

Browse files
committed
use: Extract import_module for proper plugin scope
This concludes the refactoring of `_GO_USE_MODULES` to provide correct `@go` command script scope for plugin modules. It is germane to #120. Whereas #136 implemented correctly-scoped lookup of other plugin modules, this allows any module, plugin or otherwise, to invoke its own command scripts or plugin command scripts via `@go`.
1 parent 41ebe94 commit 18f686c

File tree

3 files changed

+125
-18
lines changed

3 files changed

+125
-18
lines changed

lib/internal/use

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ _@go.find_module() {
101101
fi
102102
}
103103

104+
_@go.import_module() {
105+
if [[ "${__go_module_file#$_GO_SCRIPTS_DIR}" =~ /plugins/[^/]+/lib/ ]]; then
106+
local _GO_SCRIPTS_DIR="${__go_module_file%/lib/*}/bin"
107+
local _GO_ROOTDIR="${_GO_SCRIPTS_DIR%/bin}"
108+
local _GO_PLUGINS_PATHS=()
109+
local _GO_SEARCH_PATHS=()
110+
fi
111+
112+
# Prevent self- and circular importing by registering info before sourcing.
113+
_GO_IMPORTED_MODULES+=("$__go_module_name")
114+
_GO_IMPORTED_MODULE_FILES+=("$__go_module_file")
115+
_GO_IMPORTED_MODULE_CALLERS+=("$current_caller")
116+
. "$__go_module_file"
117+
}
118+
104119
_@go.use_modules() {
105120
local __go_module_name
106121
local __go_module_file
@@ -110,14 +125,6 @@ _@go.use_modules() {
110125
local current_caller="${BASH_SOURCE[2]}:${BASH_LINENO[1]} ${FUNCNAME[2]}"
111126
local prev_caller
112127

113-
# If a script imports a plugin module, and that module (`current_caller`)
114-
# tries to import another module from the same plugin, this block will adjust
115-
# the search parameters accordingly.
116-
if [[ "$current_caller" =~ ^$_GO_PLUGINS_DIR/.*/lib/ ]]; then
117-
local _GO_SCRIPTS_DIR="${current_caller%/lib/*}/bin"
118-
local _GO_ROOTDIR="${_GO_SCRIPTS_DIR%/bin}"
119-
fi
120-
121128
for __go_module_name in "$@"; do
122129
if ! _@go.find_module; then
123130
@go.printf 'ERROR: Module %s not found at:\n' "$__go_module_name" >&2
@@ -159,12 +166,7 @@ _@go.use_modules() {
159166
((++module_index))
160167
done
161168

162-
# Prevent self- and circular importing by registering info before sourcing.
163-
_GO_IMPORTED_MODULES+=("$__go_module_name")
164-
_GO_IMPORTED_MODULE_FILES+=("$__go_module_file")
165-
_GO_IMPORTED_MODULE_CALLERS+=("$current_caller")
166-
167-
if ! . "$__go_module_file"; then
169+
if ! _@go.import_module; then
168170
@go.printf 'ERROR: Failed to import %s module from %s at:\n' \
169171
"$__go_module_name" "$__go_module_file" >&2
170172
@go.print_stack_trace 1 >&2

tests/file/open-or-dup.bats

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ load ../environment
44
load "$_GO_CORE_DIR/lib/testing/stack-trace"
55

66
FILE_PATH="$TEST_GO_ROOTDIR/hello.txt"
7-
GO_USE_MODULES_STACK_ITEM="$(@go.stack_trace_item "$_GO_USE_MODULES" source \
8-
'_@go.use_modules "$@"')"
7+
GO_USE_MODULES_STACK_ITEMS=(
8+
"$(@go.stack_trace_item "$_GO_USE_MODULES" '_@go.use_modules' \
9+
' if ! _@go.import_module; then')"
10+
"$(@go.stack_trace_item "$_GO_USE_MODULES" source '_@go.use_modules "$@"')")
911

1012
setup() {
1113
test_filter
@@ -119,7 +121,7 @@ create_file_open_test_go_script() {
119121

120122
local expected=(
121123
'_GO_MAX_FILE_DESCRIPTORS is "foobar", must be a number greater than 3.'
122-
"$GO_USE_MODULES_STACK_ITEM"
124+
"${GO_USE_MODULES_STACK_ITEMS[@]}"
123125
" $TEST_GO_SCRIPT:3 main")
124126
assert_failure "${expected[@]}"
125127
}
@@ -130,7 +132,7 @@ create_file_open_test_go_script() {
130132

131133
local expected=(
132134
'_GO_MAX_FILE_DESCRIPTORS is "3", must be a number greater than 3.'
133-
"$GO_USE_MODULES_STACK_ITEM"
135+
"${GO_USE_MODULES_STACK_ITEMS[@]}"
134136
" $TEST_GO_SCRIPT:3 main")
135137
assert_failure "${expected[@]}"
136138
}

tests/modules/use/plugins.bats

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,44 @@ teardown() {
5454
assert_success "$TEST_GO_PLUGINS_DIR/$module_path" 'foo/foo' 'bar/bar'
5555
}
5656

57+
@test "$SUITE: @go plugins from own module shows plugin's own plugin" {
58+
@go.create_test_command_script "plugins/foo/lib/foo" "@go plugins --paths"
59+
@go.create_test_command_script 'plugins/foo/bin/plugins/bar/bin/bar' \
60+
"$PRINT_SOURCE"
61+
62+
run "$TEST_GO_SCRIPT" 'foo/foo'
63+
assert_success
64+
65+
# @go plugins will import additional modules, so only check the first
66+
assert_line_equals 0 'bar bin/plugins/bar/bin/bar'
67+
assert_line_equals 1 'foo/foo'
68+
}
69+
70+
@test "$SUITE: plugin's own module can execute own command script" {
71+
@go.create_test_command_script "plugins/foo/lib/foo" "@go bar"
72+
@go.create_test_command_script 'plugins/foo/bin/bar' "$PRINT_SOURCE"
73+
74+
run "$TEST_GO_SCRIPT" 'foo/foo'
75+
assert_success "$TEST_GO_PLUGINS_DIR/foo/bin/bar" 'foo/foo'
76+
}
77+
78+
@test "$SUITE: plugin's own module can execute own plugin script" {
79+
local plugin_path='foo/bin/plugins/bar/bin/bar'
80+
@go.create_test_command_script "plugins/foo/lib/foo" "@go bar"
81+
@go.create_test_command_script "plugins/$plugin_path" "$PRINT_SOURCE"
82+
83+
run "$TEST_GO_SCRIPT" 'foo/foo'
84+
assert_success "$TEST_GO_PLUGINS_DIR/$plugin_path" 'foo/foo'
85+
}
86+
87+
@test "$SUITE: plugin's own module can execute top-level plugin script" {
88+
@go.create_test_command_script "plugins/foo/lib/foo" "@go bar"
89+
@go.create_test_command_script "plugins/bar/bin/bar" "$PRINT_SOURCE"
90+
91+
run "$TEST_GO_SCRIPT" 'foo/foo'
92+
assert_success "$TEST_GO_PLUGINS_DIR/bar/bin/bar" 'foo/foo'
93+
}
94+
5795
@test "$SUITE: nested plugin imports own internal module" {
5896
local module_path='foo/bin/plugins/bar/bin/lib/bar-2'
5997
@go.create_test_command_script 'plugins/foo/lib/foo' \
@@ -147,6 +185,71 @@ teardown() {
147185
'foo/foo' 'bar/bar' 'baz/baz'
148186
}
149187

188+
@test "$SUITE: @go plugins from own module shows nested plugin's plugins" {
189+
local plugin_path='foo/bin/plugins/bar/bin/plugins/baz/bin/baz'
190+
@go.create_test_command_script 'plugins/foo/lib/foo' '@go bar'
191+
@go.create_test_command_script 'plugins/foo/bin/foo' "$PRINT_SOURCE"
192+
@go.create_test_command_script 'plugins/foo/bin/plugins/bar/bin/bar' \
193+
'. "$_GO_USE_MODULES" bar/bar'
194+
@go.create_test_command_script 'plugins/foo/bin/plugins/bar/lib/bar' \
195+
'@go plugins --paths'
196+
@go.create_test_command_script "plugins/$plugin_path" "$PRINT_SOURCE"
197+
198+
run "$TEST_GO_SCRIPT" 'foo/foo'
199+
200+
# Note that the parent plugin command script is included in the output.
201+
# @go plugins will import additional modules, so only check the first.
202+
assert_success
203+
assert_line_equals 0 'baz bin/plugins/baz/bin/baz'
204+
assert_line_equals 1 "foo $TEST_GO_PLUGINS_DIR/foo/bin/foo"
205+
assert_line_equals 2 'foo/foo'
206+
assert_line_equals 3 'bar/bar'
207+
}
208+
209+
@test "$SUITE: nested plugin's own module can execute own command script" {
210+
local plugin_path='foo/bin/plugins/bar/bin/plugins/baz/bin/baz'
211+
@go.create_test_command_script 'plugins/foo/lib/foo' \
212+
'. "$_GO_USE_MODULES" bar/bar'
213+
@go.create_test_command_script 'plugins/foo/bin/plugins/bar/lib/bar' '@go baz'
214+
@go.create_test_command_script "plugins/foo/bin/plugins/bar/bin/baz" \
215+
"$PRINT_SOURCE"
216+
217+
run "$TEST_GO_SCRIPT" 'foo/foo'
218+
assert_success "$TEST_GO_PLUGINS_DIR/foo/bin/plugins/bar/bin/baz" \
219+
'foo/foo' 'bar/bar'
220+
}
221+
222+
@test "$SUITE: nested plugin's own module can execute own plugin script" {
223+
local plugin_path='foo/bin/plugins/bar/bin/plugins/baz/bin/baz'
224+
@go.create_test_command_script 'plugins/foo/lib/foo' \
225+
'. "$_GO_USE_MODULES" bar/bar'
226+
@go.create_test_command_script 'plugins/foo/bin/plugins/bar/lib/bar' '@go baz'
227+
@go.create_test_command_script "plugins/$plugin_path" "$PRINT_SOURCE"
228+
229+
run "$TEST_GO_SCRIPT" 'foo/foo'
230+
assert_success "$TEST_GO_PLUGINS_DIR/$plugin_path" 'foo/foo' 'bar/bar'
231+
}
232+
233+
@test "$SUITE: nested plugin's own module can execute parent command script" {
234+
@go.create_test_command_script 'plugins/foo/lib/foo' \
235+
'. "$_GO_USE_MODULES" bar/bar'
236+
@go.create_test_command_script 'plugins/foo/bin/baz' "$PRINT_SOURCE"
237+
@go.create_test_command_script 'plugins/foo/bin/plugins/bar/lib/bar' '@go baz'
238+
239+
run "$TEST_GO_SCRIPT" 'foo/foo'
240+
assert_success "$TEST_GO_PLUGINS_DIR/foo/bin/baz" 'foo/foo' 'bar/bar'
241+
}
242+
243+
@test "$SUITE: nested plugin's own module can execute top-level plugin script" {
244+
@go.create_test_command_script 'plugins/foo/lib/foo' \
245+
'. "$_GO_USE_MODULES" bar/bar'
246+
@go.create_test_command_script 'plugins/foo/bin/plugins/bar/lib/bar' '@go baz'
247+
@go.create_test_command_script "plugins/baz/bin/baz" "$PRINT_SOURCE"
248+
249+
run "$TEST_GO_SCRIPT" 'foo/foo'
250+
assert_success "$TEST_GO_PLUGINS_DIR/baz/bin/baz" 'foo/foo' 'bar/bar'
251+
}
252+
150253
@test "$SUITE: module collision produces warning message, top level first" {
151254
local top_module_path='baz/lib/baz'
152255
local nested_module_path='foo/bin/plugins/bar/bin/plugins/baz/lib/baz'

0 commit comments

Comments
 (0)