-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
239 lines (219 loc) · 7.76 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package dotenv
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNext(t *testing.T) {
input := []byte(`abcd`)
parser := parser{input: input}
assert.Equal(t, "a", string(parser.next()))
assert.Equal(t, 0, int(parser.previous))
assert.Equal(t, "b", string(parser.next()))
assert.Equal(t, "a", string(parser.previous))
assert.Equal(t, "c", string(parser.next()))
assert.Equal(t, "b", string(parser.previous))
assert.Equal(t, "d", string(parser.next()))
assert.Equal(t, "c", string(parser.previous))
assert.Equal(t, uint8(0x0), parser.next())
assert.Equal(t, "d", string(parser.previous))
assert.Equal(t, 0, int(parser.next()))
assert.Equal(t, 0, int(parser.previous))
}
func TestParse(t *testing.T) {
testCases := []struct {
input string
expected map[string]string
}{
{"", map[string]string{}},
{`
`, map[string]string{}},
{"EMPTY_KEY=", map[string]string{"EMPTY_KEY": ""}},
{"EMPTY_KEY_WITH_SPACE= ", map[string]string{"EMPTY_KEY_WITH_SPACE": ""}},
{"KEY=value", map[string]string{"KEY": "value"}},
{"ARBITRARY_KEY= hello @ world 123 _+-;:, ", map[string]string{"ARBITRARY_KEY": "hello @ world 123 _+-;:,"}},
{"INLINE_COMMENT= value #comment", map[string]string{"INLINE_COMMENT": "value"}},
{"EMPTY_KEY=#comment", map[string]string{"EMPTY_KEY": ""}},
{"#comment", map[string]string{}},
{`QUOTED="value"`, map[string]string{"QUOTED": "value"}},
{`QUOTED_COMMENT="#quoted-comment"`, map[string]string{"QUOTED_COMMENT": "#quoted-comment"}},
{`KEY=txt" #test"`, map[string]string{"KEY": `txt"`}},
{`WITH_HASH=txt"#test"`, map[string]string{"WITH_HASH": `txt"#test"`}},
{`ONLY_SPACE=" "`, map[string]string{"ONLY_SPACE": " "}},
{`ESCAPED_QUOTE='value'`, map[string]string{"ESCAPED_QUOTE": `value`}},
{`ESCAPED_QUOTE=' value '`, map[string]string{"ESCAPED_QUOTE": ` value `}},
{`ESCAPED_QUOTE='hello world'`, map[string]string{"ESCAPED_QUOTE": `hello world`}},
{`ESCAPED_QUOTE=hello'world'`, map[string]string{"ESCAPED_QUOTE": `hello'world'`}},
{`ESCAPED_QUOTE='$VARworld'`, map[string]string{"ESCAPED_QUOTE": `world`}},
{`WITH_PADDING=" value "`, map[string]string{"WITH_PADDING": " value "}},
{`ESCAPED_QUOTE=" value\" "`, map[string]string{"ESCAPED_QUOTE": ` value" `}},
{`ESCAPED_ESCAPE_CHAR=" value\\s "`, map[string]string{"ESCAPED_ESCAPE_CHAR": ` value\s `}},
{`
KEY_1=value-1
KEY_2=value-2
`, map[string]string{"KEY_1": "value-1", "KEY_2": "value-2"}},
{`
KEY_1 = value 1
KEY_2 = value 2
`, map[string]string{"KEY_1": "value 1", "KEY_2": "value 2"}},
{"key1=value-1\nkey2=value-2", map[string]string{"key1": "value-1", "key2": "value-2"}},
{`
# some comments go here
key1 = value-1 # comment
# comment
key2=8dD63dBQ3Gf+MQO2ZScyLU6culzas5PeoaYj3Q6DddU=
# commet
key3=some-#strange-value
`, map[string]string{
"key1": "value-1",
"key2": "8dD63dBQ3Gf+MQO2ZScyLU6culzas5PeoaYj3Q6DddU=",
"key3": "some-#strange-value"}},
{`
KEY_1=value
KEY_2=$KEY_1
`, map[string]string{"KEY_1": "value", "KEY_2": "value"}},
{`
KEY_1=value
KEY_2="$KEY_1-world"
`, map[string]string{"KEY_1": "value", "KEY_2": "value-world"}},
{`
KEY_1=value
KEY_2="hello-$KEY_1-world"
`, map[string]string{"KEY_1": "value", "KEY_2": "hello-value-world"}},
{`KEY="$UNDEFINED"`, map[string]string{"KEY": ""}},
{`KEY=hello $UNDEFINED world`, map[string]string{"KEY": "hello world"}},
{`KEY=hello$UNDEFINEDworld`, map[string]string{"KEY": "helloworld"}},
{`KEY=\$ESCAPED`, map[string]string{"KEY": "$ESCAPED"}},
{`KEY= $UNDEFINED s`, map[string]string{"KEY": "s"}},
{`KEY=inline-\$ESCAPED-value`, map[string]string{"KEY": "inline-$ESCAPED-value"}},
{`
KEY_1=value
KEY_2=inline-\$KEY_1-value
`, map[string]string{"KEY_1": "value", "KEY_2": "inline-$KEY_1-value"}},
{`
# all at once
KEY_1=value # inline comment
# empty lines
# multi line
# comment
KEY_2=inline-$KEY_1-value
KEY_3=$KEY_2 # comment
KEY_4 = $UNDEFINED_KEY # comment with tabs
KEY_5 = "$KEY_1\_$KEY_2\_$KEY_3\_$KEY_4\_world " # comment with tabs
`, map[string]string{
"KEY_1": "value",
"KEY_2": "inline-value-value",
"KEY_3": "inline-value-value",
"KEY_4": "",
"KEY_5": "value_inline-value-value_inline-value-value__world ",
}},
//
{`
KEY_1=value
KEY_2=${KEY_1}
`, map[string]string{"KEY_1": "value", "KEY_2": "value"}},
{`
KEY_1=value
KEY_2="${KEY_1}-world"
`, map[string]string{"KEY_1": "value", "KEY_2": "value-world"}},
{`
KEY_1=value
KEY_2="hello-${KEY_1}-world"
`, map[string]string{"KEY_1": "value", "KEY_2": "hello-value-world"}},
{`KEY="${UNDEFINED}"`, map[string]string{"KEY": ""}},
{`KEY=hello ${UNDEFINED} world`, map[string]string{"KEY": "hello world"}},
{`KEY=hello${UNDEFINED}world`, map[string]string{"KEY": "helloworld"}},
{`KEY=\${ESCAPED}`, map[string]string{"KEY": "${ESCAPED}"}},
{`KEY= ${UNDEFINED} s`, map[string]string{"KEY": "s"}},
{`KEY=inline-\${ESCAPED}-value`, map[string]string{"KEY": "inline-${ESCAPED}-value"}},
{`
KEY_1=value
KEY_2=inline-\${KEY_1}-value
`, map[string]string{"KEY_1": "value", "KEY_2": "inline-${KEY_1}-value"}},
{`
# all at once
KEY_1=value # inline comment
# empty lines
# multi line
# comment
KEY_2=inline-${KEY_1}-value
KEY_3=${KEY_2} # comment
KEY_4 = ${UNDEFINED_KEY} # comment with tabs
KEY_5 = "${KEY_1}\_${KEY_2}\_${KEY_3}\_${KEY_4}\_world " # comment with tabs
`, map[string]string{
"KEY_1": "value",
"KEY_2": "inline-value-value",
"KEY_3": "inline-value-value",
"KEY_4": "",
"KEY_5": "value_inline-value-value_inline-value-value__world ",
}},
{`KEY=${SOME_KEY`, map[string]string{"KEY": "${SOME_KEY"}},
{`
KEY_1=value
KEY_2=${KEY_1
`, map[string]string{"KEY_1": "value", "KEY_2": "${KEY_1"}},
{`
KEY_1=value
KEY_2=${KEY_1 test
`, map[string]string{"KEY_1": "value", "KEY_2": "${KEY_1 test"}},
{`
KEY_1=value
KEY_2=${KEY_1test
`, map[string]string{"KEY_1": "value", "KEY_2": "${KEY_1test"}},
{`
KEY_1=value
KEY_2="${KEY_1test"
`, map[string]string{"KEY_1": "value", "KEY_2": "${KEY_1test"}},
{`
KEY_1=value
KEY_2="${KEY_1}test"
`, map[string]string{"KEY_1": "value", "KEY_2": "valuetest"}},
}
for _, tc := range testCases {
p := parser{input: []byte(tc.input), env: make(map[string]string)}
err := p.parse()
assert.NoError(t, err)
assert.NotNil(t, p.env)
assert.Equal(t, tc.expected, p.env)
}
}
func TestParserErrors(t *testing.T) {
testCases := []struct {
input string
expectedErr string
}{
{"key", `expected "=", found end of file, line 1:3`},
{`key= "hello`, `unterminated quoted value "hello", line 1:11`},
{`key hello`, `expected "=", found "h", line 1:5`},
}
for _, tc := range testCases {
p := parser{input: []byte(tc.input), env: make(map[string]string)}
err := p.parse()
if assert.Error(t, err) {
assert.Equal(t, tc.expectedErr, err.Error())
}
}
}
func BenchmarkParser(b *testing.B) {
input := `
# all at once
KEY_1=value # inline comment
# empty lines
# multi line
# comment
KEY_2=inline-$KEY_1-value
KEY_3=$KEY_2 # comment
KEY_4 = $UNDEFINED_KEY # comment with tabs
KEY_5 = "$KEY_1\_$KEY_2\_$KEY_3\_$KEY_4\_world " # comment with tabs
KEY_6 = "$KEY_1\_$KEY_2\_$KEY_3\_$KEY_4\_$KEY_5\_world " # comment with tabs
KEY_7 = "$KEY_1\_$KEY_2\_$KEY_3\_$KEY_4\_$KEY_5\_$KEY_6\_world " # comment with tabs
KEY_8 = "$KEY_1\_$KEY_2\_$KEY_3\_$KEY_4\_$KEY_5\_$KEY_6\_$KEY_7\_world " # comment with tabs
KEY_9 = "$KEY_1\_$KEY_2\_$KEY_3\_$KEY_4\_$KEY_5\_$KEY_6\_$KEY_7\_$KEY_8\_world " # comment with tabs
KEY_10 = "$KEY_1\_$KEY_2\_$KEY_3\_$KEY_4\_$KEY_5\_$KEY_6\_$KEY_7\_$KEY_8\_$KEY_9\_world " # comment with tabs
`
for i := 0; i < b.N; i++ {
p := parser{input: []byte(input), env: make(map[string]string)}
if err := p.parse(); err != nil {
b.Fail()
}
}
}