-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
258 lines (209 loc) · 6.54 KB
/
config_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package yamlcfg
import (
"embed"
"fmt"
"testing"
"github.com/go-quicktest/qt"
)
type TestStruct struct {
SomeValue string `yaml:"some_value"`
SecondValue string `yaml:"second_value"`
}
func (t *TestStruct) Validate() error {
return nil
}
type TestStructWithFailingValidation struct {
SomeValue string `yaml:"some_value"`
}
func (t *TestStructWithFailingValidation) Validate() error {
return fmt.Errorf("this is going to fail")
}
//go:embed testdata/*
var testdata embed.FS
func TestParseFS(t *testing.T) {
t.Run("successfully load and unmarshals config", func(t *testing.T) {
cfg, err := ParseFS[TestStruct](testdata, "testdata/test1.yaml")
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.SomeValue, "this is for testing purposes"))
})
t.Run("fails to read unknown file", func(t *testing.T) {
cfg, err := ParseFS[TestStruct](testdata, "testdata/this_file_does_not_exist.yaml")
qt.Assert(t, qt.IsNotNil(err))
qt.Assert(t, qt.ErrorMatches(err, "reading config from embed.FS: .*"))
qt.Assert(t, qt.IsNil(cfg))
})
t.Run("fails to read wrong file type", func(t *testing.T) {
cfg, err := ParseFS[TestStruct](testdata, "testdata/gopher.png")
qt.Assert(t, qt.IsNotNil(err))
qt.Assert(t, qt.ErrorMatches(err, "unmarshalling config: yaml: .*"))
qt.Assert(t, qt.IsNil(cfg))
})
t.Run("fails to validate config struct", func(t *testing.T) {
cfg, err := ParseFS[TestStructWithFailingValidation](testdata, "testdata/test1.yaml")
qt.Assert(t, qt.IsNotNil(err))
qt.Assert(t, qt.ErrorMatches(err, "validating config: this is going to fail"))
qt.Assert(t, qt.IsNil(cfg))
})
}
func TestParseWithConfig(t *testing.T) {
t.Run("successfully load and unmarshals config with defaults", func(t *testing.T) {
cfg := &TestStruct{
SecondValue: "This is the default",
}
cfg, err := ParseWithConfig[TestStruct](cfg, "testdata/test1.yaml")
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.SomeValue, "this is for testing purposes"))
qt.Assert(t, qt.Equals(cfg.SecondValue, "This is the default"))
})
}
func TestParse(t *testing.T) {
t.Run("successfully load and unmarshals config", func(t *testing.T) {
cfg, err := Parse[TestStruct]("testdata/test1.yaml")
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.SomeValue, "this is for testing purposes"))
})
t.Run("successfully parses yaml config with default value", func(t *testing.T) {
cfg, err := Parse[TestStruct]("testdata/test_with_default.yaml")
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.SomeValue, "this_is_the_default"))
})
t.Run("fails to read unknown file", func(t *testing.T) {
cfg, err := Parse[TestStruct]("testdata/this_file_does_not_exist.yaml")
qt.Assert(t, qt.IsNotNil(err))
qt.Assert(t, qt.ErrorMatches(err, "reading config file: .*"))
qt.Assert(t, qt.IsNil(cfg))
})
t.Run("fails to read wrong file type", func(t *testing.T) {
cfg, err := Parse[TestStruct]("testdata/gopher.png")
qt.Assert(t, qt.IsNotNil(err))
qt.Assert(t, qt.ErrorMatches(err, "unmarshalling config: yaml: .*"))
qt.Assert(t, qt.IsNil(cfg))
})
t.Run("fails to validate config struct", func(t *testing.T) {
cfg, err := Parse[TestStructWithFailingValidation]("testdata/test1.yaml")
qt.Assert(t, qt.IsNotNil(err))
qt.Assert(t, qt.ErrorMatches(err, "validating config: this is going to fail"))
qt.Assert(t, qt.IsNil(cfg))
})
}
func TestUnmarshalConfig(t *testing.T) {
t.Run("valid config", func(t *testing.T) {
b := []byte(`name: test`)
cfg := struct {
Name string `yaml:"name"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Name, "test"))
})
t.Run("valid config with $ inside string", func(t *testing.T) {
t.Setenv("NAME", "testing")
b := []byte(`name: $NAME`)
cfg := struct {
Name string `yaml:"name"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Name, "$NAME"))
})
t.Run("valid config with ${ENV_VAR}", func(t *testing.T) {
t.Setenv("ENV_VAR", "testing")
b := []byte(`name: ${ENV_VAR}`)
cfg := struct {
Name string `yaml:"name"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Name, "testing"))
})
t.Run("valid config fallbacks to ENV_VAR default", func(t *testing.T) {
b := []byte(`name: ${ENV_VAR:default}`)
cfg := struct {
Name string `yaml:"name"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Name, "default"))
})
t.Run("valid config with env var in middle of string", func(t *testing.T) {
b := []byte(`name: "This should return ${ENV_VAR:default}"`)
cfg := struct {
Name string `yaml:"name"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Name, "This should return default"))
})
t.Run("valid config with multiple env vars", func(t *testing.T) {
b := []byte(`name: "${FIRST:default1} ${SECOND:default2}"`)
cfg := struct {
Name string `yaml:"name"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Name, "default1 default2"))
})
t.Run("should ignore dollar var in string", func(t *testing.T) {
b := []byte(`name: "${FIRST:default1} $thisisignored"`)
cfg := struct {
Name string `yaml:"name"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Name, "default1 $thisisignored"))
})
t.Run("randomly generated string with $", func(t *testing.T) {
b := []byte(`password: "my$password123"`)
cfg := struct {
Password string `yaml:"password"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Password, "my$password123"))
})
t.Run("url with $ symbole", func(t *testing.T) {
b := []byte(`url: "http://example.com/$path"`)
cfg := struct {
Url string `yaml:"url"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Url, "http://example.com/$path"))
})
t.Run("parses mixed input", func(t *testing.T) {
b := []byte(`mixed: "prefix${ENV_VAR:default}$suffix"`)
cfg := struct {
Mixed string `yaml:"mixed"`
}{}
if err := UnmarshalConfig(&cfg, b); err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(cfg.Mixed, "prefixdefault$suffix"))
})
t.Run("invalid config", func(t *testing.T) {
b := []byte(`asdasdasdad******`)
cfg := struct {
Name string `yaml:"name"`
}{}
if err := UnmarshalConfig(&cfg, b); err == nil {
t.Fatal("expected error")
}
})
}