From c639b6a5cf77a3f1df33c9f8d79c6e4e109ebd4a Mon Sep 17 00:00:00 2001 From: Jayson Wang Date: Tue, 28 Nov 2023 22:46:44 +0800 Subject: [PATCH] fix namespace suggestion error on context switch (#2315) --- internal/view/app.go | 10 ++++----- internal/view/app_int_test.go | 38 +++++++++++++++++++++++++++++++++++ internal/view/app_test.go | 33 +++--------------------------- 3 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 internal/view/app_int_test.go diff --git a/internal/view/app.go b/internal/view/app.go index 4d1d2fc4ce..214f349c65 100644 --- a/internal/view/app.go +++ b/internal/view/app.go @@ -151,11 +151,6 @@ func (a *App) initSignals() { } func (a *App) suggestCommand() model.SuggestionFunc { - namespaceNames, err := a.namespaceNames() - if err != nil { - log.Error().Err(err).Msg("failed to list namespaces") - } - contextNames, err := a.contextNames() if err != nil { log.Error().Err(err).Msg("failed to list contexts") @@ -176,6 +171,11 @@ func (a *App) suggestCommand() model.SuggestionFunc { } } + namespaceNames, err := a.namespaceNames() + if err != nil { + log.Error().Err(err).Msg("failed to list namespaces") + } + entries = append(entries, suggestSubCommand(s, namespaceNames, contextNames)...) if len(entries) == 0 { return nil diff --git a/internal/view/app_int_test.go b/internal/view/app_int_test.go new file mode 100644 index 0000000000..f20a26c098 --- /dev/null +++ b/internal/view/app_int_test.go @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Authors of K9s + +package view + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_suggestSubCommand(t *testing.T) { + namespaceNames := []string{"kube-system", "kube-public", "default", "nginx-ingress"} + contextNames := []string{"develop", "test", "pre", "prod"} + + tests := []struct { + Command string + Suggestions []string + }{ + {Command: "q", Suggestions: nil}, + {Command: "xray dp", Suggestions: nil}, + {Command: "help k", Suggestions: nil}, + {Command: "ctx p", Suggestions: []string{"re", "rod"}}, + {Command: "ctx p", Suggestions: []string{"re", "rod"}}, + {Command: "ctx pr", Suggestions: []string{"e", "od"}}, + {Command: "context d", Suggestions: []string{"evelop"}}, + {Command: "contexts t", Suggestions: []string{"est"}}, + {Command: "po ", Suggestions: nil}, + {Command: "po x", Suggestions: nil}, + {Command: "po k", Suggestions: []string{"ube-system", "ube-public"}}, + {Command: "po kube-", Suggestions: []string{"system", "public"}}, + } + + for _, tt := range tests { + got := suggestSubCommand(tt.Command, namespaceNames, contextNames) + assert.Equal(t, tt.Suggestions, got) + } +} diff --git a/internal/view/app_test.go b/internal/view/app_test.go index 42555251d9..e214d7c4db 100644 --- a/internal/view/app_test.go +++ b/internal/view/app_test.go @@ -1,46 +1,19 @@ // SPDX-License-Identifier: Apache-2.0 // Copyright Authors of K9s -package view +package view_test import ( "testing" "github.com/derailed/k9s/internal/config" + "github.com/derailed/k9s/internal/view" "github.com/stretchr/testify/assert" ) func TestAppNew(t *testing.T) { - a := NewApp(config.NewConfig(ks{})) + a := view.NewApp(config.NewConfig(ks{})) _ = a.Init("blee", 10) assert.Equal(t, 11, len(a.GetActions())) } - -func Test_suggestSubCommand(t *testing.T) { - namespaceNames := []string{"kube-system", "kube-public", "default", "nginx-ingress"} - contextNames := []string{"develop", "test", "pre", "prod"} - - tests := []struct { - Command string - Suggestions []string - }{ - {Command: "q", Suggestions: nil}, - {Command: "xray dp", Suggestions: nil}, - {Command: "help k", Suggestions: nil}, - {Command: "ctx p", Suggestions: []string{"re", "rod"}}, - {Command: "ctx p", Suggestions: []string{"re", "rod"}}, - {Command: "ctx pr", Suggestions: []string{"e", "od"}}, - {Command: "context d", Suggestions: []string{"evelop"}}, - {Command: "contexts t", Suggestions: []string{"est"}}, - {Command: "po ", Suggestions: nil}, - {Command: "po x", Suggestions: nil}, - {Command: "po k", Suggestions: []string{"ube-system", "ube-public"}}, - {Command: "po kube-", Suggestions: []string{"system", "public"}}, - } - - for _, tt := range tests { - got := suggestSubCommand(tt.Command, namespaceNames, contextNames) - assert.Equal(t, tt.Suggestions, got) - } -}