TestPluginWithSubCommands checks usage as plugin with sub commands.
(t *testing.T)
| 392 | |
| 393 | // TestPluginWithSubCommands checks usage as plugin with sub commands. |
| 394 | func TestPluginWithSubCommands(t *testing.T) { |
| 395 | rootCmd := &Command{ |
| 396 | Use: "kubectl-plugin", |
| 397 | Version: "1.0.0", |
| 398 | Args: NoArgs, |
| 399 | Annotations: map[string]string{ |
| 400 | CommandDisplayNameAnnotation: "kubectl plugin", |
| 401 | }, |
| 402 | } |
| 403 | |
| 404 | subCmd := &Command{Use: "sub [flags]", Args: NoArgs, Run: emptyRun} |
| 405 | rootCmd.AddCommand(subCmd) |
| 406 | |
| 407 | rootHelp, err := executeCommand(rootCmd, "-h") |
| 408 | if err != nil { |
| 409 | t.Errorf("Unexpected error: %v", err) |
| 410 | } |
| 411 | |
| 412 | checkStringContains(t, rootHelp, "kubectl plugin [command]") |
| 413 | checkStringContains(t, rootHelp, "help for kubectl plugin") |
| 414 | checkStringContains(t, rootHelp, "version for kubectl plugin") |
| 415 | checkStringContains(t, rootHelp, "kubectl plugin [command] --help") |
| 416 | |
| 417 | childHelp, err := executeCommand(rootCmd, "sub", "-h") |
| 418 | if err != nil { |
| 419 | t.Errorf("Unexpected error: %v", err) |
| 420 | } |
| 421 | |
| 422 | checkStringContains(t, childHelp, "kubectl plugin sub [flags]") |
| 423 | checkStringContains(t, childHelp, "help for sub") |
| 424 | |
| 425 | helpHelp, err := executeCommand(rootCmd, "help", "-h") |
| 426 | if err != nil { |
| 427 | t.Errorf("Unexpected error: %v", err) |
| 428 | } |
| 429 | |
| 430 | checkStringContains(t, helpHelp, "kubectl plugin help [path to command]") |
| 431 | checkStringContains(t, helpHelp, "kubectl plugin help [command]") |
| 432 | } |
| 433 | |
| 434 | // TestChildSameName checks the correct behaviour of cobra in cases, |
| 435 | // when an application with name "foo" and with subcommand "foo" |
nothing calls this directly
no test coverage detected