-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
completion_test.go
86 lines (75 loc) · 1.97 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
package completion
import (
"bytes"
"errors"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
)
var _ = Describe("completion", func() {
It("with bash", func() {
cmd := newMockCmd("bash")
out, err := cmd.execute()
Expect(err).ToNot(HaveOccurred())
Expect(out).To(ContainSubstring("bash completion for eksctl"))
})
It("with zsh", func() {
cmd := newMockCmd("zsh")
out, err := cmd.execute()
Expect(err).ToNot(HaveOccurred())
Expect(out).To(ContainSubstring("#compdef _eksctl eksctl"))
})
It("with fish", func() {
cmd := newMockCmd("fish")
out, _ := cmd.execute()
Expect(out).To(ContainSubstring("fish completion for eksctl"))
})
It("with powershell", func() {
cmd := newMockCmd("powershell")
out, _ := cmd.execute()
Expect(out).To(ContainSubstring("Register-ArgumentCompleter -Native -CommandName 'eksctl'"))
})
It("with invalid shell", func() {
cmd := newMockCmd("invalid-shell")
_, err := cmd.execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Error: unknown command \"invalid-shell\" for \"completion\""))
Expect(err.Error()).To(ContainSubstring("usage"))
})
It("with no shell", func() {
cmd := newMockCmd("")
out, _ := cmd.execute()
Expect(out).To(ContainSubstring("Usage"))
})
})
func newMockCmd(args ...string) *mockVerbCmd {
cmd := Command(&cobra.Command{
Use: "eksctl [command]",
Short: "The official CLI for Amazon EKS",
Run: func(c *cobra.Command, _ []string) {
if err := c.Help(); err != nil {
fmt.Println(err.Error())
}
},
SilenceUsage: true,
})
cmd.SetArgs(args)
return &mockVerbCmd{
parentCmd: cmd,
}
}
type mockVerbCmd struct {
parentCmd *cobra.Command
}
func (c mockVerbCmd) execute() (string, error) {
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
c.parentCmd.SetOut(outBuf)
c.parentCmd.SetErr(errBuf)
err := c.parentCmd.Execute()
if err != nil {
err = errors.New(errBuf.String())
}
return outBuf.String(), err
}