-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix environment variable expansion #276
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,7 +129,7 @@ loop: | |
} | ||
|
||
// extractVarValue extracts variable value and returns rest of slice | ||
func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (value string, rest []byte, err error) { | ||
func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (string, []byte, error) { | ||
quote, isQuoted := hasQuotePrefix(src) | ||
if !isQuoted { | ||
// unquoted value - read until new line | ||
|
@@ -138,13 +138,17 @@ func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (v | |
if end < 0 { | ||
value := strings.Split(string(src), "#")[0] // Remove inline comments on unquoted lines | ||
value = strings.TrimRightFunc(value, unicode.IsSpace) | ||
return expandVariables(value, envMap, lookupFn), nil, nil | ||
|
||
retVal, err := expandVariables(value, envMap, lookupFn) | ||
return retVal, nil, err | ||
} | ||
|
||
value := strings.Split(string(src[0:end]), "#")[0] | ||
value = strings.TrimRightFunc(value, unicode.IsSpace) | ||
rest = src[end:] | ||
return expandVariables(value, envMap, lookupFn), rest, nil | ||
|
||
retVal, err := expandVariables(value, envMap, lookupFn) | ||
return retVal, rest, err | ||
} | ||
|
||
// lookup quoted string terminator | ||
|
@@ -160,11 +164,16 @@ func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (v | |
|
||
// trim quotes | ||
trimFunc := isCharFunc(rune(quote)) | ||
value = string(bytes.TrimLeftFunc(bytes.TrimRightFunc(src[0:i], trimFunc), trimFunc)) | ||
value := string(bytes.TrimLeftFunc(bytes.TrimRightFunc(src[0:i], trimFunc), trimFunc)) | ||
if quote == prefixDoubleQuote { | ||
// unescape newlines for double quote (this is compat feature) | ||
// and expand environment variables | ||
value = expandVariables(expandEscapes(value), envMap, lookupFn) | ||
|
||
retVal, err := expandVariables(expandEscapes(value), envMap, lookupFn) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
value = retVal | ||
} | ||
|
||
return value, src[i+1:], nil | ||
|
@@ -187,6 +196,8 @@ func expandEscapes(str string) string { | |
return "\n" | ||
case "r": | ||
return "\r" | ||
case "$": | ||
return "$$" | ||
Comment on lines
+199
to
+200
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤑 |
||
default: | ||
return match | ||
} | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 for these test cases!