Skip to content

Commit 1fc08b1

Browse files
committed
core: Handle pathological cases of /plugins/ paths
Command scripts are invoked correctly even when `/plugins/` appears in `_GO_ROOTDIR`, in `_GO_SCRIPTS_DIR`, in the command script path relative to `_GO_SCRIPTS_DIR`, or in one or more combined.
1 parent 6af2900 commit 1fc08b1

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

go-core.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
259259
return 1
260260
fi
261261

262-
if [[ "${__go_cmd_path#$_GO_SCRIPTS_DIR}" =~ /plugins/ ]]; then
262+
if [[ "${__go_cmd_path#$_GO_SCRIPTS_DIR}" =~ /plugins/[^/]+/bin/ ]]; then
263263
_@go.run_plugin_command_script "$__go_cmd_path" "${__go_argv[@]}"
264264
else
265265
_@go.run_command_script "$__go_cmd_path" "${__go_argv[@]}"

lib/internal/path

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ _@go.set_search_paths() {
1313
# A plugin's own local plugin paths will appear before inherited ones. If
1414
# there is a version incompatibility issue with other installed plugins, this
1515
# allows a plugin's preferred version to take precedence.
16-
while true; do
16+
while [[ "$plugins_dir" =~ ^$_GO_PLUGINS_DIR ]]; do
1717
plugin_paths=("$plugins_dir"/*/bin)
1818
if [[ "${plugin_paths[0]}" != "$plugins_dir/*/bin" ]]; then
1919
_GO_PLUGINS_PATHS+=("${plugin_paths[@]}")

tests/core/plugin-scope-execution.bats

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,94 @@ teardown() {
141141
assert_success "$TEST_GO_SCRIPTS_DIR/plugins/baz/bin/baz"
142142
}
143143

144-
@test "$SUITE: nested plugins dir doesn't leak to sibling plugin" {
145-
@go.create_test_command_script 'plugins/foo/bin/foo' '@go bar'
146-
@go.create_test_command_script 'plugins/foo/bin/plugins/bar/bin/bar' '@go baz'
147-
@go.create_test_command_script 'plugins/foo/bin/plugins/quux/bin/quux' \
144+
@test "$SUITE: plugin doesn't leak own plugins to sibling plugin" {
145+
@go.create_test_command_script 'plugins/foo/bin/foo' '@go baz'
146+
@go.create_test_command_script 'plugins/bar/bin/bar' '@go foo'
147+
@go.create_test_command_script 'plugins/bar/bin/plugins/baz/bin/baz' \
148+
"$PRINT_SOURCE"
149+
150+
run "$TEST_GO_SCRIPT" 'bar'
151+
assert_failure
152+
assert_line_equals 0 'Unknown command: baz'
153+
}
154+
155+
@test "$SUITE: plugin doesn't leak own plugins to parent plugin" {
156+
@go.create_test_command_script 'plugins/foo/bin/foo' '@go baz'
157+
@go.create_test_command_script 'plugins/foo/bin/bar' '@go quux'
158+
@go.create_test_command_script 'plugins/foo/bin/plugins/baz/bin/baz' '@go bar'
159+
@go.create_test_command_script \
160+
'plugins/foo/bin/plugins/baz/bin/plugins/quux/bin/quux' \
148161
"$PRINT_SOURCE"
149-
@go.create_test_command_script 'plugins/baz/bin/baz' '@go quux'
150162

151163
run "$TEST_GO_SCRIPT" 'foo'
152164
assert_failure
153165
assert_line_equals 0 'Unknown command: quux'
154166
}
167+
168+
@test "$SUITE: non-plugin script when _GO_SCRIPTS_DIR contains /plugins/" {
169+
create_bats_test_script 'go' \
170+
". '$_GO_CORE_DIR/go-core.bash' 'plugins'" \
171+
'@go "$@"'
172+
create_bats_test_script 'plugins/foo' \
173+
'@go.print_stack_trace 1'
174+
175+
run "$TEST_GO_SCRIPT" 'foo'
176+
assert_success
177+
assert_line_matches 0 \
178+
" $_GO_CORE_DIR/go-core.bash:[0-9]+ _@go.run_command_script"
179+
fail_if line_matches 1 \
180+
" $_GO_CORE_DIR/go-core.bash:[1-9]+ _@go.run_plugin_command_script"
181+
}
182+
183+
@test "$SUITE: non-plugin script when /plugins/ in relative path" {
184+
create_bats_test_script 'go' \
185+
". '$_GO_CORE_DIR/go-core.bash' 'plugins'" \
186+
'@go "$@"'
187+
create_bats_test_script 'plugins/plugins/foo' \
188+
'@go.print_stack_trace 1'
189+
190+
run "$TEST_GO_SCRIPT" 'plugins/foo'
191+
assert_success
192+
assert_line_matches 0 \
193+
" $_GO_CORE_DIR/go-core.bash:[0-9]+ _@go.run_command_script"
194+
fail_if line_matches 1 \
195+
" $_GO_CORE_DIR/go-core.bash:[1-9]+ _@go.run_plugin_command_script"
196+
}
197+
198+
@test "$SUITE: script isn't a plugin if /plugins/ in _GO_ROOTDIR" {
199+
local test_rootdir="$TEST_GO_ROOTDIR/plugins/rootdir"
200+
mkdir -p "$test_rootdir"
201+
mv "$TEST_GO_SCRIPT" "$test_rootdir"
202+
203+
# We can't use `@go.create_test_command_script` since we can't change the
204+
# readonly `TEST_GO_*` variables.
205+
create_bats_test_script "${test_rootdir#$BATS_TEST_ROOTDIR/}/scripts/foo" \
206+
'@go.print_stack_trace 1'
207+
208+
run "$test_rootdir/go" 'foo'
209+
assert_success
210+
assert_line_matches 0 \
211+
" $_GO_CORE_DIR/go-core.bash:[0-9]+ _@go.run_command_script"
212+
fail_if line_matches 1 \
213+
" $_GO_CORE_DIR/go-core.bash:[1-9]+ _@go.run_plugin_command_script"
214+
}
215+
216+
@test "$SUITE: script isn't a plugin despite /plugins/ paths all the way down" {
217+
local test_rootdir="$TEST_GO_ROOTDIR/plugins/plugins"
218+
local test_scripts_dir="$test_rootdir/plugins"
219+
mkdir -p "$test_scripts_dir/plugins"
220+
221+
create_bats_test_script "${test_rootdir#$BATS_TEST_ROOTDIR/}/go" \
222+
". '$_GO_CORE_DIR/go-core.bash' 'plugins'" \
223+
'@go "$@"'
224+
create_bats_test_script \
225+
"${test_rootdir#$BATS_TEST_ROOTDIR/}/plugins/plugins/foo" \
226+
'@go.print_stack_trace 1'
227+
228+
run "$test_rootdir/go" 'plugins/foo'
229+
assert_success
230+
assert_line_matches 0 \
231+
" $_GO_CORE_DIR/go-core.bash:[0-9]+ _@go.run_command_script"
232+
fail_if line_matches 1 \
233+
" $_GO_CORE_DIR/go-core.bash:[1-9]+ _@go.run_plugin_command_script"
234+
}

0 commit comments

Comments
 (0)