Skip to content

Commit

Permalink
Merge pull request #110052 from brianpursley/completion-tests
Browse files Browse the repository at this point in the history
Add unit tests for kubectl completion command
  • Loading branch information
k8s-ci-robot committed May 16, 2022
2 parents 9f46016 + a8ec462 commit c84d086
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions staging/src/k8s.io/kubectl/pkg/cmd/completion/completion_test.go
@@ -0,0 +1,99 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package completion

import (
"strings"
"testing"

"github.com/spf13/cobra"

"k8s.io/cli-runtime/pkg/genericclioptions"
)

func TestBashCompletions(t *testing.T) {
testCases := []struct {
name string
args []string
expectedError string
}{
{
name: "bash",
args: []string{"bash"},
},
{
name: "zsh",
args: []string{"zsh"},
},
{
name: "fish",
args: []string{"fish"},
},
{
name: "powershell",
args: []string{"powershell"},
},
{
name: "no args",
args: []string{},
expectedError: `Shell not specified.
See 'kubectl completion -h' for help and examples`,
},
{
name: "too many args",
args: []string{"bash", "zsh"},
expectedError: `Too many arguments. Expected only the shell type.
See 'kubectl completion -h' for help and examples`,
},
{
name: "unsupported shell",
args: []string{"foo"},
expectedError: `Unsupported shell type "foo".
See 'kubectl completion -h' for help and examples`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(tt *testing.T) {
_, _, out, _ := genericclioptions.NewTestIOStreams()
parentCmd := &cobra.Command{
Use: "kubectl",
}
cmd := NewCmdCompletion(out, defaultBoilerPlate)
parentCmd.AddCommand(cmd)
err := RunCompletion(out, defaultBoilerPlate, cmd, tc.args)
if tc.expectedError == "" {
if err != nil {
tt.Fatalf("Unexpected error: %v", err)
}
if out.Len() == 0 {
tt.Fatalf("Output was not written")
}
if !strings.Contains(out.String(), defaultBoilerPlate) {
tt.Fatalf("Output does not contain boilerplate:\n%s", out.String())
}
} else {
if err == nil {
tt.Fatalf("An error was expected but no error was returned")
}
if err.Error() != tc.expectedError {
tt.Fatalf("Unexpected error: %v\nexpected: %v\n", err, tc.expectedError)
}
}
})
}
}

0 comments on commit c84d086

Please sign in to comment.