mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
refactor(completion): name path arguments explicitly and share firstArg
The six commands that take a file or directory argument set their completer directly instead of a Use-line parser wrapping every command, the first-argument-only wrappers become one firstArg helper, and only the bash completion help keeps a custom text. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
22 files changed
+75
-212
No files matched your search
@@ -124,6 +124,8 @@ func TestCompletionOffersPathsOnlyWherePathsAreExpected(t *testing.T) {
|
||||
"release asset upload v1 a.txt ": cobra.ShellCompDirectiveDefault,
|
||||
"repo clone alice/project ": cobra.ShellCompDirectiveFilterDirs,
|
||||
"repo clone alice/project dir ": cobra.ShellCompDirectiveNoFileComp,
|
||||
"issue attachment upload 5 ": cobra.ShellCompDirectiveDefault,
|
||||
"release create v1 ": cobra.ShellCompDirectiveDefault,
|
||||
"--cwd ": cobra.ShellCompDirectiveFilterDirs,
|
||||
"--jq ": cobra.ShellCompDirectiveNoFileComp,
|
||||
} {
|
||||
@@ -136,9 +138,9 @@ func TestCompletionOffersPathsOnlyWherePathsAreExpected(t *testing.T) {
|
||||
return
|
||||
}
|
||||
path := strings.Fields(cmd.CommandPath())[1:]
|
||||
slots := argSlots(cmd.Use)
|
||||
_, directive := complete(t, deps, out, append(path, "")...)
|
||||
require.Equal(t, len(slots) > 0 && slots[0].kind == "file", directive == cobra.ShellCompDirectiveDefault, "%s <TAB>: %d", cmd.CommandPath(), directive)
|
||||
takesFile := cmd.CommandPath() == "tea label import" || cmd.CommandPath() == "tea ssh-key create"
|
||||
require.Equal(t, takesFile, directive == cobra.ShellCompDirectiveDefault, "%s <TAB>: %d", cmd.CommandPath(), directive)
|
||||
for _, flags := range []*pflag.FlagSet{cmd.LocalNonPersistentFlags(), cmd.PersistentFlags()} {
|
||||
flags.VisitAll(func(flag *pflag.Flag) {
|
||||
if flag.NoOptDefVal != "" || flag.Hidden {
|
||||
@@ -152,21 +154,6 @@ func TestCompletionOffersPathsOnlyWherePathsAreExpected(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestArgSlots(t *testing.T) {
|
||||
for use, want := range map[string][]argSlot{
|
||||
"list": {},
|
||||
"view <number>": {{}},
|
||||
"upload <tag> <file>...": {{}, {kind: "file", variadic: true}},
|
||||
"clone <repository> [directory]": {{}, {kind: "directory"}},
|
||||
"create <public-key-file>": {{kind: "file"}},
|
||||
"review [<number> | <url> | <branch>]": {{}},
|
||||
"man [command...]": {{variadic: true}},
|
||||
"set <key> <value>": {{}, {}},
|
||||
} {
|
||||
require.Equal(t, want, append([]argSlot{}, argSlots(use)...), use)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfflineCompletions(t *testing.T) {
|
||||
deps, out, _ := testDeps(t)
|
||||
deps.Git = &fakeGit{results: map[string]string{
|
||||
@@ -338,15 +325,6 @@ func TestInstanceCompletions(t *testing.T) {
|
||||
|
||||
func TestCompletionInstallHelp(t *testing.T) {
|
||||
deps, out, _ := testDeps(t)
|
||||
for shell, want := range map[string]string{
|
||||
"bash": "$ eval \"$(tea completion bash)\"\n",
|
||||
"zsh": "$ autoload -U compinit && compinit\n",
|
||||
"fish": "$ tea completion fish > ~/.config/fish/completions/tea.fish\n",
|
||||
"powershell": "$ tea completion powershell | Out-String | Invoke-Expression\n",
|
||||
} {
|
||||
out.Reset()
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"completion", shell, "--help"}))
|
||||
require.Contains(t, out.String(), want, shell)
|
||||
require.NotContains(t, out.String(), "source <(tea completion bash)\n", shell)
|
||||
}
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"completion", "bash", "--help"}))
|
||||
require.Contains(t, out.String(), "$ eval \"$(tea completion bash)\"\n")
|
||||
}
|
||||
Reference in new issue
Block a user