diff --git a/env/environment.go b/env/environment.go index 6e6b0d0b7d..241164bfa9 100644 --- a/env/environment.go +++ b/env/environment.go @@ -67,12 +67,6 @@ func FromSlice(s []string) *Environment { return env } -// Get returns a key from the environment -func (e *Environment) Get(key string) (string, bool) { - v, ok := e.underlying.Load(normalizeKeyName(key)) - return v, ok -} - // Dump returns a copy of the environment with all keys normalized func (e *Environment) Dump() map[string]string { d := make(map[string]string, e.underlying.Size()) @@ -84,6 +78,12 @@ func (e *Environment) Dump() map[string]string { return d } +// Get returns a key from the environment +func (e *Environment) Get(key string) (string, bool) { + v, ok := e.underlying.Load(normalizeKeyName(key)) + return v, ok +} + // Get a boolean value from environment, with a default for empty. Supports true|false, on|off, 1|0 func (e *Environment) GetBool(key string, defaultValue bool) bool { v, _ := e.Get(key) @@ -100,13 +100,13 @@ func (e *Environment) GetBool(key string, defaultValue bool) bool { // Exists returns true/false depending on whether or not the key exists in the env func (e *Environment) Exists(key string) bool { - _, ok := e.underlying.Load(key) + _, ok := e.underlying.Load(normalizeKeyName(key)) return ok } // Set sets a key in the environment func (e *Environment) Set(key string, value string) string { - e.underlying.Store(key, value) + e.underlying.Store(normalizeKeyName(key), value) return value } @@ -114,7 +114,7 @@ func (e *Environment) Set(key string, value string) string { func (e *Environment) Remove(key string) string { value, ok := e.Get(key) if ok { - e.underlying.Delete(key) + e.underlying.Delete(normalizeKeyName(key)) } return value } @@ -138,6 +138,7 @@ func (e *Environment) Diff(other *Environment) Diff { diff.Removed[k] = struct{}{} return true }) + return diff } diff --git a/env/environment_test.go b/env/environment_test.go index 3caab35255..6da356b5b5 100644 --- a/env/environment_test.go +++ b/env/environment_test.go @@ -1,6 +1,8 @@ package env import ( + "runtime" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -9,7 +11,7 @@ import ( func TestEnvironmentExists(t *testing.T) { t.Parallel() - env := FromSlice([]string{}) + env := New() env.Set("FOO", "bar") env.Set("EMPTY", "") @@ -22,7 +24,7 @@ func TestEnvironmentExists(t *testing.T) { func TestEnvironmentSet(t *testing.T) { t.Parallel() - env := FromSlice([]string{}) + env := New() env.Set(" THIS_IS_THE_BEST \n\n", "\"IT SURE IS\"\n\n") @@ -31,6 +33,48 @@ func TestEnvironmentSet(t *testing.T) { assert.True(t, ok) } +func TestEnvironmentSet_NormalizesKeyNames(t *testing.T) { + t.Parallel() + e := New() + + mountain := "Mountain" + e.Set(mountain, "Cerro Torre") + + switch runtime.GOOS { + case "windows": + // All keys are treated as being in the same case so long as they have the same letters + // (i.e. "Mountain", "mountain" and "MOUNTAIN" are treated the same key) + assert.True(t, e.Exists(mountain)) + assert.True(t, e.Exists(strings.ToUpper(mountain))) + + v, _ := e.Get(strings.ToUpper(mountain)) + assert.Equal(t, v, "Cerro Torre") + + e.Set(strings.ToUpper(mountain), "Cerro Poincenot") + + v, _ = e.Get(mountain) + assert.Equal(t, v, "Cerro Poincenot") + + v, _ = e.Get(strings.ToUpper(mountain)) + assert.Equal(t, v, "Cerro Poincenot") + + default: + // Two keys with the same letters but different cases can coexist + // (i.e. "Mountain", "mountain", "MOUNTAIN" are treated as three different keys) + assert.True(t, e.Exists(mountain)) + assert.False(t, e.Exists(strings.ToUpper(mountain))) + + e.Set(strings.ToUpper(mountain), "Cerro Poincenot") + + camel, _ := e.Get(mountain) + assert.Equal(t, camel, "Cerro Torre") + + upper, _ := e.Get(strings.ToUpper(mountain)) + assert.Equal(t, upper, "Cerro Poincenot") + } + +} + func TestEnvironmentGetBool(t *testing.T) { t.Parallel()