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

Function TestActiveHelpForFlag

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

Source from the content-addressed store, hash-verified

229}
230
231func TestActiveHelpForFlag(t *testing.T) {
232 rootCmd := &Command{
233 Use: "root",
234 Run: emptyRun,
235 }
236 flagname := "flag"
237 rootCmd.Flags().String(flagname, "", "A flag")
238
239 // Test that multiple activeHelp message can be added
240 _ = rootCmd.RegisterFlagCompletionFunc(flagname, func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
241 comps := []string{"first"}
242 comps = AppendActiveHelp(comps, activeHelpMessage)
243 comps = append(comps, "second")
244 comps = AppendActiveHelp(comps, activeHelpMessage2)
245 return comps, ShellCompDirectiveNoFileComp
246 })
247
248 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--flag", "")
249 if err != nil {
250 t.Errorf("Unexpected error: %v", err)
251 }
252
253 expected := strings.Join([]string{
254 "first",
255 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
256 "second",
257 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2),
258 ":4",
259 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
260
261 if output != expected {
262 t.Errorf("expected: %q, got: %q", expected, output)
263 }
264}
265
266func TestConfigActiveHelp(t *testing.T) {
267 rootCmd := &Command{

Callers

nothing calls this directly

Calls 5

FlagsMethod · 0.95
AppendActiveHelpFunction · 0.85
executeCommandFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected