-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf_test.go
207 lines (198 loc) · 7.02 KB
/
conf_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
package conf_test
import (
"testing"
"time"
"github.com/go-chai/conf"
"github.com/stretchr/testify/require"
)
var onlyDefaults = &defaultOptions{
IntDefault: 1,
Float64Default: -3.14,
StringDefault: "abc",
TimeDefault: 1 * time.Minute,
MapDefault: map[string]int{
"a": 1,
},
Map: map[string]int{},
SliceDefault: []int{1, 2},
}
var nonDefaultOverrides = &defaultOptions{
Int: 3,
IntDefault: 1,
Float64: 2.712,
Float64Default: -3.14,
NumericFlag: false,
String: "asdf",
StringDefault: "abc",
Time: 13 * time.Second,
TimeDefault: 1 * time.Minute,
Map: map[string]int{
"val1": 3,
"val2": 4,
},
MapDefault: map[string]int{
"a": 1,
},
Slice: []int{1, 2, 3},
SliceDefault: []int{1, 2},
}
var defaultOverrides = &defaultOptions{
Int: 3,
IntDefault: 13,
Float64: 2.712,
Float64Default: 1.1234,
NumericFlag: false,
String: "asdf",
StringDefault: "defg",
Time: 13 * time.Second,
TimeDefault: 11 * time.Minute,
Map: map[string]int{
"val1": 3,
"val2": 4,
},
MapDefault: map[string]int{
"val21": 21,
"val22": 22,
},
Slice: []int{1, 2, 3},
SliceDefault: []int{4, 5, 6},
}
var flagOverrides = &defaultOptions{
Int: 4,
IntDefault: 5,
Float64: 2.712,
Float64Default: 1.1234,
NumericFlag: false,
String: "asdf",
StringDefault: "defg",
Time: 17 * time.Second,
TimeDefault: 19 * time.Second,
Map: map[string]int{
"val1": 3,
"val2": 4,
},
MapDefault: map[string]int{
"val21": 21,
"val22": 22,
},
Slice: []int{1, 2, 3},
SliceDefault: []int{4, 5, 6},
}
func Test_Load_ConfigFiles(t *testing.T) {
var tcs = map[string]struct {
opts []conf.ConfOption
expected any
}{
"no args > no config": {
opts: []conf.ConfOption{conf.Args([]string{})},
expected: onlyDefaults,
},
"no args > paths > empty YAML": {
opts: []conf.ConfOption{conf.Paths("testdata/config-empty.yaml"), conf.Args([]string{})},
expected: onlyDefaults,
},
"conf args > empty YAML": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", ""), conf.Args([]string{"--conf=testdata/config-empty.yaml"})},
expected: onlyDefaults,
},
"no args > default flag config path > empty YAML": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config-empty.yaml"), conf.Args([]string{})},
expected: onlyDefaults,
},
"no args > paths > empty YML": {
opts: []conf.ConfOption{conf.Paths("testdata/config-empty.yml"), conf.Args([]string{})},
expected: onlyDefaults,
},
"conf args > empty YML": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", ""), conf.Args([]string{"--conf=testdata/config-empty.yml"})},
expected: onlyDefaults,
},
"no args > default flag config path > empty YML": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config-empty.yml"), conf.Args([]string{})},
expected: onlyDefaults,
},
"no args > paths > empty TOML": {
opts: []conf.ConfOption{conf.Paths("testdata/config-empty.toml"), conf.Args([]string{})},
expected: onlyDefaults,
},
"conf args > empty TOML": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", ""), conf.Args([]string{"--conf=testdata/config-empty.toml"})},
expected: onlyDefaults,
},
"no args > default flag config path > empty TOML": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config-empty.toml"), conf.Args([]string{})},
expected: onlyDefaults,
},
"no args > paths > empty JSON": {
opts: []conf.ConfOption{conf.Paths("testdata/config-empty.json"), conf.Args([]string{})},
expected: onlyDefaults,
},
"conf args > empty JSON": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", ""), conf.Args([]string{"--conf=testdata/config-empty.json"})},
expected: onlyDefaults,
},
"no args > default flag config path > empty JSON": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config-empty.json"), conf.Args([]string{})},
expected: onlyDefaults,
},
"no args > default flag config path > YAML with non-default overrides": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config-default.yaml"), conf.Args([]string{})},
expected: nonDefaultOverrides,
},
"no args > default flag config path > YAML with default overrides": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config.yaml"), conf.Args([]string{})},
expected: defaultOverrides,
},
"value args > default flag config path > YAML with default overrides": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config.yaml"), conf.Args([]string{"--i=4", "--id=5", "--t=17s", "--td=19s"})},
expected: flagOverrides,
},
"no args > default flag config path > TOML with non-default overrides": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config-default.toml"), conf.Args([]string{})},
expected: nonDefaultOverrides,
},
"no args > default flag config path > TOML with default overrides": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config.toml"), conf.Args([]string{})},
expected: defaultOverrides,
},
"value args > default flag config path > TOML with default overrides": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config.toml"), conf.Args([]string{"--i=4", "--id=5", "--t=17s", "--td=19s"})},
expected: flagOverrides,
},
"no args > paths > TOML with non-default overrides": {
opts: []conf.ConfOption{conf.Paths("testdata/config-default.toml"), conf.Args([]string{})},
expected: nonDefaultOverrides,
},
"no args > paths > TOML with default overrides": {
opts: []conf.ConfOption{conf.Paths("testdata/config.toml"), conf.Args([]string{})},
expected: defaultOverrides,
},
"value args > paths > TOML with default overrides": {
opts: []conf.ConfOption{conf.Paths("testdata/config.toml"), conf.Args([]string{"--i=4", "--id=5", "--t=17s", "--td=19s"})},
expected: flagOverrides,
},
"no args > paths > JSON with non-default overrides": {
opts: []conf.ConfOption{conf.Paths("testdata/config-default.json"), conf.Args([]string{})},
expected: nonDefaultOverrides,
},
"no args > paths > JSON with default overrides": {
opts: []conf.ConfOption{conf.Paths("testdata/config.json"), conf.Args([]string{})},
expected: defaultOverrides,
},
"value args > paths > JSON with default overrides": {
opts: []conf.ConfOption{conf.Paths("testdata/config.json"), conf.Args([]string{"--i=4", "--id=5", "--t=17s", "--td=19s"})},
expected: flagOverrides,
},
"value args > default flag config path > YAML with non-default overrides > TOML with default overrides": {
opts: []conf.ConfOption{conf.ConfigFlag("conf", "testdata/config.yaml", "testdata/config.toml"), conf.Args([]string{"--i=4", "--id=5", "--t=17s", "--td=19s"})},
expected: flagOverrides,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
cfg, err := conf.Load[defaultOptions](conf.ConfOptions(tc.opts))
require.NoError(t, err)
require.Equal(t, tc.expected, cfg)
})
}
}