Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit test for SelectUI #671

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ func PrintTable(config *clientcmdapi.Config) error {

// SelectUI output select ui
func SelectUI(kubeItems []Needle, label string) int {
s, err := selectUIRunner(kubeItems, label, nil)
if err != nil {
if err.Error() == "exit" {
os.Exit(0)
}
log.Fatalf("Prompt failed %v\n", err)
}
return s
}

// selectUIRunner
func selectUIRunner(kubeItems []Needle, label string, runner SelectRunner) (int, error) {
templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U0001F63C {{ .Name | red }}{{ .Center | red}}",
Expand Down Expand Up @@ -168,15 +180,17 @@ func SelectUI(kubeItems []Needle, label string) int {
Size: uiSize,
Searcher: searcher,
}
i, _, err := prompt.Run()
if runner == nil {
runner = &prompt
}
i, _, err := runner.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
return 0, err
}
if kubeItems[i].Name == "<Exit>" {
fmt.Println("Exited.")
os.Exit(1)
return 0, errors.New("exit")
}
return i
return i, err
}

// PromptUI output prompt ui
Expand Down
65 changes: 61 additions & 4 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -297,9 +299,64 @@ func TestMoreInfo(t *testing.T) {
}

// Check if the output contains expected values
expectedOutput := "[Summary] Namespace: 1 Node: 1 Pod: 1 "
str := strings.Replace(buf.String(), "\n", "", -1)
if str != expectedOutput {
t.Errorf("Expected output: %s, got: %s", expectedOutput, buf.String())
if strings.Contains(buf.String(), "Namespace: 1") && strings.Contains(buf.String(), "Node: 1") && strings.Contains(buf.String(), "Pod: 1") {
t.Logf("MoreInfo output is correct")
} else {
t.Errorf("MoreInfo output is incorrect: %s", buf.String())
}
}

type testSelectPrompt struct {
index int
err error
}

func (t *testSelectPrompt) Run() (int, string, error) {
return t.index, "", t.err
}

func TestSelectUI(t *testing.T) {
kubeItems := []Needle{
{Name: "Needle1", Cluster: "Cluster1", User: "User1", Center: "Center1"},
{Name: "Needle2", Cluster: "Cluster2", User: "User2", Center: "Center2"},
{Name: "<Exit>", Cluster: "", User: "", Center: ""},
}

tests := []struct {
name string
selectPrompt SelectRunner
expectedIndex int
expectError bool
}{
{
name: "Select Needle1",
selectPrompt: &testSelectPrompt{
index: 0,
err: nil,
},
expectedIndex: 0,
expectError: false,
},
{
name: "Select <Exit>",
selectPrompt: &testSelectPrompt{
index: 2,
err: nil,
},
expectedIndex: 0,
expectError: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
index, err := selectUIRunner(kubeItems, "Select a needle", test.selectPrompt)
if test.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, test.expectedIndex, index)
}
})
}
}