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

Function TestDisableActiveHelp

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

Source from the content-addressed store, hash-verified

317}
318
319func TestDisableActiveHelp(t *testing.T) {
320 rootCmd := &Command{
321 Use: "root",
322 Run: emptyRun,
323 }
324
325 childCmd := &Command{
326 Use: "thechild",
327 Short: "The child command",
328 Run: emptyRun,
329 }
330 rootCmd.AddCommand(childCmd)
331
332 // Test the disabling of activeHelp using the specific program
333 // environment variable that the completions scripts will be setting.
334 // Make sure the disabling value is "0" by hard-coding it in the tests;
335 // this is for backwards-compatibility as programs will be using this value.
336 os.Setenv(activeHelpEnvVar(rootCmd.Name()), "0")
337
338 childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
339 comps := []string{"first"}
340 comps = AppendActiveHelp(comps, activeHelpMessage)
341 return comps, ShellCompDirectiveDefault
342 }
343
344 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
345 if err != nil {
346 t.Errorf("Unexpected error: %v", err)
347 }
348 os.Unsetenv(activeHelpEnvVar(rootCmd.Name()))
349
350 // Make sure there is no ActiveHelp in the output
351 expected := strings.Join([]string{
352 "first",
353 ":0",
354 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
355
356 if output != expected {
357 t.Errorf("expected: %q, got: %q", expected, output)
358 }
359
360 // Now test the global disabling of ActiveHelp
361 os.Setenv(activeHelpGlobalEnvVar, "0")
362 // Set the specific variable, to make sure it is ignored when the global env
363 // var is set properly
364 os.Setenv(activeHelpEnvVar(rootCmd.Name()), "1")
365
366 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
367 if err != nil {
368 t.Errorf("Unexpected error: %v", err)
369 }
370
371 // Make sure there is no ActiveHelp in the output
372 expected = strings.Join([]string{
373 "first",
374 ":0",
375 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
376

Callers

nothing calls this directly

Calls 6

AddCommandMethod · 0.95
NameMethod · 0.95
activeHelpEnvVarFunction · 0.85
AppendActiveHelpFunction · 0.85
executeCommandFunction · 0.85
GetActiveHelpConfigFunction · 0.85

Tested by

no test coverage detected