-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: Enhance .env (dotenv) file parsing
Basic support for quoted values, newlines in quoted values, and comments. Does not support variable or command expansion.
- Loading branch information
Showing
2 changed files
with
212 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package caddycmd | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestParseEnvFile(t *testing.T) { | ||
for i, tc := range []struct { | ||
input string | ||
expect map[string]string | ||
shouldErr bool | ||
}{ | ||
{ | ||
input: `KEY=value`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
INVALID KEY=asdf | ||
OTHER_KEY=Some Value | ||
`, | ||
shouldErr: true, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
SIMPLE_QUOTED="quoted value" | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
"SIMPLE_QUOTED": "quoted value", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
NEWLINES="foo | ||
bar" | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
"NEWLINES": "foo\n\tbar", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
ESCAPED="\"escaped quotes\" | ||
here" | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
"ESCAPED": "\"escaped quotes\"\nhere", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
export KEY=value | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
=value | ||
OTHER_KEY=Some Value | ||
`, | ||
shouldErr: true, | ||
}, | ||
{ | ||
input: ` | ||
EMPTY= | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"EMPTY": "", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
EMPTY="" | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"EMPTY": "", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
#OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
COMMENT=foo bar # some comment here | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
"COMMENT": "foo bar", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
WHITESPACE= foo | ||
OTHER_KEY=Some Value | ||
`, | ||
shouldErr: true, | ||
}, | ||
{ | ||
input: ` | ||
KEY=value | ||
WHITESPACE=" foo bar " | ||
OTHER_KEY=Some Value | ||
`, | ||
expect: map[string]string{ | ||
"KEY": "value", | ||
"WHITESPACE": " foo bar ", | ||
"OTHER_KEY": "Some Value", | ||
}, | ||
}, | ||
} { | ||
actual, err := parseEnvFile(strings.NewReader(tc.input)) | ||
if err != nil && !tc.shouldErr { | ||
t.Errorf("Test %d: Got error but shouldn't have: %v", i, err) | ||
} | ||
if err == nil && tc.shouldErr { | ||
t.Errorf("Test %d: Did not get error but should have", i) | ||
} | ||
if tc.shouldErr { | ||
continue | ||
} | ||
if !reflect.DeepEqual(tc.expect, actual) { | ||
t.Errorf("Test %d: Expected %v but got %v", i, tc.expect, actual) | ||
} | ||
} | ||
} |