Skip to content

Commit

Permalink
Remove EnvResolver and resolve variables directly inside LookupEnv
Browse files Browse the repository at this point in the history
Signed-off-by: IKEDA Yasuyuki <[email protected]>
  • Loading branch information
ikedam committed May 4, 2022
1 parent 16e6d5d commit 4fcc599
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 191 deletions.
26 changes: 23 additions & 3 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ package types

import (
"encoding/json"
"runtime"
"strings"

"github.com/compose-spec/compose-go/utils"
"github.com/mitchellh/mapstructure"
)

var (
// isCaseInsensitiveEnvVars is true on platforms where environment variable names are treated case-insensitively.
isCaseInsensitiveEnvVars = (runtime.GOOS == "windows")
)

// ConfigDetails are the details about a group of ConfigFiles
type ConfigDetails struct {
Version string
Expand All @@ -33,8 +39,22 @@ type ConfigDetails struct {

// LookupEnv provides a lookup function for environment variables
func (cd ConfigDetails) LookupEnv(key string) (string, bool) {
v, ok := utils.EnvResolver(cd.Environment)(key)
return v, ok
v, ok := cd.Environment[key]
if !isCaseInsensitiveEnvVars || ok {
return v, ok
}
// variable names must be treated case-insensitively on some platforms (that is, Windows).
// Resolves in this way:
// * Return the value if its name matches with the passed name case-sensitively.
// * Otherwise, return the value if its lower-cased name matches lower-cased passed name.
// * The value is indefinite if multiple variables match.
lowerKey := strings.ToLower(key)
for k, v := range cd.Environment {
if strings.ToLower(k) == lowerKey {
return v, true
}
}
return "", false
}

// ConfigFile is a filename and the contents of the file as a Dict
Expand Down
99 changes: 99 additions & 0 deletions types/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,102 @@ func Test_WithServices(t *testing.T) {
assert.NilError(t, err)
assert.DeepEqual(t, order, []string{"service_2", "service_3", "service_1"})
}

func Test_LookupEnv(t *testing.T) {
tests := []struct {
name string
environment map[string]string
caseInsensitive bool
search string
expectedValue string
expectedOk bool
}{
{
name: "case sensitive/case match",
environment: map[string]string{
"Env1": "Value1",
"Env2": "Value2",
},
caseInsensitive: false,
search: "Env1",
expectedValue: "Value1",
expectedOk: true,
},
{
name: "case sensitive/case unmatch",
environment: map[string]string{
"Env1": "Value1",
"Env2": "Value2",
},
caseInsensitive: false,
search: "ENV1",
expectedValue: "",
expectedOk: false,
},
{
name: "case sensitive/nil environment",
environment: nil,
caseInsensitive: false,
search: "Env1",
expectedValue: "",
expectedOk: false,
},
{
name: "case insensitive/case match",
environment: map[string]string{
"Env1": "Value1",
"Env2": "Value2",
},
caseInsensitive: true,
search: "Env1",
expectedValue: "Value1",
expectedOk: true,
},
{
name: "case insensitive/case unmatch",
environment: map[string]string{
"Env1": "Value1",
"Env2": "Value2",
},
caseInsensitive: true,
search: "ENV1",
expectedValue: "Value1",
expectedOk: true,
},
{
name: "case insensitive/unmatch",
environment: map[string]string{
"Env1": "Value1",
"Env2": "Value2",
},
caseInsensitive: true,
search: "Env3",
expectedValue: "",
expectedOk: false,
},
{
name: "case insensitive/nil environment",
environment: nil,
caseInsensitive: true,
search: "Env1",
expectedValue: "",
expectedOk: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
origIsCaseInsensitiveEnvVars := isCaseInsensitiveEnvVars
defer func() {
isCaseInsensitiveEnvVars = origIsCaseInsensitiveEnvVars
}()
isCaseInsensitiveEnvVars = test.caseInsensitive
cd := ConfigDetails{
Environment: test.environment,
}
v, ok := cd.LookupEnv(test.search)
assert.Equal(t, v, test.expectedValue)
assert.Equal(t, ok, test.expectedOk)
})
}
}
73 changes: 0 additions & 73 deletions utils/envresolver.go

This file was deleted.

115 changes: 0 additions & 115 deletions utils/envresolver_test.go

This file was deleted.

0 comments on commit 4fcc599

Please sign in to comment.