-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
- Loading branch information
1 parent
9da4d5b
commit dcd88bf
Showing
6 changed files
with
420 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.