From 1364c282899bba832b263f9b89c359d8573ce111 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sun, 17 Jan 2021 19:01:32 +0000 Subject: [PATCH] chore!: rename package to envctl --- climatecontrol.go => envctl.go | 21 ++++---- ..._example_test.go => envctl_example_test.go | 13 ++--- climatecontrol_test.go => envctl_test.go | 51 +++++++++++++++++-- go.mod | 2 +- go.sum | 1 + 5 files changed, 66 insertions(+), 22 deletions(-) rename climatecontrol.go => envctl.go (67%) rename climatecontrol_example_test.go => envctl_example_test.go (94%) rename climatecontrol_test.go => envctl_test.go (71%) diff --git a/climatecontrol.go b/envctl.go similarity index 67% rename from climatecontrol.go rename to envctl.go index 743b686..49a2424 100644 --- a/climatecontrol.go +++ b/envctl.go @@ -1,7 +1,8 @@ -// Package climatecontrol provides test helper functions which temporary changes -// environment variables accessible via os.Getenv and os.LookupEnv. After they -// are done, all changes to environment variables are reset. -package climatecontrol +// Package envctl provides test helper functions that temporarily changes +// environment variables accessible via os.Getenv() and os.LookupEnv(). After +// they are done, all changes to environment variables are reset, including +// manual changes with os.Setenv(). +package envctl import ( "os" @@ -11,20 +12,20 @@ import ( var mux = &sync.Mutex{} -// WithEnv temporarily sets all given vars as environment variables during the +// With temporarily sets all given vars as environment variables during the // execution of f function. Existing environment variables are also available // within f. Any overridden environment variables will contain the overridden // value.. // // After f execution completes all changes to environment variables are reset, // including manual changes within the f function. -func WithEnv(vars map[string]string, f func()) { +func With(env map[string]string, f func()) { mux.Lock() defer mux.Unlock() undo := parseEnviron(os.Environ()) - apply(vars) + apply(env) defer func() { os.Clearenv() apply(undo) @@ -33,20 +34,20 @@ func WithEnv(vars map[string]string, f func()) { f() } -// WithCleanEnv temporarily changes all environment variables available within f +// WithClean temporarily changes all environment variables available within f // function to only be those provided. Existing environment variables are not // available within f. // // After f execution completes all changes to environment variables are reset, // including manual changes within the f function. -func WithCleanEnv(vars map[string]string, f func()) { +func WithClean(env map[string]string, f func()) { mux.Lock() defer mux.Unlock() undo := parseEnviron(os.Environ()) os.Clearenv() - apply(vars) + apply(env) defer func() { os.Clearenv() apply(undo) diff --git a/climatecontrol_example_test.go b/envctl_example_test.go similarity index 94% rename from climatecontrol_example_test.go rename to envctl_example_test.go index cfde412..72c886b 100644 --- a/climatecontrol_example_test.go +++ b/envctl_example_test.go @@ -1,13 +1,13 @@ -package climatecontrol_test +package envctl_test import ( "fmt" "os" - cc "github.com/jimeh/climatecontrol" + "github.com/jimeh/envctl" ) -func ExampleWithEnv() { +func ExampleWith() { // existing environment variables os.Setenv("MYAPP_HOSTNAME", "myapp.com") os.Setenv("MYAPP_PORT", "80") @@ -23,7 +23,8 @@ func ExampleWithEnv() { "MYAPP_HOSTNAME": "testing-myapp.test", "MYAPP_TESTING": "unit", } - cc.WithEnv(env, func() { + + envctl.With(env, func() { os.Setenv("MYAPP_THEME", "dark") fmt.Println("Inside func:") @@ -57,7 +58,7 @@ func ExampleWithEnv() { // - MYAPP_TESTING= } -func ExampleWithCleanEnv() { +func ExampleWithClean() { // existing environment variables os.Setenv("MYAPP_HOSTNAME", "myapp.com") os.Setenv("MYAPP_PORT", "80") @@ -73,7 +74,7 @@ func ExampleWithCleanEnv() { "MYAPP_HOSTNAME": "testing-myapp.test", "MYAPP_TESTING": "unit", } - cc.WithCleanEnv(env, func() { + envctl.WithClean(env, func() { os.Setenv("MYAPP_THEME", "dark") fmt.Println("Inside func:") diff --git a/climatecontrol_test.go b/envctl_test.go similarity index 71% rename from climatecontrol_test.go rename to envctl_test.go index e4e66ce..cb3a7af 100644 --- a/climatecontrol_test.go +++ b/envctl_test.go @@ -1,4 +1,4 @@ -package climatecontrol +package envctl import ( "os" @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestWithEnv(t *testing.T) { +func TestWith(t *testing.T) { tests := []struct { name string env map[string]string @@ -36,7 +36,7 @@ func TestWithEnv(t *testing.T) { os.Setenv("CC_TEST_OUTSIDE", t.Name()) outside := parseEnviron(os.Environ()) - WithEnv(tt.env, func() { + With(tt.env, func() { os.Setenv("CC_TEST_SET_INSIDE", t.Name()+"-manual") for k, v := range tt.env { @@ -70,7 +70,7 @@ func TestWithEnv(t *testing.T) { } } -func TestWithCleanEnv(t *testing.T) { +func TestWithClean(t *testing.T) { tests := []struct { name string env map[string]string @@ -99,7 +99,7 @@ func TestWithCleanEnv(t *testing.T) { os.Setenv("CC_TEST_OUTSIDE", t.Name()) outside := parseEnviron(os.Environ()) - WithCleanEnv(tt.env, func() { + WithClean(tt.env, func() { os.Setenv("CC_TEST_SET_INSIDE", t.Name()+"-manual") for k, v := range tt.env { @@ -152,6 +152,8 @@ func Test_parseEnviron(t *testing.T) { "NAME=John Doe", "SHELL=/bin/bash", "GOPRIVATE=", + "LESS= -R", + "LESSOPEN=| src-hilite-lesspipe.sh %s", "X=11", "TAGS=foo=bar,hello=world", "_=go", @@ -162,6 +164,8 @@ func Test_parseEnviron(t *testing.T) { "NAME": "John Doe", "SHELL": "/bin/bash", "GOPRIVATE": "", + "LESS": " -R", + "LESSOPEN": "| src-hilite-lesspipe.sh %s", "X": "11", "TAGS": "foo=bar,hello=world", "_": "go", @@ -176,3 +180,40 @@ func Test_parseEnviron(t *testing.T) { }) } } + +func Benchmark_parseEnviron(b *testing.B) { + env := []string{ + `EDITOR=emacsclient-wrapper`, + `EMACS=/Applications/Emacs.app/Contents/MacOS/Emacs`, + `GEM_EDITOR=emacsclient-wrapper`, + `GOPATH=/Users/jimeh/.go`, + `HOME=/Users/jimeh`, + `HOMEBREW_NO_ANALYTICS=1`, + `LANG=en_US.UTF-8`, + `LC_ALL=en_US.UTF-8`, + `LC_TERMINAL=iTerm2`, + `LC_TERMINAL_VERSION=3.4.3`, + `LESS= -R`, + `LESSOPEN=| src-hilite-lesspipe.sh %s`, + `NODENV_SHELL=zsh`, + `PWD=/Users/jimeh/Projects/envctl`, + `PYENV_SHELL=zsh`, + `RBENV_SHELL=zsh`, + `TERM=screen-256color`, + `TERM_PROGRAM=iTerm.app`, + `TERM_PROGRAM_VERSION=3.4.3`, + `TMPDIR=/tmp/user-jimeh`, + `TMUX=/private/tmp/tmux-501/default,4148,2`, + `TMUX_PANE=%29`, + `TMUX_PLUGIN_MANAGER_PATH=/Users/jimeh/.tmux/plugins/`, + `USER=jimeh`, + `ZPFX=/Users/jimeh/.local/zsh/zinit/polaris`, + `ZSH_CACHE_DIR=/Users/jimeh/.cache/zinit`, + `_=/usr/bin/env`, + `__CFBundleIdentifier=com.googlecode.iterm2`, + } + + for i := 0; i < b.N; i++ { + parseEnviron(env) + } +} diff --git a/go.mod b/go.mod index 30b6b62..b97f757 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/jimeh/climatecontrol +module github.com/jimeh/envctl go 1.15 diff --git a/go.sum b/go.sum index 56d62e7..afe7890 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=