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

Function TestPluginWithSubCommands

command_test.go:394–432  ·  view source on GitHub ↗

TestPluginWithSubCommands checks usage as plugin with sub commands.

(t *testing.T)

Source from the content-addressed store, hash-verified

392
393// TestPluginWithSubCommands checks usage as plugin with sub commands.
394func 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"

Callers

nothing calls this directly

Calls 3

AddCommandMethod · 0.95
executeCommandFunction · 0.85
checkStringContainsFunction · 0.70

Tested by

no test coverage detected