-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from compose-spec/fix-default-values
Fix environment variable expansion
- Loading branch information
Showing
6 changed files
with
189 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package dotenv | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/compose-spec/compose-go/template" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var envMap = map[string]string{ | ||
// UNSET_VAR: <Cannot be here :D> | ||
"EMPTY_VAR": "", | ||
"TEST_VAR": "Test Value", | ||
} | ||
|
||
var notFoundLookup = func(s string) (string, bool) { | ||
return "", false | ||
} | ||
|
||
func TestExpandIfEmptyOrUnset(t *testing.T) { | ||
templateResults := []struct { | ||
name string | ||
input string | ||
result string | ||
}{ | ||
{ | ||
"Expand if empty or unset: UNSET_VAR", | ||
"RESULT=${UNSET_VAR:-Default Value}", | ||
"RESULT=Default Value", | ||
}, | ||
{ | ||
"Expand if empty or unset: EMPTY_VAR", | ||
"RESULT=${EMPTY_VAR:-Default Value}", | ||
"RESULT=Default Value", | ||
}, | ||
{ | ||
"Expand if empty or unset: TEST_VAR", | ||
"RESULT=${TEST_VAR:-Default Value}", | ||
"RESULT=Test Value", | ||
}, | ||
} | ||
|
||
for _, expected := range templateResults { | ||
t.Run(expected.name, func(t *testing.T) { | ||
result, err := expandVariables(expected.input, envMap, notFoundLookup) | ||
assert.Nil(t, err) | ||
assert.Equal(t, result, expected.result) | ||
}) | ||
} | ||
} | ||
|
||
func TestExpandIfUnset(t *testing.T) { | ||
templateResults := []struct { | ||
name string | ||
input string | ||
result string | ||
}{ | ||
{ | ||
"Expand if unset: UNSET_VAR", | ||
"RESULT=${UNSET_VAR-Default Value}", | ||
"RESULT=Default Value", | ||
}, | ||
{ | ||
"Expand if unset: EMPTY_VAR", | ||
"RESULT=${EMPTY_VAR-Default Value}", | ||
"RESULT=", | ||
}, | ||
{ | ||
"Expand if unset: TEST_VAR", | ||
"RESULT=${TEST_VAR-Default Value}", | ||
"RESULT=Test Value", | ||
}, | ||
} | ||
|
||
for _, expected := range templateResults { | ||
t.Run(expected.name, func(t *testing.T) { | ||
result, err := expandVariables(expected.input, envMap, notFoundLookup) | ||
assert.Nil(t, err) | ||
assert.Equal(t, result, expected.result) | ||
}) | ||
} | ||
} | ||
|
||
func TestErrorIfEmptyOrUnset(t *testing.T) { | ||
templateResults := []struct { | ||
name string | ||
input string | ||
result string | ||
err error | ||
}{ | ||
{ | ||
"Error empty or unset: UNSET_VAR", | ||
"RESULT=${UNSET_VAR:?Test error}", | ||
"RESULT=${UNSET_VAR:?Test error}", | ||
&template.InvalidTemplateError{Template: "required variable UNSET_VAR is missing a value: Test error"}, | ||
}, | ||
{ | ||
"Error empty or unset: EMPTY_VAR", | ||
"RESULT=${EMPTY_VAR:?Test error}", | ||
"RESULT=${EMPTY_VAR:?Test error}", | ||
&template.InvalidTemplateError{Template: "required variable EMPTY_VAR is missing a value: Test error"}, | ||
}, | ||
{ | ||
"Error empty or unset: TEST_VAR", | ||
"RESULT=${TEST_VAR:?Default Value}", | ||
"RESULT=Test Value", | ||
nil, | ||
}, | ||
} | ||
|
||
for _, expected := range templateResults { | ||
t.Run(expected.name, func(t *testing.T) { | ||
result, err := expandVariables(expected.input, envMap, notFoundLookup) | ||
assert.Equal(t, expected.err, err) | ||
assert.Equal(t, expected.result, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestErrorIfUnset(t *testing.T) { | ||
templateResults := []struct { | ||
name string | ||
input string | ||
result string | ||
err error | ||
}{ | ||
{ | ||
"Error on unset: UNSET_VAR", | ||
"RESULT=${UNSET_VAR?Test error}", | ||
"RESULT=${UNSET_VAR?Test error}", | ||
&template.InvalidTemplateError{Template: "required variable UNSET_VAR is missing a value: Test error"}, | ||
}, | ||
{ | ||
"Error on unset: EMPTY_VAR", | ||
"RESULT=${EMPTY_VAR?Test error}", | ||
"RESULT=", | ||
nil, | ||
}, | ||
{ | ||
"Error on unset: TEST_VAR", | ||
"RESULT=${TEST_VAR?Default Value}", | ||
"RESULT=Test Value", | ||
nil, | ||
}, | ||
} | ||
|
||
for _, expected := range templateResults { | ||
t.Run(expected.name, func(t *testing.T) { | ||
result, err := expandVariables(expected.input, envMap, notFoundLookup) | ||
assert.Equal(t, expected.err, err) | ||
assert.Equal(t, expected.result, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters