Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #612 from surajssd/env-lookup-trimspace
Browse files Browse the repository at this point in the history
Trim whitespaces before env lookup
  • Loading branch information
surajnarwade authored Mar 8, 2018
2 parents 965e40b + d33cd6d commit ba2dad0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func replaceWithEnv(in []byte) []byte {
slice := strings.Split(name, ":")
// if slice has 2 elements, i.e given variable has default value
if len(slice) == 2 {
name = slice[0]
name = strings.TrimSpace(slice[0])
// look into environment for the value
value, found = os.LookupEnv(name)
// put default value if it's not present in environment
Expand Down
46 changes: 35 additions & 11 deletions pkg/cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ func TestSubstituteVariables(t *testing.T) {
}

tests := []struct {
name string
input []byte
out []byte
shouldRaiseError bool
}{
{
"",
[]byte(`
name: httpd
containers:
Expand All @@ -167,6 +169,7 @@ func TestSubstituteVariables(t *testing.T) {
false,
},
{
"",
[]byte(`
name: [[ TEST_SERVICE_NAME]]
containers:
Expand All @@ -180,6 +183,7 @@ func TestSubstituteVariables(t *testing.T) {
false,
},
{
"",
[]byte(`
name: httpd-[[ NONEXISTING ]]
containers:
Expand All @@ -189,6 +193,7 @@ func TestSubstituteVariables(t *testing.T) {
true,
},
{
"",
[]byte(`
name: httpd
containers:
Expand All @@ -202,6 +207,7 @@ func TestSubstituteVariables(t *testing.T) {
false,
},
{
"",
[]byte(`
name: httpd
containers:
Expand All @@ -215,6 +221,7 @@ func TestSubstituteVariables(t *testing.T) {
false,
},
{
"",
[]byte(`
name: httpd
containers:
Expand All @@ -228,6 +235,7 @@ func TestSubstituteVariables(t *testing.T) {
false,
},
{
"",
[]byte(`
name: httpd
containers:
Expand All @@ -241,6 +249,7 @@ func TestSubstituteVariables(t *testing.T) {
false,
},
{
"",
[]byte(`
name: httpd
containers:
Expand All @@ -253,22 +262,37 @@ func TestSubstituteVariables(t *testing.T) {
`),
false,
},
{
"Env variable has spaces around it and also has default value",
[]byte(`
name: httpd
containers:
- image: foo/bar:[[ TEST_IMAGE_TAG : latest ]]
`),
[]byte(`
name: httpd
containers:
- image: foo/bar:version
`),
false,
},
}

for _, test := range tests {
output, err := SubstituteVariables(test.input)
t.Run(test.name, func(t *testing.T) {
output, err := SubstituteVariables(test.input)

if test.shouldRaiseError && err == nil {
t.Errorf("input should cause error, but no error was returned \n input: %s\n", string(test.input[:]))
}

if !test.shouldRaiseError && err != nil {
t.Errorf("input caused error \n error: %s", err)
}
if test.shouldRaiseError && err == nil {
t.Errorf("input should cause error, but no error was returned \n input: %s\n", string(test.input[:]))
}

if !bytes.Equal(output, test.out) {
t.Errorf("output doesn't match expected output \n output : %s \n expected: %s \n", string(output[:]), string(test.out[:]))
}
if !test.shouldRaiseError && err != nil {
t.Errorf("input caused error \n error: %s", err)
}

if !bytes.Equal(output, test.out) {
t.Errorf("output doesn't match expected output \n output : %s \n expected: %s \n", string(output[:]), string(test.out[:]))
}
})
}
}

0 comments on commit ba2dad0

Please sign in to comment.