MCPcopy
hub / github.com/spf13/cobra / TestNoCmdNameCompletionInGo

Function TestNoCmdNameCompletionInGo

completions_test.go:157–320  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

155}
156
157func TestNoCmdNameCompletionInGo(t *testing.T) {
158 rootCmd := &Command{
159 Use: "root",
160 Run: emptyRun,
161 }
162 rootCmd.Flags().String("localroot", "", "local root flag")
163
164 childCmd1 := &Command{
165 Use: "childCmd1",
166 Short: "First command",
167 Args: MinimumNArgs(0),
168 Run: emptyRun,
169 }
170 rootCmd.AddCommand(childCmd1)
171 childCmd1.PersistentFlags().StringP("persistent", "p", "", "persistent flag")
172 persistentFlag := childCmd1.PersistentFlags().Lookup("persistent")
173 childCmd1.Flags().StringP("nonPersistent", "n", "", "non-persistent flag")
174 nonPersistentFlag := childCmd1.Flags().Lookup("nonPersistent")
175
176 childCmd2 := &Command{
177 Use: "childCmd2",
178 Run: emptyRun,
179 }
180 childCmd1.AddCommand(childCmd2)
181
182 // Test that sub-command names are not completed if there is an argument already
183 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "arg1", "")
184 if err != nil {
185 t.Errorf("Unexpected error: %v", err)
186 }
187
188 expected := strings.Join([]string{
189 ":0",
190 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
191
192 if output != expected {
193 t.Errorf("expected: %q, got: %q", expected, output)
194 }
195
196 // Test that sub-command names are not completed if a local non-persistent flag is present
197 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "--nonPersistent", "value", "")
198 if err != nil {
199 t.Errorf("Unexpected error: %v", err)
200 }
201 // Reset the flag for the next command
202 nonPersistentFlag.Changed = false
203
204 expected = strings.Join([]string{
205 ":0",
206 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
207
208 if output != expected {
209 t.Errorf("expected: %q, got: %q", expected, output)
210 }
211
212 // Test that sub-command names are completed if a local non-persistent flag is present and TraverseChildren is set to true
213 // set TraverseChildren to true on the root cmd
214 rootCmd.TraverseChildren = true

Callers

nothing calls this directly

Calls 6

FlagsMethod · 0.95
AddCommandMethod · 0.95
PersistentFlagsMethod · 0.95
MinimumNArgsFunction · 0.85
executeCommandFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected