|
| 1 | +package env |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestParseDotEnv(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + content string |
| 13 | + expected map[string]string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "simple key=value", |
| 17 | + content: "FOO=bar", |
| 18 | + expected: map[string]string{ |
| 19 | + "FOO": "bar", |
| 20 | + }, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "single-line single-quoted JSON", |
| 24 | + content: `KEY='{"a": "b"}'`, |
| 25 | + expected: map[string]string{ |
| 26 | + "KEY": `{"a": "b"}`, |
| 27 | + }, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "single-line double-quoted JSON", |
| 31 | + content: `KEY="{\"a\": \"b\"}"`, |
| 32 | + expected: map[string]string{ |
| 33 | + "KEY": `{\"a\": \"b\"}`, |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "multi-line single-quoted JSON", |
| 38 | + content: `KEY='{ |
| 39 | + "a": "b" |
| 40 | +}'`, |
| 41 | + expected: map[string]string{ |
| 42 | + "KEY": "{\n \"a\": \"b\"\n}", |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "multi-line double-quoted value", |
| 47 | + content: "KEY=\"hello\nworld\"", |
| 48 | + expected: map[string]string{ |
| 49 | + "KEY": "hello\nworld", |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "multi-line with multiple keys", |
| 54 | + content: `BEFORE=hello |
| 55 | +JSON_VAL='{ |
| 56 | + "key": "value", |
| 57 | + "num": 42 |
| 58 | +}' |
| 59 | +AFTER=world`, |
| 60 | + expected: map[string]string{ |
| 61 | + "BEFORE": "hello", |
| 62 | + "JSON_VAL": "{\n \"key\": \"value\",\n \"num\": 42\n}", |
| 63 | + "AFTER": "world", |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "comments and blank lines are skipped", |
| 68 | + content: `# this is a comment |
| 69 | +FOO=bar |
| 70 | +
|
| 71 | +# another comment |
| 72 | +BAZ=qux`, |
| 73 | + expected: map[string]string{ |
| 74 | + "FOO": "bar", |
| 75 | + "BAZ": "qux", |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "value with equals sign", |
| 80 | + content: `CONN=postgres://user:pass@host/db?sslmode=require`, |
| 81 | + expected: map[string]string{ |
| 82 | + "CONN": "postgres://user:pass@host/db?sslmode=require", |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "multi-line with nested braces", |
| 87 | + content: `CONFIG='{ |
| 88 | + "database": { |
| 89 | + "host": "localhost", |
| 90 | + "port": 5432 |
| 91 | + } |
| 92 | +}'`, |
| 93 | + expected: map[string]string{ |
| 94 | + "CONFIG": "{\n \"database\": {\n \"host\": \"localhost\",\n \"port\": 5432\n }\n}", |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "unquoted value", |
| 99 | + content: `KEY=some value here`, |
| 100 | + expected: map[string]string{ |
| 101 | + "KEY": "some value here", |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "empty value", |
| 106 | + content: `KEY=`, |
| 107 | + expected: map[string]string{ |
| 108 | + "KEY": "", |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "double-quoted value with single quotes inside", |
| 113 | + content: `KEY="{'a': 'b'}"`, |
| 114 | + expected: map[string]string{ |
| 115 | + "KEY": "{'a': 'b'}", |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "multi-line double-quoted with single quotes inside", |
| 120 | + content: "KEY=\"{\n 'a': 'b'\n}\"", |
| 121 | + expected: map[string]string{ |
| 122 | + "KEY": "{\n 'a': 'b'\n}", |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "line without equals is skipped", |
| 127 | + content: "NOPE", |
| 128 | + expected: map[string]string{}, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + for _, tt := range tests { |
| 133 | + t.Run(tt.name, func(t *testing.T) { |
| 134 | + result := ParseDotEnv(tt.content) |
| 135 | + assert.Equal(t, tt.expected, result) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments