Skip to content

Commit

Permalink
Add test ensuring that key names are normalized correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
moskyb committed Mar 15, 2023
1 parent 369d892 commit bd40b25
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
19 changes: 10 additions & 9 deletions env/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -100,21 +100,21 @@ 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
}

// Remove a key from the Environment and return its value
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
}
Expand All @@ -138,6 +138,7 @@ func (e *Environment) Diff(other *Environment) Diff {
diff.Removed[k] = struct{}{}
return true
})

return diff
}

Expand Down
48 changes: 46 additions & 2 deletions env/environment_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package env

import (
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -9,7 +11,7 @@ import (
func TestEnvironmentExists(t *testing.T) {
t.Parallel()

env := FromSlice([]string{})
env := New()

env.Set("FOO", "bar")
env.Set("EMPTY", "")
Expand All @@ -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")

Expand All @@ -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()

Expand Down

0 comments on commit bd40b25

Please sign in to comment.