From 9be76b3741e4780a777ab19d76a1179603abdbfa Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Mon, 20 Nov 2017 22:21:39 +0900 Subject: [PATCH 1/4] Pass envMap to parseLine & parseValue --- godotenv.go | 8 ++++---- godotenv_test.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/godotenv.go b/godotenv.go index 48ae78c..b505f51 100644 --- a/godotenv.go +++ b/godotenv.go @@ -112,7 +112,7 @@ func Parse(r io.Reader) (envMap map[string]string, err error) { for _, fullLine := range lines { if !isIgnoredLine(fullLine) { var key, value string - key, value, err = parseLine(fullLine) + key, value, err = parseLine(fullLine, envMap) if err != nil { return @@ -209,7 +209,7 @@ func readFile(filename string) (envMap map[string]string, err error) { return Parse(file) } -func parseLine(line string) (key string, value string, err error) { +func parseLine(line string, envMap map[string]string) (key string, value string, err error) { if len(line) == 0 { err = errors.New("zero length string") return @@ -259,11 +259,11 @@ func parseLine(line string) (key string, value string, err error) { key = strings.Trim(key, " ") // Parse the value - value = parseValue(splitString[1]) + value = parseValue(splitString[1], envMap) return } -func parseValue(value string) string { +func parseValue(value string, envMap map[string]string) string { // trim value = strings.Trim(value, " ") diff --git a/godotenv_test.go b/godotenv_test.go index fc4f7f0..475f4ec 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -11,7 +11,7 @@ import ( var noopPresets = make(map[string]string) func parseAndCompare(t *testing.T, rawEnvLine string, expectedKey string, expectedValue string) { - key, value, _ := parseLine(rawEnvLine) + key, value, _ := parseLine(rawEnvLine, noopPresets) if key != expectedKey || value != expectedValue { t.Errorf("Expected '%v' to parse as '%v' => '%v', got '%v' => '%v' instead", rawEnvLine, expectedKey, expectedValue, key, value) } @@ -280,7 +280,7 @@ func TestParsing(t *testing.T) { // it 'throws an error if line format is incorrect' do // expect{env('lol$wut')}.to raise_error(Dotenv::FormatError) badlyFormattedLine := "lol$wut" - _, _, err := parseLine(badlyFormattedLine) + _, _, err := parseLine(badlyFormattedLine, noopPresets) if err == nil { t.Errorf("Expected \"%v\" to return error, but it didn't", badlyFormattedLine) } From 33977c2d8d42464321c5c9010519ddb0dad59330 Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Mon, 20 Nov 2017 23:20:19 +0900 Subject: [PATCH 2/4] Add test for substitutions --- fixtures/substitutions.env | 5 +++++ godotenv_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 fixtures/substitutions.env diff --git a/fixtures/substitutions.env b/fixtures/substitutions.env new file mode 100644 index 0000000..44337a9 --- /dev/null +++ b/fixtures/substitutions.env @@ -0,0 +1,5 @@ +OPTION_A=1 +OPTION_B=${OPTION_A} +OPTION_C=$OPTION_B +OPTION_D=${OPTION_A}${OPTION_B} +OPTION_E=${OPTION_NOT_DEFINED} diff --git a/godotenv_test.go b/godotenv_test.go index 475f4ec..8e75092 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -193,6 +193,19 @@ func TestLoadQuotedEnv(t *testing.T) { loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) } +func TestSubstituitions(t *testing.T) { + envFileName := "fixtures/substitutions.env" + expectedValues := map[string]string{ + "OPTION_A": "1", + "OPTION_B": "1", + "OPTION_C": "1", + "OPTION_D": "11", + "OPTION_E": "", + } + + loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) +} + func TestActualEnvVarsAreLeftAlone(t *testing.T) { os.Clearenv() os.Setenv("OPTION_A", "actualenv") From 50c29652a0bbce88f011406f5db06a939f9d0d5e Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Mon, 20 Nov 2017 23:20:38 +0900 Subject: [PATCH 3/4] Expand variables on parseValue --- godotenv.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/godotenv.go b/godotenv.go index b505f51..19751d7 100644 --- a/godotenv.go +++ b/godotenv.go @@ -291,6 +291,13 @@ func parseValue(value string, envMap map[string]string) string { } } + // expand variables + value = os.Expand(value, func(key string) string { + if val, ok := envMap[key]; ok { + return val + } + return "" + }) return value } From 2707e9ff66c3758b5e7342e3ee72dd183aba7aef Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Mon, 20 Nov 2017 23:41:46 +0900 Subject: [PATCH 4/4] Fix test, `$` should not be escaped --- godotenv_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/godotenv_test.go b/godotenv_test.go index 8e75092..bbbd658 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -361,7 +361,7 @@ func TestWrite(t *testing.T) { //but single quotes are left alone writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`) // newlines, backslashes, and some other special chars are escaped - writeAndCompare(`foo="$ba\n\r\\r!"`, `foo="\$ba\n\r\\r\!"`) + writeAndCompare(`foo="\n\r\\r!"`, `foo="\n\r\\r\!"`) // lines should be sorted writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"")