-
Notifications
You must be signed in to change notification settings - Fork 11
/
configurator_test.go
253 lines (210 loc) · 5.65 KB
/
configurator_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
package configuration
import (
"net"
"os"
"testing"
"time"
)
// nolint:paralleltest
// t.Setenv doesn't work with t.Parallel()
func TestConfigurator(t *testing.T) {
// setting command line flag
os.Args = []string{"smth", "-name=flag_value"}
// test file
fileName := "./testdata/input.json"
// setting env variable
t.Setenv("AGE_ENV", "45")
expectedURLs := []string{
"http://localhost:3000",
"1.2.3.4:8080",
}
// defining a struct
cfg := struct {
Name string `flag:"name"`
LastName string `default:"defaultLastName"`
Age byte `env:"AGE_ENV" default:"-1"`
BoolPtr *bool `default:"false"`
ObjPtr *struct {
F32 float32 `default:"32"`
StrPtr *string `default:"str_ptr_test"`
HundredMS time.Duration `default:"100ms"` // nolint:stylecheck
}
Obj struct {
IntPtr *int16 `default:"123"`
Beta int `file_json:"inside.beta" default:"24"`
StrSlice []string `default:"one;two"`
IntSlice []int64 `default:"3; 4"`
unexported string `xml:"ignored"` // nolint:govet
}
URLs []*string `default:"http://localhost:3000;1.2.3.4:8080"`
HostIP ipTest `default:"127.0.0.3"`
}{}
configurator := New(
&cfg,
// order of execution will be preserved:
NewFlagProvider(), // 1st
NewEnvProvider(), // 2nd
NewJSONFileProvider(fileName), // 3rd
NewDefaultProvider(), // 4th
)
if err := configurator.InitValues(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
assert(t, "flag_value", cfg.Name)
assert(t, "defaultLastName", cfg.LastName)
assert(t, byte(45), cfg.Age)
assert(t, true, cfg.BoolPtr != nil)
assert(t, false, *cfg.BoolPtr)
assert(t, true, cfg.ObjPtr != nil)
assert(t, float32(32), cfg.ObjPtr.F32)
assert(t, true, cfg.ObjPtr.StrPtr != nil)
assert(t, "str_ptr_test", *cfg.ObjPtr.StrPtr)
assert(t, true, cfg.Obj.IntPtr != nil)
assert(t, int16(123), *cfg.Obj.IntPtr)
assert(t, int(42), cfg.Obj.Beta)
assert(t, []string{"one", "two"}, cfg.Obj.StrSlice)
assert(t, []int64{3, 4}, cfg.Obj.IntSlice)
assert(t, time.Millisecond*100, cfg.ObjPtr.HundredMS)
assert(t, net.ParseIP("127.0.0.3"), net.IP(cfg.HostIP))
for i := range expectedURLs {
assert(t, expectedURLs[i], *cfg.URLs[i])
}
}
func TestConfigurator_Errors(t *testing.T) {
t.Parallel()
tests := map[string]struct {
input any
providers []Provider
}{
"empty providers": {
input: &struct{}{},
providers: []Provider{},
},
"non-pointer": {
input: struct{}{},
providers: []Provider{
NewDefaultProvider(),
},
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
err := New(test.input, test.providers...).InitValues()
if err == nil {
t.Fatal("expected error but got nil")
}
})
}
}
func TestEmbeddedFlags(t *testing.T) {
t.Parallel()
type (
Client struct {
ServerAddress string `flag:"addr|127.0.0.1:443|server address"`
}
Config struct {
Client *Client
}
)
os.Args = []string{"smth", "-addr=addr_value"}
var cfg Config
if err := New(&cfg, NewFlagProvider()).InitValues(); err != nil {
t.Fatal("unexpected err: ", err)
}
assert(t, true, cfg.Client != nil)
assert(t, cfg.Client.ServerAddress, "addr_value")
}
// nolint:paralleltest
func TestFallBackToDefault(t *testing.T) {
// defining a struct
cfg := struct {
NameFlag string `flag:"name_flag||Some description" default:"default_val"`
}{}
c := New(&cfg,
NewFlagProvider(),
NewDefaultProvider(),
)
if err := c.InitValues(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
assert(t, "default_val", cfg.NameFlag)
}
func TestSetOnFailFn(t *testing.T) {
t.Parallel()
cfg := struct {
Name string `default:"test_name"`
}{}
onFailFn := func(err error) {
if err != nil && err.Error() != "configurator: field [Name] with tags [default:\"test_name\"] cannot be set. Last Provider error: no tag" {
t.Fatalf("unexpected error: %v", err)
}
}
c := New(
&cfg,
NewFlagProvider(),
).
SetOptions(
OnFailFnOpt(onFailFn),
)
if err := c.InitValues(); err != nil {
t.Fatal("unexpected err: ", err)
}
}
func TestProviderName(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
provider Provider
expectedName string
}{
DefaultProviderName: {
provider: NewDefaultProvider(),
expectedName: DefaultProviderName,
},
EnvProviderName: {
provider: NewEnvProvider(),
expectedName: EnvProviderName,
},
FlagProviderName: {
provider: NewFlagProvider(),
expectedName: FlagProviderName,
},
JSONFileProviderName: {
provider: NewJSONFileProvider(""),
expectedName: JSONFileProviderName,
},
}
for name, test := range testCases {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
assert(t, test.expectedName, test.provider.Name())
})
}
}
func TestConfigurator_NameCollision(t *testing.T) {
t.Parallel()
err := New(&struct{}{}, NewDefaultProvider(), NewDefaultProvider()).InitValues()
assert(t, ErrProviderNameCollision, err)
}
func TestConfigurator_FailedProvider(t *testing.T) {
t.Parallel()
err := New(&struct{}{}, NewJSONFileProvider("doesn't exist")).InitValues()
assert(t, "cannot init [JSONFileProvider] provider: JSONFileProvider.Init: open doesn't exist: no such file or directory", err.Error())
}
// nolint:paralleltest
// t.Setenv doesn't work with t.Parallel()
func Test_FromEnvAndDefault(t *testing.T) {
t.Setenv("AGE", "24")
type st struct {
Name string `env:"name" default:"Alex"`
Age int `env:"AGE" default:"42"`
}
cfg := st{}
if err := FromEnvAndDefault(&cfg); err != nil {
t.Fatal("unexpected err", err)
}
assert(t, cfg.Name, "Alex")
assert(t, cfg.Age, 24)
}