...
1
16
17 package completion
18
19 import (
20 "strings"
21 "testing"
22
23 "github.com/spf13/cobra"
24
25 "k8s.io/cli-runtime/pkg/genericiooptions"
26 )
27
28 func TestBashCompletions(t *testing.T) {
29 testCases := []struct {
30 name string
31 args []string
32 expectedError string
33 }{
34 {
35 name: "bash",
36 args: []string{"bash"},
37 },
38 {
39 name: "zsh",
40 args: []string{"zsh"},
41 },
42 {
43 name: "fish",
44 args: []string{"fish"},
45 },
46 {
47 name: "powershell",
48 args: []string{"powershell"},
49 },
50 {
51 name: "no args",
52 args: []string{},
53 expectedError: `Shell not specified.
54 See 'kubectl completion -h' for help and examples`,
55 },
56 {
57 name: "too many args",
58 args: []string{"bash", "zsh"},
59 expectedError: `Too many arguments. Expected only the shell type.
60 See 'kubectl completion -h' for help and examples`,
61 },
62 {
63 name: "unsupported shell",
64 args: []string{"foo"},
65 expectedError: `Unsupported shell type "foo".
66 See 'kubectl completion -h' for help and examples`,
67 },
68 }
69
70 for _, tc := range testCases {
71 t.Run(tc.name, func(tt *testing.T) {
72 _, _, out, _ := genericiooptions.NewTestIOStreams()
73 parentCmd := &cobra.Command{
74 Use: "kubectl",
75 }
76 cmd := NewCmdCompletion(out, defaultBoilerPlate)
77 parentCmd.AddCommand(cmd)
78 err := RunCompletion(out, defaultBoilerPlate, cmd, tc.args)
79 if tc.expectedError == "" {
80 if err != nil {
81 tt.Fatalf("Unexpected error: %v", err)
82 }
83 if out.Len() == 0 {
84 tt.Fatalf("Output was not written")
85 }
86 if !strings.Contains(out.String(), defaultBoilerPlate) {
87 tt.Fatalf("Output does not contain boilerplate:\n%s", out.String())
88 }
89 } else {
90 if err == nil {
91 tt.Fatalf("An error was expected but no error was returned")
92 }
93 if err.Error() != tc.expectedError {
94 tt.Fatalf("Unexpected error: %v\nexpected: %v\n", err, tc.expectedError)
95 }
96 }
97 })
98 }
99 }
100
View as plain text