-
Notifications
You must be signed in to change notification settings - Fork 7
fix(completion): return suggestions instead of the help page #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vrobert78
wants to merge
1
commit into
main
Choose a base branch
from
fix/shell-completion-no-suggestions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/platformsh/platformify/vendorization" | ||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestCompletionScriptShellOption checks that the generated completion scripts | ||
| // request suggestions with the long --shell option, instead of the glued short | ||
| // option that Symfony's templates use. | ||
| func TestCompletionScriptShellOption(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| template string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "zsh", | ||
| template: `requestComp="${words[0]} ${words[1]} _complete --no-interaction -szsh -a1 -c$((CURRENT-1))" i=""`, | ||
| expected: `requestComp="${words[0]} ${words[1]} _complete --no-interaction --shell=zsh -a1 -c$((CURRENT-1))" i=""`, | ||
| }, | ||
| { | ||
| name: "bash", | ||
| template: `local completecmd=("$sf_cmd" "_complete" "--no-interaction" "-sbash" "-c$cword" "-a1")`, | ||
| expected: `local completecmd=("$sf_cmd" "_complete" "--no-interaction" "--shell=bash" "-c$cword" "-a1")`, | ||
| }, | ||
| { | ||
| name: "fish", | ||
| template: `set completecmd "$sf_cmd[1]" "_complete" "--no-interaction" "-sfish" "-a1"`, | ||
| expected: `set completecmd "$sf_cmd[1]" "_complete" "--no-interaction" "--shell=fish" "-a1"`, | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| assert.Equal(t, c.expected, shellOptionReplacer.Replace(c.template)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestCompleteCommandPassesArgsThrough checks that the arguments of a | ||
| // completion request reach the legacy CLI unchanged. Cobra used to split the | ||
| // glued -s<shell> option into single-letter flags, and the bundled -h made it | ||
| // print the help page instead of any completions. | ||
| func TestCompleteCommandPassesArgsThrough(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| args []string | ||
| }{ | ||
| { | ||
| name: "zsh", | ||
| args: []string{"--no-interaction", "-szsh", "-a1", "-c1", "-itest-cli-executable", "-ienv"}, | ||
| }, | ||
| { | ||
| name: "bash", | ||
| args: []string{"--no-interaction", "-sbash", "-c1", "-a1", "-itest-cli-executable", "-ienv"}, | ||
| }, | ||
| { | ||
| name: "fish", | ||
| args: []string{"--no-interaction", "-sfish", "-a1", "-itest-cli-executable", "-ienv"}, | ||
| }, | ||
| { | ||
| name: "long options", | ||
| args: []string{"--no-interaction", "--shell=zsh", "--api-version=1", "--current=1", "--input=test-cli-executable"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| cnf := testConfig() | ||
| root := newRootCommand(cnf, &vendorization.VendorAssets{Binary: cnf.Application.Executable}) | ||
| root.SetOut(io.Discard) | ||
| root.SetErr(io.Discard) | ||
|
|
||
| var helpCalled bool | ||
| root.SetHelpFunc(func(_ *cobra.Command, _ []string) { helpCalled = true }) | ||
|
|
||
| // Stub out the command so that the legacy CLI is not executed. | ||
| completeCmd, _, err := root.Find([]string{completeCommandName}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, completeCommandName, completeCmd.Name()) | ||
|
|
||
| var got []string | ||
| completeCmd.Run = func(_ *cobra.Command, args []string) { got = args } | ||
|
|
||
| root.SetArgs(append([]string{completeCommandName}, c.args...)) | ||
| require.NoError(t, root.Execute()) | ||
|
|
||
| assert.False(t, helpCalled, "the help page was printed instead of completions") | ||
| assert.Equal(t, c.args, got) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // TestCompletionScript checks that the generated completion scripts request | ||
| // suggestions with the long --shell option, rather than the glued short option | ||
| // (-szsh) that Symfony's templates use. | ||
| func TestCompletionScript(t *testing.T) { | ||
| f := newCommandFactory(t, "", "") | ||
|
|
||
| cases := []struct { | ||
| shell string | ||
| }{ | ||
| {shell: "zsh"}, | ||
| {shell: "bash"}, | ||
| {shell: "fish"}, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.shell, func(t *testing.T) { | ||
| script := f.Run("completion", c.shell) | ||
| assert.Contains(t, script, "--shell="+c.shell) | ||
| assert.NotContains(t, script, "-s"+c.shell) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestComplete checks that a completion request returns suggestions. The glued | ||
| // short options of the completion scripts must reach the legacy CLI unparsed: | ||
| // the bundled -h of -szsh used to make the CLI print its help page instead. | ||
| func TestComplete(t *testing.T) { | ||
| f := newCommandFactory(t, "", "") | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| args []string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "short options", | ||
| args: []string{"_complete", "--no-interaction", "-szsh", "-a1", "-c1", "-iplatform-test", "-ienv"}, | ||
| expected: "environment:list", | ||
| }, | ||
| { | ||
| name: "long options", | ||
| args: []string{ | ||
| "_complete", "--no-interaction", "--shell=zsh", "--api-version=1", "--current=1", | ||
| "--input=platform-test", "--input=env", | ||
| }, | ||
| expected: "environment:list", | ||
| }, | ||
| { | ||
| // An input token can contain an "h" too, as in "platform-test ssh --pro<TAB>". | ||
| name: "input containing h", | ||
| args: []string{"_complete", "--no-interaction", "-szsh", "-a1", "-c2", "-iplatform-test", "-issh", "-i--pro"}, | ||
| expected: "--project", | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| assert.Contains(t, f.Run(c.args...), c.expected) | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.