diff --git a/active_help_test.go b/active_help_test.go new file mode 100644 index 000000000..c217918e8 --- /dev/null +++ b/active_help_test.go @@ -0,0 +1,338 @@ +package cobra + +import ( + "fmt" + "os" + "strings" + "testing" +) + +const ( + activeHelpMessage = "This is an activeHelp message" + activeHelpMessage2 = "This is the rest of the activeHelp message" +) + +func TestActiveHelpAlone(t *testing.T) { + rootCmd := &Command{ + Use: "root", + Run: emptyRun, + } + + activeHelpFunc := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + comps := AppendActiveHelp(nil, activeHelpMessage) + return comps, ShellCompDirectiveDefault + } + + // Test that activeHelp can be added to a root command + rootCmd.ValidArgsFunction = activeHelpFunc + + output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected := strings.Join([]string{ + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage), + ":0", + "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + rootCmd.ValidArgsFunction = nil + + // Test that activeHelp can be added to a child command + childCmd := &Command{ + Use: "thechild", + Short: "The child command", + Run: emptyRun, + } + rootCmd.AddCommand(childCmd) + + childCmd.ValidArgsFunction = activeHelpFunc + + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage), + ":0", + "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } +} + +func TestActiveHelpWithComps(t *testing.T) { + rootCmd := &Command{ + Use: "root", + Run: emptyRun, + } + + childCmd := &Command{ + Use: "thechild", + Short: "The child command", + Run: emptyRun, + } + rootCmd.AddCommand(childCmd) + + // Test that activeHelp can be added following other completions + childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + comps := []string{"first", "second"} + comps = AppendActiveHelp(comps, activeHelpMessage) + return comps, ShellCompDirectiveDefault + } + + output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected := strings.Join([]string{ + "first", + "second", + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage), + ":0", + "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Test that activeHelp can be added preceding other completions + childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + var comps []string + comps = AppendActiveHelp(comps, activeHelpMessage) + comps = append(comps, []string{"first", "second"}...) + return comps, ShellCompDirectiveDefault + } + + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage), + "first", + "second", + ":0", + "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Test that activeHelp can be added interleaved with other completions + childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + comps := []string{"first"} + comps = AppendActiveHelp(comps, activeHelpMessage) + comps = append(comps, "second") + return comps, ShellCompDirectiveDefault + } + + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + "first", + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage), + "second", + ":0", + "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } +} + +func TestMultiActiveHelp(t *testing.T) { + rootCmd := &Command{ + Use: "root", + Run: emptyRun, + } + + childCmd := &Command{ + Use: "thechild", + Short: "The child command", + Run: emptyRun, + } + rootCmd.AddCommand(childCmd) + + // Test that multiple activeHelp message can be added + childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + comps := AppendActiveHelp(nil, activeHelpMessage) + comps = AppendActiveHelp(comps, activeHelpMessage2) + return comps, ShellCompDirectiveNoFileComp + } + + output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected := strings.Join([]string{ + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage), + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2), + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Test that multiple activeHelp messages can be used along with completions + childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + comps := []string{"first"} + comps = AppendActiveHelp(comps, activeHelpMessage) + comps = append(comps, "second") + comps = AppendActiveHelp(comps, activeHelpMessage2) + return comps, ShellCompDirectiveNoFileComp + } + + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + "first", + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage), + "second", + fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2), + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } +} + +func TestConfigActiveHelp(t *testing.T) { + rootCmd := &Command{ + Use: "root", + Run: emptyRun, + } + + childCmd := &Command{ + Use: "thechild", + Short: "The child command", + Run: emptyRun, + } + rootCmd.AddCommand(childCmd) + + activeHelpCfg := "someconfig,anotherconfig" + // Set the variable that the completions scripts will be setting + os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg) + + childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + // The activeHelpConfig variable should be set on the command + if cmd.ActiveHelpConfig != activeHelpCfg { + t.Errorf("expected activeHelpConfig on command: %q, but got: %q", activeHelpCfg, cmd.ActiveHelpConfig) + } + // The activeHelpConfig variable should also be set on the root + if cmd.Root().ActiveHelpConfig != activeHelpCfg { + t.Errorf("expected activeHelpConfig on root: %q, but got: %q", activeHelpCfg, cmd.ActiveHelpConfig) + } + return nil, ShellCompDirectiveDefault + } + + _, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + os.Unsetenv(activeHelpEnvVar(rootCmd.Name())) +} + +func TestDisableActiveHelp(t *testing.T) { + rootCmd := &Command{ + Use: "root", + Run: emptyRun, + } + + childCmd := &Command{ + Use: "thechild", + Short: "The child command", + Run: emptyRun, + } + rootCmd.AddCommand(childCmd) + + // Test the disabling of activeHelp using the specific program + // environment variable that the completions scripts will be setting. + // Make sure the disabling value is "0" by hard-coding it in the tests; + // this is for backwards-compatibility as programs will be using this value. + os.Setenv(activeHelpEnvVar(rootCmd.Name()), "0") + + childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + comps := []string{"first"} + comps = AppendActiveHelp(comps, activeHelpMessage) + return comps, ShellCompDirectiveDefault + } + + output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + os.Unsetenv(activeHelpEnvVar(rootCmd.Name())) + + // Make sure there is no ActiveHelp in the output + expected := strings.Join([]string{ + "first", + ":0", + "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Now test the global disabling of ActiveHelp + os.Setenv(activeHelpGlobalEnvVar, "0") + // Set the specific variable, to make sure it is ignored when the global env + // var is set properly + os.Setenv(activeHelpEnvVar(rootCmd.Name()), "1") + + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + // Make sure there is no ActiveHelp in the output + expected = strings.Join([]string{ + "first", + ":0", + "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Make sure that if the global env variable is set to anything else than + // the disable value it is ignored + os.Setenv(activeHelpGlobalEnvVar, "on") + // Set the specific variable, to make sure it is used (while ignoring the global env var) + activeHelpCfg := "1" + os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg) + + childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + // The activeHelpConfig variable should be set on the command + if cmd.ActiveHelpConfig != activeHelpCfg { + t.Errorf("expected activeHelpConfig on command: %q, but got: %q", activeHelpCfg, cmd.ActiveHelpConfig) + } + // The activeHelpConfig variable should also be set on the root + if cmd.Root().ActiveHelpConfig != activeHelpCfg { + t.Errorf("expected activeHelpConfig on root: %q, but got: %q", activeHelpCfg, cmd.ActiveHelpConfig) + } + return nil, ShellCompDirectiveDefault + } + + _, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } +} diff --git a/bash_completionsV2_test.go b/bash_completionsV2_test.go new file mode 100644 index 000000000..a9a277ed2 --- /dev/null +++ b/bash_completionsV2_test.go @@ -0,0 +1,19 @@ +package cobra + +import ( + "bytes" + "fmt" + "testing" +) + +func TestBashCompletionV2WithActiveHelp(t *testing.T) { + c := &Command{Use: "c", Run: emptyRun} + + buf := new(bytes.Buffer) + assertNoErr(t, c.GenBashCompletionV2(buf, true)) + output := buf.String() + + // check that active help is not being disabled + activeHelpVar := activeHelpEnvVar(c.Name()) + checkOmit(t, output, fmt.Sprintf("%s=0", activeHelpVar)) +} diff --git a/bash_completions_test.go b/bash_completions_test.go index 21d1fe0eb..6896e91c7 100644 --- a/bash_completions_test.go +++ b/bash_completions_test.go @@ -261,3 +261,15 @@ func TestBashCompletionTraverseChildren(t *testing.T) { checkOmit(t, output, `local_nonpersistent_flags+=("--bool-flag")`) checkOmit(t, output, `local_nonpersistent_flags+=("-b")`) } + +func TestBashCompletionNoActiveHelp(t *testing.T) { + c := &Command{Use: "c", Run: emptyRun} + + buf := new(bytes.Buffer) + assertNoErr(t, c.GenBashCompletion(buf)) + output := buf.String() + + // check that active help is being disabled + activeHelpVar := activeHelpEnvVar(c.Name()) + check(t, output, fmt.Sprintf("%s=0", activeHelpVar)) +} diff --git a/fish_completions_test.go b/fish_completions_test.go index a3171e481..37091dcba 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -2,6 +2,7 @@ package cobra import ( "bytes" + "fmt" "testing" ) @@ -67,3 +68,15 @@ func TestProgWithColon(t *testing.T) { check(t, output, "-c root:colon") checkOmit(t, output, "-c root_colon") } + +func TestFishCompletionNoActiveHelp(t *testing.T) { + c := &Command{Use: "c", Run: emptyRun} + + buf := new(bytes.Buffer) + assertNoErr(t, c.GenFishCompletion(buf, true)) + output := buf.String() + + // check that active help is being disabled + activeHelpVar := activeHelpEnvVar(c.Name()) + check(t, output, fmt.Sprintf("%s=0", activeHelpVar)) +} diff --git a/power_completions_test.go b/power_completions_test.go new file mode 100644 index 000000000..771383597 --- /dev/null +++ b/power_completions_test.go @@ -0,0 +1,19 @@ +package cobra + +import ( + "bytes" + "fmt" + "testing" +) + +func TestPwshCompletionNoActiveHelp(t *testing.T) { + c := &Command{Use: "c", Run: emptyRun} + + buf := new(bytes.Buffer) + assertNoErr(t, c.GenPowerShellCompletion(buf)) + output := buf.String() + + // check that active help is being disabled + activeHelpVar := activeHelpEnvVar(c.Name()) + check(t, output, fmt.Sprintf("%s=0", activeHelpVar)) +} diff --git a/zsh_completions_test.go b/zsh_completions_test.go new file mode 100644 index 000000000..b7addb4ca --- /dev/null +++ b/zsh_completions_test.go @@ -0,0 +1,19 @@ +package cobra + +import ( + "bytes" + "fmt" + "testing" +) + +func TestZshCompletionWithActiveHelp(t *testing.T) { + c := &Command{Use: "c", Run: emptyRun} + + buf := new(bytes.Buffer) + assertNoErr(t, c.GenZshCompletion(buf)) + output := buf.String() + + // check that active help is not being disabled + activeHelpVar := activeHelpEnvVar(c.Name()) + checkOmit(t, output, fmt.Sprintf("%s=0", activeHelpVar)) +}