Debian Codegraph @

parsers/dotenv/dotenv_test.go

raw
1 package dotenv
2
3 import (
4 "encoding/json"
5 "strings"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 )
11
12 func TestDotEnv_Marshal(t *testing.T) {
13 de := DotEnv{}
14 testCases := []struct {
15 name string
16 cfg map[string]any
17 expOut []byte
18 err error
19 }{
20 {
21 name: "Empty config",
22 cfg: map[string]any{},
23 expOut: []byte{},
24 },
25 {
26 name: "Simple key-value pair",
27 cfg: map[string]any{
28 "key": "value",
29 },
30 expOut: []byte("key=\"value\""),
31 },
32 {
33 name: "Multiple key-values",
34 cfg: map[string]any{
35 "key_1": "value_1",
36 "key_2": "value_2",
37 "key_3": "value_3",
38 },
39 expOut: []byte("key_1=\"value_1\"\nkey_2=\"value_2\"\nkey_3=\"value_3\""),
40 },
41 {
42 name: "Mixed data types",
43 cfg: map[string]any{
44 "int_key": 12,
45 "bool_key": true,
46 "arr_key": []int{1, 2, 3, 4},
47 "float_key": 10.5,
48 },
49 expOut: []byte("arr_key=\"[1 2 3 4]\"\nbool_key=\"true\"\nfloat_key=\"10.5\"\nint_key=12"),
50 },
51 {
52 name: "Nested config",
53 cfg: map[string]any{
54 "map_key": map[string]any{
55 "arr_key": []float64{1.2, 4.3, 5, 6},
56 "bool_key": false,
57 "inner_map_key": map[any]any{
58 0: "zero",
59 1: 1.0,
60 },
61 "int_key": 12,
62 },
63 },
64 expOut: []byte(`map_key="map[arr_key:[1.2 4.3 5 6] bool_key:false inner_map_key:map[0:zero 1:1] int_key:12]"`),
65 },
66 }
67
68 for _, tc := range testCases {
69 t.Run(tc.name, func(t *testing.T) {
70 out, err := de.Marshal(tc.cfg)
71 assert.Equal(t, tc.err, err)
72 assert.Equal(t, string(tc.expOut), string(out))
73 })
74 }
75 }
76
77 func TestDotEnv_Unmarshal(t *testing.T) {
78 de := DotEnv{}
79 testCases := []struct {
80 name string
81 cfg []byte
82 expOut map[string]any
83 err bool
84 }{
85 {
86 name: "Empty config",
87 expOut: map[string]any{},
88 },
89 {
90 name: "Simple key_value",
91 cfg: []byte(`key="value"`),
92 expOut: map[string]any{
93 "key": "value",
94 },
95 },
96 {
97 name: "Multiple key_values",
98 cfg: []byte("key_1=\"value_1\"\nkey_2=\"value_2\""),
99 expOut: map[string]any{
100 "key_1": "value_1",
101 "key_2": "value_2",
102 },
103 },
104 {
105 name: "Mixed data types",
106 cfg: []byte("arr=\"[1 2 3 4]\"\nbool=\"true\"\nfloat=\"12.5\"\nint=\"32\"\n"),
107 expOut: map[string]any{
108 "arr": "[1 2 3 4]",
109 "bool": "true",
110 "float": "12.5",
111 "int": "32",
112 },
113 },
114 {
115 name: "Nested config",
116 cfg: []byte(`map_key="map[arr_key:[1 4 5 6] bool_key:false inner_map_key:map[0:zero 1:1] int_key:12]"`),
117 expOut: map[string]any{
118 "map_key": "map[arr_key:[1 4 5 6] bool_key:false inner_map_key:map[0:zero 1:1] int_key:12]",
119 },
120 },
121 {
122 name: "Missing quotation mark",
123 cfg: []byte(`key="value`),
124 err: true,
125 },
126 {
127 name: "Missing value",
128 cfg: []byte(`key=`),
129 expOut: map[string]any{
130 "key": "",
131 },
132 },
133 }
134
135 for _, tc := range testCases {
136 t.Run(tc.name, func(t *testing.T) {
137 outMap, err := de.Unmarshal(tc.cfg)
138 if tc.err {
139 require.NotNil(t, err)
140 }
141 assert.Equal(t, tc.expOut, outMap)
142 })
143 }
144 }
145
146 func TestCompareToEnvProvider(t *testing.T) {
147
148 testCases := []struct {
149 name string
150 prefix string
151 delim string
152 key string
153 value string
154 expKey string
155 expValue string
156 cb func(key string) string
157 want *DotEnv
158 }{
159 {
160 name: "Nil cb",
161 prefix: "TESTVAR_",
162 delim: ".",
163 want: &DotEnv{
164 prefix: "TESTVAR_",
165 delim: ".",
166 },
167 },
168 {
169 name: "Simple cb",
170 prefix: "TESTVAR_",
171 delim: ".",
172 key: "TestKey",
173 value: "TestVal",
174 expKey: "testkey",
175 expValue: "TestVal",
176 cb: func(key string) string {
177 return strings.ToLower(key)
178 },
179 want: &DotEnv{
180 prefix: "TESTVAR_",
181 delim: ".",
182 },
183 },
184 {
185 name: "Empty string nil cb",
186 prefix: "",
187 delim: ".",
188 want: &DotEnv{
189 prefix: "",
190 delim: ".",
191 },
192 },
193 {
194 name: "Cb is given",
195 prefix: "",
196 delim: ".",
197 key: "test_key",
198 value: "test_val",
199 expKey: "TEST.KEY",
200 expValue: "test_val",
201 cb: func(key string) string {
202 return strings.Replace(strings.ToUpper(key), "_", ".", -1)
203 },
204 },
205 }
206
207 for _, tc := range testCases {
208 t.Run(tc.name, func(t *testing.T) {
209 gotProvider := ParserEnv(tc.prefix, tc.delim, tc.cb)
210 if tc.cb == nil {
211 assert.Equal(t, tc.prefix, gotProvider.prefix)
212 assert.Equal(t, tc.delim, gotProvider.delim)
213 // do not compare cb or reverseCB
214 }
215 if tc.cb != nil {
216 k, v := gotProvider.cb(tc.key, tc.value)
217 assert.Equal(t, tc.expKey, k)
218 assert.Equal(t, tc.expValue, v)
219 }
220 })
221 }
222 }
223
224 func TestParserEnvWithValue(t *testing.T) {
225 testCases := []struct {
226 name string
227 prefix string
228 delim string
229 key string
230 value string
231 expKey string
232 expValue any
233 cb func(key, value string) (string, any)
234 }{
235 {
236 name: "Nil cb",
237 prefix: "TESTVAR_",
238 delim: ".",
239 },
240 {
241 name: "Simple cb",
242 prefix: "TESTVAR_",
243 delim: ".",
244 key: "TestKey",
245 value: "TestVal",
246 expKey: "testkey",
247 expValue: "TestVal",
248 cb: func(key, value string) (string, any) {
249 return strings.ToLower(key), value
250 },
251 },
252 {
253 name: "Empty string nil cb",
254 prefix: "",
255 delim: ".",
256 },
257 {
258 name: "Cb is given",
259 prefix: "",
260 delim: ".",
261 key: "test_key",
262 value: "test_val",
263 expKey: "TEST.KEY",
264 expValue: "test_val",
265 cb: func(key, value string) (string, any) {
266 return strings.Replace(strings.ToUpper(key), "_", ".", -1), value
267 },
268 },
269 {
270 name: "Cb is given and changes value",
271 prefix: "",
272 delim: ".",
273 key: "test_key",
274 value: `{"foo": "bar"}`,
275 expKey: "TEST.KEY",
276 expValue: map[string]any{
277 "foo": "bar",
278 },
279 cb: func(key, value string) (string, any) {
280 key = strings.Replace(strings.ToUpper(key), "_", ".", -1)
281
282 var v map[string]any
283 err := json.Unmarshal([]byte(value), &v)
284 if err == nil {
285 return key, v
286 }
287 return key, value
288 },
289 },
290 }
291
292 for _, tc := range testCases {
293 t.Run(tc.name, func(t *testing.T) {
294 gotProvider := ParserEnvWithValue(tc.prefix, tc.delim, tc.cb)
295 if tc.cb == nil {
296 assert.Equal(t, tc.prefix, gotProvider.prefix)
297 assert.Equal(t, tc.delim, gotProvider.delim)
298 }
299 if tc.cb != nil {
300 k, v := gotProvider.cb(tc.key, tc.value)
301 assert.Equal(t, tc.expKey, k)
302 assert.Equal(t, tc.expValue, v)
303 }
304 })
305 }
306 }