-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_test.go
215 lines (178 loc) · 4.09 KB
/
load_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
package snuffler
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestLoad(t *testing.T) {
type D struct {
A int
B string
C []string
}
type E struct {
A int
B string
C []string
F string
}
type Conf struct {
A int
B string
C []string
D D
E []E
}
simple := Conf{
A: 1,
B: "b",
C: []string{"a", "b", "c"},
D: D{
A: 1,
B: "b",
C: []string{"a", "b", "c"},
},
E: []E{
E{
A: 1,
B: "b",
C: []string{"a", "b", "c"},
},
E{
F: "ggg",
},
},
}
Convey("When loading a single file", t, func() {
Convey("It should load based on specified filetypes, such as", func() {
Convey("YAML", func() {
cf := &configFile{
fileName: "_testdata/simple.yaml",
fileType: yamlType,
}
var conf Conf
err := unmarshalFile(cf, &conf)
So(err, ShouldBeNil)
So(conf, ShouldResemble, simple)
})
Convey("TOML", func() {
cf := &configFile{
fileName: "_testdata/simple.toml",
fileType: tomlType,
}
var conf Conf
err := unmarshalFile(cf, &conf)
So(err, ShouldBeNil)
So(conf, ShouldResemble, simple)
})
Convey("JSON", func() {
cf := &configFile{
fileName: "_testdata/simple.json",
fileType: jsonType,
}
var conf Conf
err := unmarshalFile(cf, &conf)
So(err, ShouldBeNil)
So(conf, ShouldResemble, simple)
})
})
Convey("It should try all types if it doesn't know the filetype", func() {
Convey("And succeed with", func() {
Convey("YAML", func() {
cf := &configFile{
fileName: "_testdata/simple.yaml",
fileType: unknownType,
}
var conf Conf
err := unmarshalFile(cf, &conf)
So(err, ShouldBeNil)
So(conf, ShouldResemble, simple)
})
Convey("TOML", func() {
cf := &configFile{
fileName: "_testdata/simple.toml",
fileType: unknownType,
}
var conf Conf
err := unmarshalFile(cf, &conf)
So(err, ShouldBeNil)
So(conf, ShouldResemble, simple)
})
Convey("JSON", func() {
cf := &configFile{
fileName: "_testdata/simple.json",
fileType: unknownType,
}
var conf Conf
err := unmarshalFile(cf, &conf)
So(err, ShouldBeNil)
So(conf, ShouldResemble, simple)
})
})
Convey("But fail with bad data", func() {
cf := &configFile{
fileName: "_testdata/bad-wolf",
fileType: unknownType,
}
var conf Conf
err := unmarshalFile(cf, &conf)
So(err.Error(), ShouldStartWith, "couldn't read _testdata/bad-wolf as YAML/JSON")
})
})
})
Convey("Unmarshallers should error on bad file names", t, func() {
var conf Conf
Convey("YAML", func() {
err := unmarshalYAML(&configFile{fileName: "bad!wolf"}, &conf)
So(err, ShouldNotBeNil)
})
Convey("JSON", func() {
err := unmarshalJSON(&configFile{fileName: "bad!wolf"}, &conf)
So(err, ShouldNotBeNil)
})
})
Convey("When loading multiple files", t, func() {
snoot := New(&Conf{})
Convey("It should work with no files", func() {
var conf Conf
err := snoot.unmarshalFiles(&conf)
So(err, ShouldBeNil)
So(conf, ShouldResemble, Conf{})
})
Convey("It should work with one file", func() {
var conf Conf
snoot.MaybeAddFile("_testdata/simple.yaml")
err := snoot.unmarshalFiles(&conf)
So(err, ShouldBeNil)
So(conf, ShouldResemble, simple)
})
Convey("It should override with multiple files", func() {
type Override struct {
A int
B string
E int
D bool
}
var override Override
snoot.MaybeAddFile("_testdata/overridden.yaml")
snoot.MaybeAddFile("_testdata/override.yaml")
err := snoot.unmarshalFiles(&override)
So(err, ShouldBeNil)
So(override, ShouldResemble, Override{
// from both, equal
A: 1,
// from both, overridden
B: "b",
// from override, added
E: 42,
// from overridden, kept
D: true,
})
})
Convey("It should surface errors", func() {
var conf Conf
snoot.MaybeAddFile("_testdata/bad-wolf")
err := snoot.unmarshalFiles(&conf)
So(err.Error(), ShouldStartWith, "couldn't read _testdata/bad-wolf as YAML/JSON")
})
})
}