Extract env vars to a file + test

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2020-04-12 16:15:17 -07:00
parent 91e00f9867
commit 37ba52f357
No known key found for this signature in database
GPG Key ID: 441833503E604E2C
17 changed files with 104 additions and 51 deletions

View File

@ -57,4 +57,3 @@ func deleteContext(name string) (deleteName string, wasActiveContext bool, err e
}
return name, wasActiveContext, errors.Wrap(kc.Save(), "failed to save kubeconfig file")
}

26
cmd/kubectx/env.go Normal file
View File

@ -0,0 +1,26 @@
package main
import "os"
const (
// EnvFZFIgnore describes the environment variable to set to disable
// interactive context selection when fzf is installed.
EnvFZFIgnore = "KUBECTX_IGNORE_FZF"
// EnvForceColor describes the environment variable to disable color usage
// when printing current context in a list.
EnvNoColor = `NO_COLOR`
// EnvForceColor describes the "internal" environment variable to force
// color usage to show current context in a list.
EnvForceColor = `_KUBECTX_FORCE_COLOR`
)
func useColors() bool {
if os.Getenv(EnvForceColor) != "" {
return true
} else if os.Getenv(EnvNoColor) != "" {
return false
}
return true
}

44
cmd/kubectx/env_test.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"os"
"testing"
)
func withTestVar(key, value string) func() {
orig, ok := os.LookupEnv(key)
os.Setenv(key, value)
return func() {
if ok {
os.Setenv(key, orig)
} else {
os.Unsetenv(key)
}
}
}
func Test_useColors_forceColors(t *testing.T) {
defer withTestVar("_KUBECTX_FORCE_COLOR", "1")()
defer withTestVar("NO_COLOR", "1")()
if !useColors() {
t.Fatal("expected useColors() = true")
}
}
func Test_useColors_disableColors(t *testing.T) {
defer withTestVar("NO_COLOR", "1")()
if useColors() {
t.Fatal("expected useColors() = false")
}
}
func Test_useColors_default(t *testing.T) {
defer withTestVar("NO_COLOR", "")()
defer withTestVar("_KUBECTX_FORCE_COLOR", "")()
if !useColors() {
t.Fatal("expected useColors() = true")
}
}

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
)
func Test_parseArgs_new(t *testing.T) {
@ -59,7 +60,7 @@ func Test_parseArgs_new(t *testing.T) {
want: RenameOp{"a", "."}},
{name: "unrecognized flag",
args: []string{"-x"},
want: UnsupportedOp{Args: []string{"-x"}}},
want: UnsupportedOp{Err: errors.Errorf("unsupported option \"-x\"")}},
// TODO add more UnsupportedOp cases
// TODO consider these cases

View File

@ -13,10 +13,6 @@ import (
"github.com/mattn/go-isatty"
)
const (
envFZFIgnore = "KUBECTX_IGNORE_FZF"
)
type InteractiveSwitchOp struct {
SelfCmd string
}
@ -30,7 +26,7 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
cmd.Env = append(os.Environ(),
fmt.Sprintf("FZF_DEFAULT_COMMAND=%s", op.SelfCmd),
fmt.Sprintf("%s=1", envForceColor))
fmt.Sprintf("%s=1", EnvForceColor))
if err := cmd.Run(); err != nil {
if _, ok := err.(*exec.ExitError); !ok {
return err
@ -64,6 +60,6 @@ func fzfInstalled() bool {
// isInteractiveMode determines if we can do choosing with fzf.
func isInteractiveMode(stdout *os.File) bool {
v := os.Getenv(envFZFIgnore)
v := os.Getenv(EnvFZFIgnore)
return v == "" && isTerminal(stdout) && fzfInstalled()
}

View File

@ -8,7 +8,9 @@ import (
func TestPrintHelp(t *testing.T) {
var buf bytes.Buffer
printHelp(&buf)
if err := (&HelpOp{}).Run(&buf, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "USAGE:") {

View File

@ -60,7 +60,9 @@ func Test_kubeconfigPath_envOvveride(t *testing.T) {
defer os.Unsetenv("KUBECONFIG")
v, err := kubeconfigPath()
if err != nil { t.Fatal(err)}
if err != nil {
t.Fatal(err)
}
if expected := "foo"; v != expected {
t.Fatalf("expected=%q, got=%q", expected, v)
}
@ -72,7 +74,9 @@ func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
defer os.Unsetenv("KUBECONFIG")
_, err := kubeconfigPath()
if err == nil { t.Fatal("expected error")}
if err == nil {
t.Fatal("expected error")
}
}
func testfile(t *testing.T, contents string) (path string, cleanup func()) {

View File

@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"os"
"facette.io/natsort"
"github.com/fatih/color"
@ -45,17 +44,3 @@ func (_ ListOp) Run(stdout, _ io.Writer) error {
}
return nil
}
const (
envForceColor = `_KUBECTX_FORCE_COLOR`
envNoColor = `NO_COLOR`
)
func useColors() bool {
if os.Getenv(envForceColor) != "" {
return true
} else if os.Getenv(envNoColor) != "" {
return false
}
return true
}

View File

@ -30,4 +30,3 @@ func printWarning(w io.Writer, format string, args ...interface{}) {
func printSuccess(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, color.GreenString(fmt.Sprintf(format+"\n", args...)))
}

View File

@ -69,4 +69,3 @@ func (op RenameOp) Run(_, stderr io.Writer) error {
printSuccess(stderr, "Context %q renamed to %q.", op.Old, op.New)
return nil
}

View File

@ -60,7 +60,6 @@ func switchContext(name string) (string, error) {
return name, nil
}
// swapContext switches to previously switch context.
func swapContext() (string, error) {
prevCtxFile, err := kubectxPrevCtxFile()

View File

@ -29,4 +29,3 @@ func (_ UnsetOp) Run(_, stderr io.Writer) error {
_, err := fmt.Fprintln(stderr, "Successfully unset the current context")
return err
}