Skip to content

Commit

Permalink
Add tests for Active Help
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
  • Loading branch information
marckhouzam committed Oct 12, 2021
1 parent 421ff09 commit d84f81f
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 0 deletions.
338 changes: 338 additions & 0 deletions 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)
}
}
19 changes: 19 additions & 0 deletions 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))
}
12 changes: 12 additions & 0 deletions bash_completions_test.go
Expand Up @@ -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))
}
13 changes: 13 additions & 0 deletions fish_completions_test.go
Expand Up @@ -2,6 +2,7 @@ package cobra

import (
"bytes"
"fmt"
"testing"
)

Expand Down Expand Up @@ -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))
}

0 comments on commit d84f81f

Please sign in to comment.