Skip to content

Commit

Permalink
Merge pull request #29 from buildkite/windows-env-flake
Browse files Browse the repository at this point in the history
fix: Env test flakes on Windows
  • Loading branch information
DrJosh9000 authored Mar 7, 2024
2 parents 4c17e96 + 68e0fb4 commit 6cc34bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func CaseSensitive(actuallyCaseSensitive bool) Options {
// FromMap is an option that sets the Env to have the key-values pairs from the `source` map.
// The key-value pair will be inserted with the case sensitivity of the Env, which by default is
// case-insensitive on Windows and case-sensitive on other platforms.
// Note that random map iteration will cause the result to be non-deterministic if there are
// multiple keys in `source`, which are equivalent under case insensitivity, that have different
// corresponding values.
func FromMap(source map[string]string) Options {
return func(e *Env) {
if e.env == nil {
Expand Down
31 changes: 30 additions & 1 deletion internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,36 @@ func TestEnvCaseInsensitive(t *testing.T) {
func TestEnvWithMap(t *testing.T) {
t.Parallel()

e := env.New(env.FromMap(map[string]string{"FOO": "upper-bar", "Foo": "lower-bar"}))
// To prevent this test from flaking on Windows, the `source` map should not
// contain case-insensitively-equivalent keys with different values.

e := env.New(env.FromMap(map[string]string{"FOO": "foo", "Bar": "bar"}))

if v, found := e.Get("FOO"); !found {
expected := "foo"
if v != expected {
t.Errorf("Expected FOO to be %q, got %q", expected, v)
}
}

if v, found := e.Get("Bar"); !found || v != "bar" {
t.Errorf(`Expected Foo to be "bar", got %q`, v)
}

if _, found := e.Get("not-foo"); found {
t.Errorf("Expected not-foo to not be found")
}
}

func TestEnvDefaults(t *testing.T) {
t.Parallel()

// To prevent this test from flaking on Windows, e should not be created
// with FromMap.

e := env.New()
e.Set("FOO", "upper-bar")
e.Set("Foo", "lower-bar")

if v, found := e.Get("FOO"); !found {
expected := "upper-bar"
Expand Down

0 comments on commit 6cc34bb

Please sign in to comment.