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

Function TestConfigActiveHelp

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

Source from the content-addressed store, hash-verified

264}
265
266func TestConfigActiveHelp(t *testing.T) {
267 rootCmd := &Command{
268 Use: "root",
269 Run: emptyRun,
270 }
271
272 childCmd := &Command{
273 Use: "thechild",
274 Short: "The child command",
275 Run: emptyRun,
276 }
277 rootCmd.AddCommand(childCmd)
278
279 activeHelpCfg := "someconfig,anotherconfig"
280 // Set the variable that the user would be setting
281 os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg)
282
283 childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
284 receivedActiveHelpCfg := GetActiveHelpConfig(cmd)
285 if receivedActiveHelpCfg != activeHelpCfg {
286 t.Errorf("expected activeHelpConfig: %q, but got: %q", activeHelpCfg, receivedActiveHelpCfg)
287 }
288 return nil, ShellCompDirectiveDefault
289 }
290
291 _, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
292 if err != nil {
293 t.Errorf("Unexpected error: %v", err)
294 }
295
296 // Test active help config for a flag
297 activeHelpCfg = "a config for a flag"
298 // Set the variable that the completions scripts will be setting
299 os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg)
300
301 flagname := "flag"
302 childCmd.Flags().String(flagname, "", "A flag")
303
304 // Test that multiple activeHelp message can be added
305 _ = childCmd.RegisterFlagCompletionFunc(flagname, func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
306 receivedActiveHelpCfg := GetActiveHelpConfig(cmd)
307 if receivedActiveHelpCfg != activeHelpCfg {
308 t.Errorf("expected activeHelpConfig: %q, but got: %q", activeHelpCfg, receivedActiveHelpCfg)
309 }
310 return nil, ShellCompDirectiveDefault
311 })
312
313 _, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "--flag", "")
314 if err != nil {
315 t.Errorf("Unexpected error: %v", err)
316 }
317}
318
319func TestDisableActiveHelp(t *testing.T) {
320 rootCmd := &Command{

Callers

nothing calls this directly

Calls 8

AddCommandMethod · 0.95
NameMethod · 0.95
FlagsMethod · 0.95
activeHelpEnvVarFunction · 0.85
GetActiveHelpConfigFunction · 0.85
executeCommandFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected