mirror of
https://github.com/jimeh/envctl.git
synced 2026-02-19 03:56:39 +00:00
chore!: rename package to envctl
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
// Package climatecontrol provides test helper functions which temporary changes
|
// Package envctl provides test helper functions that temporarily changes
|
||||||
// environment variables accessible via os.Getenv and os.LookupEnv. After they
|
// environment variables accessible via os.Getenv() and os.LookupEnv(). After
|
||||||
// are done, all changes to environment variables are reset.
|
// they are done, all changes to environment variables are reset, including
|
||||||
package climatecontrol
|
// manual changes with os.Setenv().
|
||||||
|
package envctl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
@@ -11,20 +12,20 @@ import (
|
|||||||
|
|
||||||
var mux = &sync.Mutex{}
|
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
|
// execution of f function. Existing environment variables are also available
|
||||||
// within f. Any overridden environment variables will contain the overridden
|
// within f. Any overridden environment variables will contain the overridden
|
||||||
// value..
|
// value..
|
||||||
//
|
//
|
||||||
// After f execution completes all changes to environment variables are reset,
|
// After f execution completes all changes to environment variables are reset,
|
||||||
// including manual changes within the f function.
|
// 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()
|
mux.Lock()
|
||||||
defer mux.Unlock()
|
defer mux.Unlock()
|
||||||
|
|
||||||
undo := parseEnviron(os.Environ())
|
undo := parseEnviron(os.Environ())
|
||||||
|
|
||||||
apply(vars)
|
apply(env)
|
||||||
defer func() {
|
defer func() {
|
||||||
os.Clearenv()
|
os.Clearenv()
|
||||||
apply(undo)
|
apply(undo)
|
||||||
@@ -33,20 +34,20 @@ func WithEnv(vars map[string]string, f func()) {
|
|||||||
f()
|
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
|
// function to only be those provided. Existing environment variables are not
|
||||||
// available within f.
|
// available within f.
|
||||||
//
|
//
|
||||||
// After f execution completes all changes to environment variables are reset,
|
// After f execution completes all changes to environment variables are reset,
|
||||||
// including manual changes within the f function.
|
// 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()
|
mux.Lock()
|
||||||
defer mux.Unlock()
|
defer mux.Unlock()
|
||||||
|
|
||||||
undo := parseEnviron(os.Environ())
|
undo := parseEnviron(os.Environ())
|
||||||
|
|
||||||
os.Clearenv()
|
os.Clearenv()
|
||||||
apply(vars)
|
apply(env)
|
||||||
defer func() {
|
defer func() {
|
||||||
os.Clearenv()
|
os.Clearenv()
|
||||||
apply(undo)
|
apply(undo)
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package climatecontrol_test
|
package envctl_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
cc "github.com/jimeh/climatecontrol"
|
"github.com/jimeh/envctl"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleWithEnv() {
|
func ExampleWith() {
|
||||||
// existing environment variables
|
// existing environment variables
|
||||||
os.Setenv("MYAPP_HOSTNAME", "myapp.com")
|
os.Setenv("MYAPP_HOSTNAME", "myapp.com")
|
||||||
os.Setenv("MYAPP_PORT", "80")
|
os.Setenv("MYAPP_PORT", "80")
|
||||||
@@ -23,7 +23,8 @@ func ExampleWithEnv() {
|
|||||||
"MYAPP_HOSTNAME": "testing-myapp.test",
|
"MYAPP_HOSTNAME": "testing-myapp.test",
|
||||||
"MYAPP_TESTING": "unit",
|
"MYAPP_TESTING": "unit",
|
||||||
}
|
}
|
||||||
cc.WithEnv(env, func() {
|
|
||||||
|
envctl.With(env, func() {
|
||||||
os.Setenv("MYAPP_THEME", "dark")
|
os.Setenv("MYAPP_THEME", "dark")
|
||||||
|
|
||||||
fmt.Println("Inside func:")
|
fmt.Println("Inside func:")
|
||||||
@@ -57,7 +58,7 @@ func ExampleWithEnv() {
|
|||||||
// - MYAPP_TESTING=
|
// - MYAPP_TESTING=
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleWithCleanEnv() {
|
func ExampleWithClean() {
|
||||||
// existing environment variables
|
// existing environment variables
|
||||||
os.Setenv("MYAPP_HOSTNAME", "myapp.com")
|
os.Setenv("MYAPP_HOSTNAME", "myapp.com")
|
||||||
os.Setenv("MYAPP_PORT", "80")
|
os.Setenv("MYAPP_PORT", "80")
|
||||||
@@ -73,7 +74,7 @@ func ExampleWithCleanEnv() {
|
|||||||
"MYAPP_HOSTNAME": "testing-myapp.test",
|
"MYAPP_HOSTNAME": "testing-myapp.test",
|
||||||
"MYAPP_TESTING": "unit",
|
"MYAPP_TESTING": "unit",
|
||||||
}
|
}
|
||||||
cc.WithCleanEnv(env, func() {
|
envctl.WithClean(env, func() {
|
||||||
os.Setenv("MYAPP_THEME", "dark")
|
os.Setenv("MYAPP_THEME", "dark")
|
||||||
|
|
||||||
fmt.Println("Inside func:")
|
fmt.Println("Inside func:")
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package climatecontrol
|
package envctl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWithEnv(t *testing.T) {
|
func TestWith(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
env map[string]string
|
env map[string]string
|
||||||
@@ -36,7 +36,7 @@ func TestWithEnv(t *testing.T) {
|
|||||||
os.Setenv("CC_TEST_OUTSIDE", t.Name())
|
os.Setenv("CC_TEST_OUTSIDE", t.Name())
|
||||||
outside := parseEnviron(os.Environ())
|
outside := parseEnviron(os.Environ())
|
||||||
|
|
||||||
WithEnv(tt.env, func() {
|
With(tt.env, func() {
|
||||||
os.Setenv("CC_TEST_SET_INSIDE", t.Name()+"-manual")
|
os.Setenv("CC_TEST_SET_INSIDE", t.Name()+"-manual")
|
||||||
|
|
||||||
for k, v := range tt.env {
|
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 {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
env map[string]string
|
env map[string]string
|
||||||
@@ -99,7 +99,7 @@ func TestWithCleanEnv(t *testing.T) {
|
|||||||
os.Setenv("CC_TEST_OUTSIDE", t.Name())
|
os.Setenv("CC_TEST_OUTSIDE", t.Name())
|
||||||
outside := parseEnviron(os.Environ())
|
outside := parseEnviron(os.Environ())
|
||||||
|
|
||||||
WithCleanEnv(tt.env, func() {
|
WithClean(tt.env, func() {
|
||||||
os.Setenv("CC_TEST_SET_INSIDE", t.Name()+"-manual")
|
os.Setenv("CC_TEST_SET_INSIDE", t.Name()+"-manual")
|
||||||
|
|
||||||
for k, v := range tt.env {
|
for k, v := range tt.env {
|
||||||
@@ -152,6 +152,8 @@ func Test_parseEnviron(t *testing.T) {
|
|||||||
"NAME=John Doe",
|
"NAME=John Doe",
|
||||||
"SHELL=/bin/bash",
|
"SHELL=/bin/bash",
|
||||||
"GOPRIVATE=",
|
"GOPRIVATE=",
|
||||||
|
"LESS= -R",
|
||||||
|
"LESSOPEN=| src-hilite-lesspipe.sh %s",
|
||||||
"X=11",
|
"X=11",
|
||||||
"TAGS=foo=bar,hello=world",
|
"TAGS=foo=bar,hello=world",
|
||||||
"_=go",
|
"_=go",
|
||||||
@@ -162,6 +164,8 @@ func Test_parseEnviron(t *testing.T) {
|
|||||||
"NAME": "John Doe",
|
"NAME": "John Doe",
|
||||||
"SHELL": "/bin/bash",
|
"SHELL": "/bin/bash",
|
||||||
"GOPRIVATE": "",
|
"GOPRIVATE": "",
|
||||||
|
"LESS": " -R",
|
||||||
|
"LESSOPEN": "| src-hilite-lesspipe.sh %s",
|
||||||
"X": "11",
|
"X": "11",
|
||||||
"TAGS": "foo=bar,hello=world",
|
"TAGS": "foo=bar,hello=world",
|
||||||
"_": "go",
|
"_": "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)
|
||||||
|
}
|
||||||
|
}
|
||||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module github.com/jimeh/climatecontrol
|
module github.com/jimeh/envctl
|
||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
|
|||||||
1
go.sum
1
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/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 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
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/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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
Reference in New Issue
Block a user