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..aa1c7d86 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" @@ -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: "", 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) + } + }) } }