forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletion_test.go
108 lines (88 loc) · 2.32 KB
/
completion_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cli
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCompletionDisable(t *testing.T) {
cmd := &Command{}
err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName})
assert.Error(t, err, "Expected error for no help topic for completion")
}
func TestCompletionEnable(t *testing.T) {
cmd := &Command{
EnableShellCompletion: true,
}
err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName})
assert.ErrorContains(t, err, "no shell provided")
}
func TestCompletionEnableDiffCommandName(t *testing.T) {
cmd := &Command{
EnableShellCompletion: true,
ShellCompletionCommandName: "junky",
}
err := cmd.Run(buildTestContext(t), []string{"foo", "junky"})
assert.ErrorContains(t, err, "no shell provided")
}
func TestCompletionShell(t *testing.T) {
for k := range shellCompletions {
out := &bytes.Buffer{}
t.Run(k, func(t *testing.T) {
cmd := &Command{
EnableShellCompletion: true,
Writer: out,
}
r := require.New(t)
r.NoError(cmd.Run(buildTestContext(t), []string{"foo", completionCommandName, k}))
r.Containsf(
k, out.String(),
"Expected output to contain shell name %[1]q", k,
)
})
}
}
func TestCompletionSubcommand(t *testing.T) {
out := &bytes.Buffer{}
cmd := &Command{
EnableShellCompletion: true,
Writer: out,
Commands: []*Command{
{
Name: "bar",
Commands: []*Command{
{
Name: "xyz",
Flags: []Flag{
&StringFlag{
Name: "g",
Aliases: []string{
"t",
},
},
},
},
},
},
},
}
r := require.New(t)
r.NoError(cmd.Run(buildTestContext(t), []string{"foo", "bar", "--generate-shell-completion"}))
r.Containsf(
out.String(), "xyz",
"Expected output to contain shell name %[1]q", "xyz",
)
out.Reset()
r.NoError(cmd.Run(buildTestContext(t), []string{"foo", "bar", "xyz", "-", "--generate-shell-completion"}))
r.Containsf(
out.String(), "-g",
"Expected output to contain flag %[1]q", "-g",
)
}
func TestCompletionInvalidShell(t *testing.T) {
cmd := &Command{
EnableShellCompletion: true,
}
err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName, "junky-sheell"})
assert.ErrorContains(t, err, "unknown shell junky-sheell")
}