File: dotenv_test.go

package info (click to toggle)
golang-github-knadh-koanf 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 808 kB
  • sloc: sh: 31; makefile: 14
file content (213 lines) | stat: -rw-r--r-- 4,549 bytes parent folder | download
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
package dotenv

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestDotEnv_Marshal(t *testing.T) {
	de := DotEnv{}
	testCases := []struct {
		name   string
		cfg    map[string]interface{}
		expOut []byte
		err    error
	}{
		{
			name:   "Empty config",
			cfg:    map[string]interface{}{},
			expOut: []byte{},
		},
		{
			name: "Simple key-value pair",
			cfg: map[string]interface{}{
				"key": "value",
			},
			expOut: []byte("key=\"value\""),
		},
		{
			name: "Multiple key-values",
			cfg: map[string]interface{}{
				"key_1": "value-1",
				"key_2": "value-2",
				"key_3": "value-3",
			},
			expOut: []byte("key_1=\"value-1\"\nkey_2=\"value-2\"\nkey_3=\"value-3\""),
		},
		{
			name: "Mixed data types",
			cfg: map[string]interface{}{
				"int_key":   12,
				"bool_key":  true,
				"arr_key":   []int{1, 2, 3, 4},
				"float_key": 10.5,
			},
			expOut: []byte("arr_key=\"[1 2 3 4]\"\nbool_key=\"true\"\nfloat_key=\"10.5\"\nint_key=12"),
		},
		{
			name: "Nested config",
			cfg: map[string]interface{}{
				"map_key": map[string]interface{}{
					"arr_key":  []float64{1.2, 4.3, 5, 6},
					"bool_key": false,
					"inner_map_key": map[interface{}]interface{}{
						0: "zero",
						1: 1.0,
					},
					"int_key": 12,
				},
			},
			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]"`),
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			out, err := de.Marshal(tc.cfg)
			assert.Equal(t, tc.err, err)
			assert.Equal(t, string(tc.expOut), string(out))
		})
	}
}

func TestDotEnv_Unmarshal(t *testing.T) {
	de := DotEnv{}
	testCases := []struct {
		name   string
		cfg    []byte
		expOut map[string]interface{}
		err    error
	}{
		{
			name:   "Empty config",
			expOut: map[string]interface{}{},
		},
		{
			name: "Simple key-value",
			cfg:  []byte(`key="value"`),
			expOut: map[string]interface{}{
				"key": "value",
			},
		},
		{
			name: "Multiple key-values",
			cfg:  []byte("key_1=\"value-1\"\nkey_2=\"value-2\""),
			expOut: map[string]interface{}{
				"key_1": "value-1",
				"key_2": "value-2",
			},
		},
		{
			name: "Mixed data types",
			cfg:  []byte("arr=\"[1 2 3 4]\"\nbool=\"true\"\nfloat=\"12.5\"\nint=\"32\"\n"),
			expOut: map[string]interface{}{
				"arr":   "[1 2 3 4]",
				"bool":  "true",
				"float": "12.5",
				"int":   "32",
			},
		},
		{
			name: "Nested config",
			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]"`),
			expOut: map[string]interface{}{
				"map_key": "map[arr_key:[1 4 5 6] bool_key:false inner_map_key:map[0:zero 1:1] int_key:12]",
			},
		},
		{
			name: "Missing value",
			cfg:  []byte(`key=`),
			expOut: map[string]interface{}{
				"key": "",
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			outMap, err := de.Unmarshal(tc.cfg)
			assert.Equal(t, tc.err, err)
			assert.Equal(t, tc.expOut, outMap)
		})
	}
}

func TestCompareToEnvProvider(t *testing.T) {

	testCases := []struct {
		name     string
		prefix   string
		delim    string
		key      string
		value    string
		expKey   string
		expValue string
		cb       func(key string) string
		want     *DotEnv
	}{
		{
			name:   "Nil cb",
			prefix: "TESTVAR_",
			delim:  ".",
			want: &DotEnv{
				prefix: "TESTVAR_",
				delim:  ".",
			},
		},
		{
			name:     "Simple cb",
			prefix:   "TESTVAR_",
			delim:    ".",
			key:      "TestKey",
			value:    "TestVal",
			expKey:   "testkey",
			expValue: "TestVal",
			cb: func(key string) string {
				return strings.ToLower(key)
			},
			want: &DotEnv{
				prefix: "TESTVAR_",
				delim:  ".",
			},
		},
		{
			name:   "Empty string nil cb",
			prefix: "",
			delim:  ".",
			want: &DotEnv{
				prefix: "",
				delim:  ".",
			},
		},
		{
			name:     "Cb is given",
			prefix:   "",
			delim:    ".",
			key:      "test_key",
			value:    "test_val",
			expKey:   "TEST.KEY",
			expValue: "test_val",
			cb: func(key string) string {
				return strings.Replace(strings.ToUpper(key), "_", ".", -1)
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			gotProvider := ParserEnv(tc.prefix, tc.delim, tc.cb)
			if tc.cb == nil {
				assert.Equal(t, tc.prefix, gotProvider.prefix)
				assert.Equal(t, tc.delim, gotProvider.delim)
				// do not compare cb or reverseCB
			}
			if tc.cb != nil {
				k, v := gotProvider.cb(tc.key, tc.value)
				assert.Equal(t, tc.expKey, k)
				assert.Equal(t, tc.expValue, v)
			}
		})
	}
}