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

Function TestDefaultCompletionCmd

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

Source from the content-addressed store, hash-verified

2459}
2460
2461func TestDefaultCompletionCmd(t *testing.T) {
2462 rootCmd := &Command{
2463 Use: "root",
2464 Args: NoArgs,
2465 Run: emptyRun,
2466 }
2467
2468 // Test that when there are no sub-commands, the completion command is not created if it is not called directly.
2469 assertNoErr(t, rootCmd.Execute())
2470 for _, cmd := range rootCmd.commands {
2471 if cmd.Name() == compCmdName {
2472 t.Errorf("Should not have a 'completion' command when there are no other sub-commands of root")
2473 break
2474 }
2475 }
2476
2477 // Test that when there are no sub-commands, the completion command is created when it is called directly.
2478 _, err := executeCommand(rootCmd, compCmdName)
2479 if err != nil {
2480 t.Errorf("Unexpected error: %v", err)
2481 }
2482 // Reset the arguments
2483 rootCmd.args = nil
2484 // Remove completion command for the next test
2485 removeCompCmd(rootCmd)
2486
2487 // Add a sub-command
2488 subCmd := &Command{
2489 Use: "sub",
2490 Run: emptyRun,
2491 }
2492 rootCmd.AddCommand(subCmd)
2493
2494 // Test that a completion command is created if there are other sub-commands
2495 found := false
2496 assertNoErr(t, rootCmd.Execute())
2497 for _, cmd := range rootCmd.commands {
2498 if cmd.Name() == compCmdName {
2499 found = true
2500 break
2501 }
2502 }
2503 if !found {
2504 t.Errorf("Should have a 'completion' command when there are other sub-commands of root")
2505 }
2506 // Remove completion command for the next test
2507 removeCompCmd(rootCmd)
2508
2509 // Test that the default completion command can be disabled
2510 rootCmd.CompletionOptions.DisableDefaultCmd = true
2511 assertNoErr(t, rootCmd.Execute())
2512 for _, cmd := range rootCmd.commands {
2513 if cmd.Name() == compCmdName {
2514 t.Errorf("Should not have a 'completion' command when the feature is disabled")
2515 break
2516 }
2517 }
2518 // Re-enable for next test

Callers

nothing calls this directly

Calls 11

ExecuteMethod · 0.95
AddCommandMethod · 0.95
FindMethod · 0.95
FlagsMethod · 0.95
executeCommandFunction · 0.85
removeCompCmdFunction · 0.85
checkFunction · 0.85
checkOmitFunction · 0.85
NameMethod · 0.80
ErrorMethod · 0.80
assertNoErrFunction · 0.70

Tested by

no test coverage detected