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

Function TestActiveHelpWithComps

active_help_test.go:84–167  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

82}
83
84func TestActiveHelpWithComps(t *testing.T) {
85 rootCmd := &Command{
86 Use: "root",
87 Run: emptyRun,
88 }
89
90 childCmd := &Command{
91 Use: "thechild",
92 Short: "The child command",
93 Run: emptyRun,
94 }
95 rootCmd.AddCommand(childCmd)
96
97 // Test that activeHelp can be added following other completions
98 childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
99 comps := []string{"first", "second"}
100 comps = AppendActiveHelp(comps, activeHelpMessage)
101 return comps, ShellCompDirectiveDefault
102 }
103
104 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
105 if err != nil {
106 t.Errorf("Unexpected error: %v", err)
107 }
108
109 expected := strings.Join([]string{
110 "first",
111 "second",
112 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
113 ":0",
114 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
115
116 if output != expected {
117 t.Errorf("expected: %q, got: %q", expected, output)
118 }
119
120 // Test that activeHelp can be added preceding other completions
121 childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
122 var comps []string
123 comps = AppendActiveHelp(comps, activeHelpMessage)
124 comps = append(comps, []string{"first", "second"}...)
125 return comps, ShellCompDirectiveDefault
126 }
127
128 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
129 if err != nil {
130 t.Errorf("Unexpected error: %v", err)
131 }
132
133 expected = strings.Join([]string{
134 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
135 "first",
136 "second",
137 ":0",
138 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
139
140 if output != expected {
141 t.Errorf("expected: %q, got: %q", expected, output)

Callers

nothing calls this directly

Calls 3

AddCommandMethod · 0.95
AppendActiveHelpFunction · 0.85
executeCommandFunction · 0.85

Tested by

no test coverage detected