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

Function TestMultiActiveHelp

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

Source from the content-addressed store, hash-verified

167}
168
169func TestMultiActiveHelp(t *testing.T) {
170 rootCmd := &Command{
171 Use: "root",
172 Run: emptyRun,
173 }
174
175 childCmd := &Command{
176 Use: "thechild",
177 Short: "The child command",
178 Run: emptyRun,
179 }
180 rootCmd.AddCommand(childCmd)
181
182 // Test that multiple activeHelp message can be added
183 childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
184 comps := AppendActiveHelp(nil, activeHelpMessage)
185 comps = AppendActiveHelp(comps, activeHelpMessage2)
186 return comps, ShellCompDirectiveNoFileComp
187 }
188
189 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
190 if err != nil {
191 t.Errorf("Unexpected error: %v", err)
192 }
193
194 expected := strings.Join([]string{
195 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
196 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2),
197 ":4",
198 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
199
200 if output != expected {
201 t.Errorf("expected: %q, got: %q", expected, output)
202 }
203
204 // Test that multiple activeHelp messages can be used along with completions
205 childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
206 comps := []string{"first"}
207 comps = AppendActiveHelp(comps, activeHelpMessage)
208 comps = append(comps, "second")
209 comps = AppendActiveHelp(comps, activeHelpMessage2)
210 return comps, ShellCompDirectiveNoFileComp
211 }
212
213 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
214 if err != nil {
215 t.Errorf("Unexpected error: %v", err)
216 }
217
218 expected = strings.Join([]string{
219 "first",
220 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
221 "second",
222 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2),
223 ":4",
224 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
225
226 if output != expected {

Callers

nothing calls this directly

Calls 3

AddCommandMethod · 0.95
AppendActiveHelpFunction · 0.85
executeCommandFunction · 0.85

Tested by

no test coverage detected