forked from spf13/cobra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish_completions_test.go
141 lines (118 loc) · 5.71 KB
/
fish_completions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cobra
import (
"bytes"
"testing"
)
func TestFishCompletions(t *testing.T) {
rootCmd := &Command{
Use: "root",
ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"},
ValidArgs: []string{"pod", "node", "service", "replicationcontroller"},
Run: emptyRun,
}
rootCmd.Flags().IntP("introot", "i", -1, "help's message for flag introot")
rootCmd.MarkFlagRequired("introot")
// Filename.
rootCmd.Flags().String("filename", "", "Enter a filename")
rootCmd.MarkFlagFilename("filename", "json", "yaml", "yml")
// Persistent filename.
rootCmd.PersistentFlags().String("persistent-filename", "", "Enter a filename")
rootCmd.MarkPersistentFlagFilename("persistent-filename")
rootCmd.MarkPersistentFlagRequired("persistent-filename")
// Filename extensions.
rootCmd.Flags().String("filename-ext", "", "Enter a filename (extension limited)")
rootCmd.MarkFlagFilename("filename-ext")
rootCmd.Flags().String("custom", "", "Enter a filename (extension limited)")
rootCmd.MarkFlagCustom("custom", "__complete_custom")
// Subdirectories in a given directory.
rootCmd.Flags().String("theme", "", "theme to use (located in /themes/THEMENAME/)")
echoCmd := &Command{
Use: "echo [string to echo]",
Aliases: []string{"say"},
Short: "Echo anything's to the screen",
Long: "an utterly useless command for testing.",
Example: "Just run cobra-test echo",
Run: emptyRun,
}
echoCmd.Flags().String("filename", "", "Enter a filename")
echoCmd.MarkFlagFilename("filename", "json", "yaml", "yml")
echoCmd.Flags().String("config", "", "config to use (located in /config/PROFILE/)")
printCmd := &Command{
Use: "print [string to print]",
Args: MinimumNArgs(1),
Short: "Print anything to the screen",
Long: "an absolutely utterly useless command for testing.",
Run: emptyRun,
}
deprecatedCmd := &Command{
Use: "deprecated [can't do anything here]",
Args: NoArgs,
Short: "A command which is deprecated",
Long: "an absolutely utterly useless command for testing deprecation!.",
Deprecated: "Please use echo instead",
Run: emptyRun,
}
colonCmd := &Command{
Use: "cmd:colon",
Run: emptyRun,
}
timesCmd := &Command{
Use: "times [# times] [string to echo]",
SuggestFor: []string{"counts"},
Args: OnlyValidArgs,
ValidArgs: []string{"one", "two", "three", "four"},
Short: "Echo anything to the screen more times",
Long: "a slightly useless command for testing.",
Run: emptyRun,
}
echoCmd.AddCommand(timesCmd)
rootCmd.AddCommand(echoCmd, printCmd, deprecatedCmd, colonCmd)
buf := new(bytes.Buffer)
rootCmd.GenFishCompletion(buf)
output := buf.String()
// check for preamble helper functions
check(t, output, "__fish_root_no_subcommand")
check(t, output, "__fish_root_seen_subcommand_path")
check(t, output, "__fish_seen_argument")
// check for subcommands
check(t, output, "-a echo")
check(t, output, "-a print")
checkOmit(t, output, "-a deprecated")
check(t, output, "-a cmd:colon")
// check for nested subcommands
checkRegex(t, output, `-n '__fish_root_seen_subcommand_path echo(; and[^']*)?' -a times`)
// check for flags that will take arguments
check(t, output, "-n '__fish_root_no_subcommand' -r -s i -l introot")
check(t, output, "-n '__fish_root_no_subcommand' -r -l filename")
check(t, output, "-n '__fish_root_no_subcommand' -r -l persistent-filename")
check(t, output, "-n '__fish_root_no_subcommand' -r -l theme")
check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l config")
check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l filename")
// checks escape of description in flags
check(t, output, "-n '__fish_root_no_subcommand' -r -s i -l introot -d 'help\\'s message for flag introot'")
// check for persistent flags that will take arguments
check(t, output, "-n '__fish_root_seen_subcommand_path cmd:colon' -r -l persistent-filename")
check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l persistent-filename")
check(t, output, "-n '__fish_root_seen_subcommand_path echo times' -r -l persistent-filename")
check(t, output, "-n '__fish_root_seen_subcommand_path print' -r -l persistent-filename")
// check for local non-persistent flags
checkRegex(t, output, `; and not __fish_seen_argument -l custom[^']*' -a echo`)
checkRegex(t, output, `; and not __fish_seen_argument -l filename[^']*' -a echo`)
checkRegex(t, output, `; and not __fish_seen_argument -l filename-ext[^']*' -a echo`)
checkRegex(t, output, `; and not __fish_seen_argument -s i -l introot[^']*' -a echo`)
checkRegex(t, output, `; and not __fish_seen_argument -l theme[^']*' -a echo`)
// check for positional arguments to a command
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a pod`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a node`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a service`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a replicationcontroller`)
// check for aliases to positional arguments for a command
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a pods`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a nodes`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a services`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a replicationcontrollers`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a po`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a no`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a svc`)
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a rc`)
}