From 37441b648f090f74dbac9c49970e965ac6255f3c Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Fri, 10 Apr 2020 16:22:43 -0700 Subject: [PATCH] Fix bugs for test pass, update tests Signed-off-by: Ahmet Alp Balkan --- cmd/kubectx/kubeconfig.go | 3 +++ cmd/kubectx/main.go | 2 +- cmd/kubectx/rename.go | 2 +- test/kubectx.bats | 12 +----------- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/cmd/kubectx/kubeconfig.go b/cmd/kubectx/kubeconfig.go index e03e8e5..88b9abf 100644 --- a/cmd/kubectx/kubeconfig.go +++ b/cmd/kubectx/kubeconfig.go @@ -43,6 +43,9 @@ func parseKubeconfig(path string) (kubeconfig, error) { f, err := os.Open(path) if err != nil { + if os.IsNotExist(err) { + return v, nil + } return v, errors.Wrap(err, "file open error") } err = yaml.NewDecoder(f).Decode(&v) diff --git a/cmd/kubectx/main.go b/cmd/kubectx/main.go index 8470cad..65a4f00 100644 --- a/cmd/kubectx/main.go +++ b/cmd/kubectx/main.go @@ -38,7 +38,7 @@ func main() { os.Exit(1) } case RenameOp: - if err := rename(v.Old, v.New); err != nil { + if err := renameContexts(v.Old, v.New); err != nil { printError("failed to rename: %v", err) os.Exit(1) } diff --git a/cmd/kubectx/rename.go b/cmd/kubectx/rename.go index 14fd3a6..b8ba3dd 100644 --- a/cmd/kubectx/rename.go +++ b/cmd/kubectx/rename.go @@ -26,7 +26,7 @@ func parseRenameSyntax(v string) (string, string, bool) { // rename changes the old (NAME or '.' for current-context) // to the "new" value. If the old refers to the current-context, // current-context preference is also updated. -func rename(old, new string) error { +func renameContexts(old, new string) error { f, rootNode, err := openKubeconfig() if err != nil { return nil diff --git a/test/kubectx.bats b/test/kubectx.bats index d5e0e64..220f510 100644 --- a/test/kubectx.bats +++ b/test/kubectx.bats @@ -4,16 +4,6 @@ COMMAND="$BATS_TEST_DIRNAME/../kubectx" load common -@test "no kubectl detected" { - OLDPATH="$PATH" - PATH=/bin - run ${COMMAND} - echo "$output" - [ "$status" -eq 1 ] - [[ "$output" = "kubectl is not installed" ]] - PATH="$OLDPATH" -} - @test "--help should not fail" { run ${COMMAND} --help echo "$output" @@ -32,7 +22,7 @@ load common run ${COMMAND} - echo "$output" [ "$status" -eq 1 ] - [[ "$output" = "error: No previous context found." ]] + [[ $output = *"no previous context found" ]] } @test "list contexts when no kubeconfig exists" {