Skip to content

Commit 37b3af4

Browse files
committed
core: Test that plugins don't run selves as plugin
Closes #132. Adds test cases missing from #131. As demonstrated by the "plugin subcommand finds correct command in own plugin" test case, not setting `_GO_SCRIPTS_DIR` correctly for a plugin could've caused serious bugs: - showstopper (best case): no other command script of that name exists outside the plugin - obscure (medium case): finding the wrong script could cause confusing error messages when the interface between the caller and callee don't match - subtle (worst case): finding the wrong version of a script could exhibit confusing behaviors that might only become apparent after-the-fact
1 parent 1fc08b1 commit 37b3af4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/core/plugin-scope-execution.bats

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,40 @@ teardown() {
3030
assert_line_equals 0 'Unknown command: bar'
3131
}
3232

33+
@test "$SUITE: plugin runs command from own _GO_SCRIPTS_DIR not as plugin" {
34+
@go.create_test_command_script 'plugins/foo/bin/foo' '@go bar'
35+
@go.create_test_command_script 'plugins/foo/bin/bar' '@go.print_stack_trace'
36+
37+
run "$TEST_GO_SCRIPT" 'foo'
38+
assert_success
39+
40+
# It shouldn't invoke its own scripts as though they were separate plugins.
41+
assert_line_matches 0 \
42+
" $TEST_GO_PLUGINS_DIR/foo/bin/bar:2 source"
43+
assert_line_matches 1 \
44+
" $_GO_CORE_DIR/go-core.bash:[0-9]+ _@go.run_command_script"
45+
fail_if line_matches 2 \
46+
" $_GO_CORE_DIR/go-core.bash:[1-9]+ _@go.run_plugin_command_script"
47+
}
48+
49+
@test "$SUITE: plugin subcommand finds correct command in own plugin" {
50+
@go.create_test_command_script 'plugins/foo/bin/foo' '@go.print_stack_trace'
51+
@go.create_test_command_script 'plugins/foo/bin/bar' "$PRINT_SOURCE"
52+
@go.create_test_command_script 'plugins/foo/bin/bar.d/baz' '@go foo'
53+
@go.create_test_command_script 'plugins/aaa/bin/foo' "$PRINT_SOURCE"
54+
55+
run "$TEST_GO_SCRIPT" 'bar' 'baz'
56+
assert_success
57+
58+
# It shouldn't invoke its own scripts as though they were separate plugins.
59+
assert_line_matches 0 \
60+
" $TEST_GO_PLUGINS_DIR/foo/bin/foo:2 source"
61+
assert_line_matches 1 \
62+
" $_GO_CORE_DIR/go-core.bash:[0-9]+ _@go.run_command_script"
63+
fail_if line_matches 2 \
64+
" $_GO_CORE_DIR/go-core.bash:[1-9]+ _@go.run_plugin_command_script"
65+
}
66+
3367
@test "$SUITE: plugin can use script from top-level _GO_PLUGINS_DIR" {
3468
@go.create_test_command_script 'plugins/foo/bin/foo' '@go bar'
3569
@go.create_test_command_script 'plugins/bar/bin/bar' "$PRINT_SOURCE"

0 commit comments

Comments
 (0)