summaryrefslogtreecommitdiff
path: root/cmd/main_test.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2022-04-13 11:35:28 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2022-04-13 11:38:20 -0600
commit30b6d1f47a35d00fca7cff0daa2ff59a98c5a85e (patch)
tree61adc9f026e4432d30a732d1f437b4a0234c2345 /cmd/main_test.go
parentbc15b4b0e799c52be8be86e85e9624141319474d (diff)
cmd: Enhance .env (dotenv) file parsing
Basic support for quoted values, newlines in quoted values, and comments. Does not support variable or command expansion.
Diffstat (limited to 'cmd/main_test.go')
-rw-r--r--cmd/main_test.go170
1 files changed, 170 insertions, 0 deletions
diff --git a/cmd/main_test.go b/cmd/main_test.go
new file mode 100644
index 0000000..90e8194
--- /dev/null
+++ b/cmd/main_test.go
@@ -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)
+ }
+ }
+}