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

Function TestFlagCompletionForPersistentFlagsCalledFromSubCmd

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

Source from the content-addressed store, hash-verified

2077}
2078
2079func TestFlagCompletionForPersistentFlagsCalledFromSubCmd(t *testing.T) {
2080 rootCmd := &Command{Use: "root", Run: emptyRun}
2081 rootCmd.PersistentFlags().String("string", "", "test string flag")
2082 _ = rootCmd.RegisterFlagCompletionFunc("string", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
2083 return []string{"myval"}, ShellCompDirectiveDefault
2084 })
2085
2086 childCmd := &Command{
2087 Use: "child",
2088 Run: emptyRun,
2089 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
2090 return []string{"--validarg", "test"}, ShellCompDirectiveDefault
2091 },
2092 }
2093 childCmd.Flags().Bool("bool", false, "test bool flag")
2094 rootCmd.AddCommand(childCmd)
2095
2096 // Test that persistent flag completion works for the subcmd
2097 output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child", "--string", "")
2098 if err != nil {
2099 t.Errorf("Unexpected error: %v", err)
2100 }
2101
2102 expected := strings.Join([]string{
2103 "myval",
2104 ":0",
2105 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
2106
2107 if output != expected {
2108 t.Errorf("expected: %q, got: %q", expected, output)
2109 }
2110}
2111
2112// This test tries to register flag completion concurrently to make sure the
2113// code handles concurrency properly.

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected