feat(plugin): Allow plugins to receive empty arg

Empty arguments were being swallowed by the plugin parsing logic; this
commit fixes that.

This was not a big problem as empty arguments are probably quite rare.
However, the shell completion logic does have a use case for an empty
argument.  Plugins using Cobra have a '__complete' command that can be
called to obtain the valid completion choices.  That command takes as
arguments what the user has typed.  When the user has not typed anything
the command needs to be passed an explicit empty argument.  For example:

  helm 2to3 __complete ''

but since the empty argument is not being passed to the plugin we get a
missing argument error because the __complete command requires at least
one argument.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
pull/10656/head
Marc Khouzam 4 years ago
parent 12f1bc0acd
commit b8a994bd78

@ -170,7 +170,9 @@ func manuallyProcessArgs(args []string) ([]string, []string) {
return v return v
} }
} }
return "" // Return something different than what we received by adding a random prefix.
// We cannot simply return an empty string as v could be an empty string itself.
return fmt.Sprintf("x%s", v)
} }
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {

@ -116,6 +116,8 @@ func TestLoadPlugins(t *testing.T) {
{"env", "env stuff", "show the env", "env\n", []string{}, 0}, {"env", "env stuff", "show the env", "env\n", []string{}, 0},
{"exitwith", "exitwith code", "This exits with the specified exit code", "", []string{"2"}, 2}, {"exitwith", "exitwith code", "This exits with the specified exit code", "", []string{"2"}, 2},
{"fullenv", "show env vars", "show all env vars", envs + "\n", []string{}, 0}, {"fullenv", "show env vars", "show all env vars", envs + "\n", []string{}, 0},
// Test that empty arguments are passed to the plugin
{"numargs", "echo num args", "echo number of arguments received", "3\n", []string{"-a", "", "-b"}, 0},
} }
plugins := cmd.Commands() plugins := cmd.Commands()
@ -196,6 +198,7 @@ func TestLoadPluginsForCompletion(t *testing.T) {
{"more", []string{"one", "two"}, []string{"b", "ball"}, []staticCompletionDetails{}}, {"more", []string{"one", "two"}, []string{"b", "ball"}, []staticCompletionDetails{}},
}}, }},
}}, }},
{"numargs", []string{}, []string{}, []staticCompletionDetails{}},
} }
checkCommand(t, cmd.Commands(), tests) checkCommand(t, cmd.Commands(), tests)
} }

@ -0,0 +1,4 @@
name: numargs
usage: "echo num args"
description: "echo number of arguments received"
command: "$HELM_PLUGIN_DIR/numargs.sh"

@ -3,5 +3,6 @@ echo echo stuff
env env stuff env env stuff
exitwith exitwith code exitwith exitwith code
fullenv show env vars fullenv show env vars
numargs echo num args
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

@ -2,5 +2,6 @@ echo echo stuff
env env stuff env env stuff
exitwith exitwith code exitwith exitwith code
fullenv show env vars fullenv show env vars
numargs echo num args
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

Loading…
Cancel
Save