From 60377ba03f6b5f8abd3032415913056d7d19f4c7 Mon Sep 17 00:00:00 2001 From: guoxudong Date: Mon, 10 Apr 2023 20:00:42 +0800 Subject: [PATCH 1/2] test: add unit test for SelectUI --- cmd/utils.go | 24 +++++++++++++++----- cmd/utils_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/cmd/utils.go b/cmd/utils.go index 0d23f227..d8087dc7 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -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}}", @@ -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 == "" { - fmt.Println("Exited.") - os.Exit(1) + return 0, errors.New("exit") } - return i + return i, err } // PromptUI output prompt ui diff --git a/cmd/utils_test.go b/cmd/utils_test.go index 12577079..af88bf7d 100644 --- a/cmd/utils_test.go +++ b/cmd/utils_test.go @@ -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" @@ -303,3 +305,58 @@ func TestMoreInfo(t *testing.T) { t.Errorf("Expected output: %s, got: %s", expectedOutput, 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: "", 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 ", + 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) + } + }) + } +} From 39e4fb5254cc3ee4565608608a3bfd3ee8a7ade8 Mon Sep 17 00:00:00 2001 From: guoxudong Date: Mon, 10 Apr 2023 20:13:17 +0800 Subject: [PATCH 2/2] fix --- cmd/utils_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/utils_test.go b/cmd/utils_test.go index af88bf7d..aa1c7d86 100644 --- a/cmd/utils_test.go +++ b/cmd/utils_test.go @@ -299,10 +299,10 @@ 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()) } }