Skip to content

Commit d84f81f

Browse files
committed
Add tests for Active Help
Signed-off-by: Marc Khouzam <[email protected]>
1 parent 421ff09 commit d84f81f

6 files changed

+420
-0
lines changed

active_help_test.go

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
package cobra
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"testing"
8+
)
9+
10+
const (
11+
activeHelpMessage = "This is an activeHelp message"
12+
activeHelpMessage2 = "This is the rest of the activeHelp message"
13+
)
14+
15+
func TestActiveHelpAlone(t *testing.T) {
16+
rootCmd := &Command{
17+
Use: "root",
18+
Run: emptyRun,
19+
}
20+
21+
activeHelpFunc := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
22+
comps := AppendActiveHelp(nil, activeHelpMessage)
23+
return comps, ShellCompDirectiveDefault
24+
}
25+
26+
// Test that activeHelp can be added to a root command
27+
rootCmd.ValidArgsFunction = activeHelpFunc
28+
29+
output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")
30+
if err != nil {
31+
t.Errorf("Unexpected error: %v", err)
32+
}
33+
34+
expected := strings.Join([]string{
35+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
36+
":0",
37+
"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
38+
39+
if output != expected {
40+
t.Errorf("expected: %q, got: %q", expected, output)
41+
}
42+
43+
rootCmd.ValidArgsFunction = nil
44+
45+
// Test that activeHelp can be added to a child command
46+
childCmd := &Command{
47+
Use: "thechild",
48+
Short: "The child command",
49+
Run: emptyRun,
50+
}
51+
rootCmd.AddCommand(childCmd)
52+
53+
childCmd.ValidArgsFunction = activeHelpFunc
54+
55+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
56+
if err != nil {
57+
t.Errorf("Unexpected error: %v", err)
58+
}
59+
60+
expected = strings.Join([]string{
61+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
62+
":0",
63+
"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
64+
65+
if output != expected {
66+
t.Errorf("expected: %q, got: %q", expected, output)
67+
}
68+
}
69+
70+
func TestActiveHelpWithComps(t *testing.T) {
71+
rootCmd := &Command{
72+
Use: "root",
73+
Run: emptyRun,
74+
}
75+
76+
childCmd := &Command{
77+
Use: "thechild",
78+
Short: "The child command",
79+
Run: emptyRun,
80+
}
81+
rootCmd.AddCommand(childCmd)
82+
83+
// Test that activeHelp can be added following other completions
84+
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
85+
comps := []string{"first", "second"}
86+
comps = AppendActiveHelp(comps, activeHelpMessage)
87+
return comps, ShellCompDirectiveDefault
88+
}
89+
90+
output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
91+
if err != nil {
92+
t.Errorf("Unexpected error: %v", err)
93+
}
94+
95+
expected := strings.Join([]string{
96+
"first",
97+
"second",
98+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
99+
":0",
100+
"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
101+
102+
if output != expected {
103+
t.Errorf("expected: %q, got: %q", expected, output)
104+
}
105+
106+
// Test that activeHelp can be added preceding other completions
107+
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
108+
var comps []string
109+
comps = AppendActiveHelp(comps, activeHelpMessage)
110+
comps = append(comps, []string{"first", "second"}...)
111+
return comps, ShellCompDirectiveDefault
112+
}
113+
114+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
115+
if err != nil {
116+
t.Errorf("Unexpected error: %v", err)
117+
}
118+
119+
expected = strings.Join([]string{
120+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
121+
"first",
122+
"second",
123+
":0",
124+
"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
125+
126+
if output != expected {
127+
t.Errorf("expected: %q, got: %q", expected, output)
128+
}
129+
130+
// Test that activeHelp can be added interleaved with other completions
131+
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
132+
comps := []string{"first"}
133+
comps = AppendActiveHelp(comps, activeHelpMessage)
134+
comps = append(comps, "second")
135+
return comps, ShellCompDirectiveDefault
136+
}
137+
138+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
139+
if err != nil {
140+
t.Errorf("Unexpected error: %v", err)
141+
}
142+
143+
expected = strings.Join([]string{
144+
"first",
145+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
146+
"second",
147+
":0",
148+
"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
149+
150+
if output != expected {
151+
t.Errorf("expected: %q, got: %q", expected, output)
152+
}
153+
}
154+
155+
func TestMultiActiveHelp(t *testing.T) {
156+
rootCmd := &Command{
157+
Use: "root",
158+
Run: emptyRun,
159+
}
160+
161+
childCmd := &Command{
162+
Use: "thechild",
163+
Short: "The child command",
164+
Run: emptyRun,
165+
}
166+
rootCmd.AddCommand(childCmd)
167+
168+
// Test that multiple activeHelp message can be added
169+
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
170+
comps := AppendActiveHelp(nil, activeHelpMessage)
171+
comps = AppendActiveHelp(comps, activeHelpMessage2)
172+
return comps, ShellCompDirectiveNoFileComp
173+
}
174+
175+
output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
176+
if err != nil {
177+
t.Errorf("Unexpected error: %v", err)
178+
}
179+
180+
expected := strings.Join([]string{
181+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
182+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2),
183+
":4",
184+
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
185+
186+
if output != expected {
187+
t.Errorf("expected: %q, got: %q", expected, output)
188+
}
189+
190+
// Test that multiple activeHelp messages can be used along with completions
191+
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
192+
comps := []string{"first"}
193+
comps = AppendActiveHelp(comps, activeHelpMessage)
194+
comps = append(comps, "second")
195+
comps = AppendActiveHelp(comps, activeHelpMessage2)
196+
return comps, ShellCompDirectiveNoFileComp
197+
}
198+
199+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
200+
if err != nil {
201+
t.Errorf("Unexpected error: %v", err)
202+
}
203+
204+
expected = strings.Join([]string{
205+
"first",
206+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
207+
"second",
208+
fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2),
209+
":4",
210+
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
211+
212+
if output != expected {
213+
t.Errorf("expected: %q, got: %q", expected, output)
214+
}
215+
}
216+
217+
func TestConfigActiveHelp(t *testing.T) {
218+
rootCmd := &Command{
219+
Use: "root",
220+
Run: emptyRun,
221+
}
222+
223+
childCmd := &Command{
224+
Use: "thechild",
225+
Short: "The child command",
226+
Run: emptyRun,
227+
}
228+
rootCmd.AddCommand(childCmd)
229+
230+
activeHelpCfg := "someconfig,anotherconfig"
231+
// Set the variable that the completions scripts will be setting
232+
os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg)
233+
234+
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
235+
// The activeHelpConfig variable should be set on the command
236+
if cmd.ActiveHelpConfig != activeHelpCfg {
237+
t.Errorf("expected activeHelpConfig on command: %q, but got: %q", activeHelpCfg, cmd.ActiveHelpConfig)
238+
}
239+
// The activeHelpConfig variable should also be set on the root
240+
if cmd.Root().ActiveHelpConfig != activeHelpCfg {
241+
t.Errorf("expected activeHelpConfig on root: %q, but got: %q", activeHelpCfg, cmd.ActiveHelpConfig)
242+
}
243+
return nil, ShellCompDirectiveDefault
244+
}
245+
246+
_, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
247+
if err != nil {
248+
t.Errorf("Unexpected error: %v", err)
249+
}
250+
os.Unsetenv(activeHelpEnvVar(rootCmd.Name()))
251+
}
252+
253+
func TestDisableActiveHelp(t *testing.T) {
254+
rootCmd := &Command{
255+
Use: "root",
256+
Run: emptyRun,
257+
}
258+
259+
childCmd := &Command{
260+
Use: "thechild",
261+
Short: "The child command",
262+
Run: emptyRun,
263+
}
264+
rootCmd.AddCommand(childCmd)
265+
266+
// Test the disabling of activeHelp using the specific program
267+
// environment variable that the completions scripts will be setting.
268+
// Make sure the disabling value is "0" by hard-coding it in the tests;
269+
// this is for backwards-compatibility as programs will be using this value.
270+
os.Setenv(activeHelpEnvVar(rootCmd.Name()), "0")
271+
272+
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
273+
comps := []string{"first"}
274+
comps = AppendActiveHelp(comps, activeHelpMessage)
275+
return comps, ShellCompDirectiveDefault
276+
}
277+
278+
output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
279+
if err != nil {
280+
t.Errorf("Unexpected error: %v", err)
281+
}
282+
os.Unsetenv(activeHelpEnvVar(rootCmd.Name()))
283+
284+
// Make sure there is no ActiveHelp in the output
285+
expected := strings.Join([]string{
286+
"first",
287+
":0",
288+
"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
289+
290+
if output != expected {
291+
t.Errorf("expected: %q, got: %q", expected, output)
292+
}
293+
294+
// Now test the global disabling of ActiveHelp
295+
os.Setenv(activeHelpGlobalEnvVar, "0")
296+
// Set the specific variable, to make sure it is ignored when the global env
297+
// var is set properly
298+
os.Setenv(activeHelpEnvVar(rootCmd.Name()), "1")
299+
300+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
301+
if err != nil {
302+
t.Errorf("Unexpected error: %v", err)
303+
}
304+
305+
// Make sure there is no ActiveHelp in the output
306+
expected = strings.Join([]string{
307+
"first",
308+
":0",
309+
"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
310+
311+
if output != expected {
312+
t.Errorf("expected: %q, got: %q", expected, output)
313+
}
314+
315+
// Make sure that if the global env variable is set to anything else than
316+
// the disable value it is ignored
317+
os.Setenv(activeHelpGlobalEnvVar, "on")
318+
// Set the specific variable, to make sure it is used (while ignoring the global env var)
319+
activeHelpCfg := "1"
320+
os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg)
321+
322+
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
323+
// The activeHelpConfig variable should be set on the command
324+
if cmd.ActiveHelpConfig != activeHelpCfg {
325+
t.Errorf("expected activeHelpConfig on command: %q, but got: %q", activeHelpCfg, cmd.ActiveHelpConfig)
326+
}
327+
// The activeHelpConfig variable should also be set on the root
328+
if cmd.Root().ActiveHelpConfig != activeHelpCfg {
329+
t.Errorf("expected activeHelpConfig on root: %q, but got: %q", activeHelpCfg, cmd.ActiveHelpConfig)
330+
}
331+
return nil, ShellCompDirectiveDefault
332+
}
333+
334+
_, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
335+
if err != nil {
336+
t.Errorf("Unexpected error: %v", err)
337+
}
338+
}

bash_completionsV2_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cobra
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"testing"
7+
)
8+
9+
func TestBashCompletionV2WithActiveHelp(t *testing.T) {
10+
c := &Command{Use: "c", Run: emptyRun}
11+
12+
buf := new(bytes.Buffer)
13+
assertNoErr(t, c.GenBashCompletionV2(buf, true))
14+
output := buf.String()
15+
16+
// check that active help is not being disabled
17+
activeHelpVar := activeHelpEnvVar(c.Name())
18+
checkOmit(t, output, fmt.Sprintf("%s=0", activeHelpVar))
19+
}

bash_completions_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,15 @@ func TestBashCompletionTraverseChildren(t *testing.T) {
261261
checkOmit(t, output, `local_nonpersistent_flags+=("--bool-flag")`)
262262
checkOmit(t, output, `local_nonpersistent_flags+=("-b")`)
263263
}
264+
265+
func TestBashCompletionNoActiveHelp(t *testing.T) {
266+
c := &Command{Use: "c", Run: emptyRun}
267+
268+
buf := new(bytes.Buffer)
269+
assertNoErr(t, c.GenBashCompletion(buf))
270+
output := buf.String()
271+
272+
// check that active help is being disabled
273+
activeHelpVar := activeHelpEnvVar(c.Name())
274+
check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
275+
}

fish_completions_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cobra
22

33
import (
44
"bytes"
5+
"fmt"
56
"testing"
67
)
78

@@ -67,3 +68,15 @@ func TestProgWithColon(t *testing.T) {
6768
check(t, output, "-c root:colon")
6869
checkOmit(t, output, "-c root_colon")
6970
}
71+
72+
func TestFishCompletionNoActiveHelp(t *testing.T) {
73+
c := &Command{Use: "c", Run: emptyRun}
74+
75+
buf := new(bytes.Buffer)
76+
assertNoErr(t, c.GenFishCompletion(buf, true))
77+
output := buf.String()
78+
79+
// check that active help is being disabled
80+
activeHelpVar := activeHelpEnvVar(c.Name())
81+
check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
82+
}

0 commit comments

Comments
 (0)