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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
package config
import (
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"gopkg.in/yaml.v3"
)
func t(s string) []string {
if s == "" {
return []string{}
}
return strings.Split(s, ".")
}
func n(s string) (n yaml.Node) {
n.SetString(s)
return
}
func TestBuildOptions(tst *testing.T) {
tests := []struct {
name string
inp Map
opts Options
out Options
unknowns []string
}{
{"Empty", nil, Options{}, Options{}, nil},
{"Simple copy", nil, Options{MinFreq: 75}, Options{MinFreq: 75}, nil},
{"Unknowns", Map{"foo": 1}, Options{}, Options{}, []string{"foo"}},
{"Override", Map{"include-images": true}, Options{InclImages: false}, Options{InclImages: true}, nil},
{"Mixed", Map{"min-frequency": 24}, Options{MinFreq: 6, InclImages: true}, Options{MinFreq: 24, InclImages: true}, nil},
{"All",
Map{"max-frequency": 12, "include-images": true, "ignore-hash": true, "obsolete": 54},
Options{MinFreq: 6, InclImages: true, IgnHash: false},
Options{MinFreq: 6, InclImages: true, IgnHash: true},
[]string{"max-frequency", "obsolete"},
},
}
for _, tt := range tests {
tst.Run(tt.name, func(tst *testing.T) {
out, unk := buildOptions(&tt.opts, tt.inp)
if diff := cmp.Diff(tt.out, out); diff != "" {
tst.Error(diff)
}
sort.Strings(unk)
sort.Strings(tt.unknowns)
if diff := cmp.Diff(unk, tt.unknowns); diff != "" {
tst.Error(diff)
}
})
}
}
func TestBuildFeeds(tst *testing.T) {
tests := []struct {
name string
wantErr bool
target string
feeds []configGroupFeed
result Feeds
noAutoTarget bool
}{
{name: "Empty input", wantErr: false, target: "", feeds: nil, result: Feeds{}},
{name: "Empty Feed", wantErr: true, target: "",
feeds: []configGroupFeed{
{Target: n("foo"), Feed: feed{Url: "google.de"}},
}, result: Feeds{}},
{name: "Empty Feed", wantErr: true, target: "",
feeds: []configGroupFeed{
{Feed: feed{Url: "google.de"}},
}, result: Feeds{}},
{name: "Duplicate Feed Name", wantErr: true, target: "",
feeds: []configGroupFeed{
{Feed: feed{Name: "Dup"}},
{Feed: feed{Name: "Dup"}},
}, result: Feeds{}},
{name: "Simple", wantErr: false, target: "",
feeds: []configGroupFeed{
{Target: n("foo"), Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("foo")}},
},
{name: "Simple With Target", wantErr: false, target: "moep",
feeds: []configGroupFeed{
{Target: n("foo"), Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.foo")}},
},
{name: "Simple With Target and Whitespace", wantErr: false, target: "moep",
feeds: []configGroupFeed{
{Target: n("\r\nfoo "), Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.foo")}},
},
{name: "Simple With Target and NoAutoTarget", wantErr: false, target: "moep", noAutoTarget: true,
feeds: []configGroupFeed{
{Target: n("foo"), Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.foo")}},
},
{name: "Simple Without Target", wantErr: false, target: "moep",
feeds: []configGroupFeed{
{Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.muh")}},
},
{name: "Simple Without Target and NoAutoTarget", wantErr: false, target: "moep", noAutoTarget: true,
feeds: []configGroupFeed{
{Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep")}},
},
{name: "Simple With Nil Target", wantErr: false, target: "moep",
feeds: []configGroupFeed{
{Target: yaml.Node{Tag: "!!null"}, Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep")}},
},
{name: "Simple With Empty Target", wantErr: false, target: "moep",
feeds: []configGroupFeed{
{Target: n(""), Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep")}},
},
{name: "Simple With Blank Target", wantErr: false, target: "moep",
feeds: []configGroupFeed{
{Target: n(" "), Feed: feed{Name: "muh"}},
},
result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep")}},
},
{name: "Multiple Feeds", wantErr: false, target: "moep",
feeds: []configGroupFeed{
{Target: n("foo"), Feed: feed{Name: "muh"}},
{Feed: feed{Name: "bar"}},
},
result: Feeds{
"muh": &Feed{Name: "muh", Target: t("moep.foo")},
"bar": &Feed{Name: "bar", Target: t("moep.bar")},
},
},
{name: "Empty Group", wantErr: false, target: "",
feeds: []configGroupFeed{
{Group: group{Group: "G1"}},
},
result: Feeds{},
},
{name: "Simple Group", wantErr: false, target: "",
feeds: []configGroupFeed{
{Group: group{Group: "G1", Feeds: []configGroupFeed{
{Target: n("bar"), Feed: feed{Name: "F1"}},
{Target: n(""), Feed: feed{Name: "F2"}},
{Feed: feed{Name: "F3"}},
}}},
},
result: Feeds{
"F1": &Feed{Name: "F1", Target: t("G1.bar")},
"F2": &Feed{Name: "F2", Target: t("G1")},
"F3": &Feed{Name: "F3", Target: t("G1.F3")},
},
},
{name: "Simple Group, NoAutoTarget", wantErr: false, target: "IN", noAutoTarget: true,
feeds: []configGroupFeed{
{Group: group{Group: "G1", Feeds: []configGroupFeed{
{Target: n("bar"), Feed: feed{Name: "F1"}},
{Target: n(""), Feed: feed{Name: "F2"}},
{Feed: feed{Name: "F3"}},
}}},
},
result: Feeds{
"F1": &Feed{Name: "F1", Target: t("IN.bar")},
"F2": &Feed{Name: "F2", Target: t("IN")},
"F3": &Feed{Name: "F3", Target: t("IN")},
},
},
{name: "Nested Groups", wantErr: false, target: "",
feeds: []configGroupFeed{
{Group: group{Group: "G1", Feeds: []configGroupFeed{
{Feed: feed{Name: "F0"}},
{Target: n("bar"), Group: group{Group: "G2",
Feeds: []configGroupFeed{{Feed: feed{Name: "F1"}}}}},
{Target: n(""), Group: group{Group: "G3",
Feeds: []configGroupFeed{{Target: n("baz"), Feed: feed{Name: "F2"}}}}},
{Group: group{Group: "G4",
Feeds: []configGroupFeed{{Feed: feed{Name: "F3"}}}}},
}}},
},
result: Feeds{
"F0": &Feed{Name: "F0", Target: t("G1.F0")},
"F1": &Feed{Name: "F1", Target: t("G1.bar.F1")},
"F2": &Feed{Name: "F2", Target: t("G1.baz")},
"F3": &Feed{Name: "F3", Target: t("G1.G4.F3")},
},
},
}
for _, tt := range tests {
tst.Run(tt.name, func(tst *testing.T) {
var feeds = Feeds{}
var opts = Options{}
err := buildFeeds(tt.feeds, t(tt.target), feeds, &opts, !tt.noAutoTarget)
if (err != nil) != tt.wantErr {
tst.Errorf("buildFeeds() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.result, feeds); !tt.wantErr && diff != "" {
tst.Error(diff)
}
})
}
}
func defaultConfig(feeds []configGroupFeed, global Map) config {
defCfg := WithDefault()
return config{
Config: defCfg,
Feeds: feeds,
GlobalConfig: global,
}
}
func TestUnmarshal(tst *testing.T) {
tests := []struct {
name string
inp string
wantErr bool
config config
}{
{name: "Empty",
inp: "", wantErr: false, config: defaultConfig(nil, nil)},
{name: "Trash", inp: "Something", wantErr: true},
{name: "Simple config",
inp: "something: 1\nsomething_else: 2", wantErr: false, config: defaultConfig(nil, Map{"something": 1, "something_else": 2})},
{name: "Known config",
inp: "whatever: 2\ndefault-email: foo@foobar.de\ntimeout: 60\nsomething: 1", wantErr: false, config: func() config {
c := defaultConfig(nil, Map{"something": 1, "whatever": 2})
c.Timeout = 60
c.DefaultEmail = "foo@foobar.de"
return c
}()},
{name: "Known config with feed-options",
inp: "whatever: 2\ntimeout: 60\noptions:\n min-frequency: 6", wantErr: false, config: func() config {
c := defaultConfig(nil, Map{"whatever": 2})
c.Timeout = 60
c.FeedOptions.MinFreq = 6
return c
}()},
{name: "Known config with invalid feed-options",
inp: "options:\n max-frequency: 6", wantErr: true, config: config{}},
{name: "Config with feed",
inp: `
something: 1
feeds:
- name: Foo
url: whatever
target: bar
include-images: true
unknown-option: foo
`,
wantErr: false,
config: defaultConfig([]configGroupFeed{{
Target: n("bar"),
Feed: feed{
Name: "Foo",
Url: "whatever",
},
Options: Map{"include-images": true, "unknown-option": "foo"},
}}, Map{"something": 1})},
{name: "Feed with Exec",
inp: `
feeds:
- name: Foo
exec: [whatever, -i, http://foo.bar]
target: bar
include-images: true
unknown-option: foo
`,
wantErr: false,
config: defaultConfig([]configGroupFeed{{
Target: n("bar"),
Feed: feed{
Name: "Foo",
Exec: []string{"whatever", "-i", "http://foo.bar"},
},
Options: Map{"include-images": true, "unknown-option": "foo"},
}}, nil)},
{name: "Feeds",
inp: `
feeds:
- name: Foo
url: whatever
min-frequency: 2
- name: Shrubbery
url: google.de
target: bla
include-images: false
`,
wantErr: false,
config: defaultConfig([]configGroupFeed{
{
Feed: feed{
Name: "Foo",
Url: "whatever",
},
Options: Map{"min-frequency": 2},
},
{
Target: n("bla"),
Feed: feed{
Name: "Shrubbery",
Url: "google.de",
},
Options: Map{"include-images": false},
},
}, nil),
},
{name: "Empty Group",
inp: `
feeds:
- group: Foo
target: bla
`,
wantErr: false,
config: defaultConfig([]configGroupFeed{{Target: n("bla"), Group: group{"Foo", nil}}}, nil),
},
{name: "Feeds and Groups",
inp: `
feeds:
- name: Foo
url: whatever
- group: G1
target: target
feeds:
- group: G2
target: ""
feeds:
- name: F1
url: google.de
- name: F2
- group: G3
`,
wantErr: false,
config: defaultConfig([]configGroupFeed{
{Feed: feed{
Name: "Foo",
Url: "whatever",
}},
{Target: n("target"), Group: group{
Group: "G1",
Feeds: []configGroupFeed{
{Target: n(""), Group: group{
Group: "G2",
Feeds: []configGroupFeed{
{Feed: feed{Name: "F1", Url: "google.de"}},
}},
},
{Feed: feed{Name: "F2"}},
{Group: group{Group: "G3"}},
}},
},
}, nil),
},
}
eqNode := cmp.Comparer(func(l, r yaml.Node) bool {
return l.Tag == r.Tag && l.Value == r.Value
})
for _, tt := range tests {
tst.Run(tt.name, func(tst *testing.T) {
in := strings.NewReader(tt.inp)
got, err := unmarshal(in, WithDefault())
if (err != nil) != tt.wantErr {
tst.Errorf("parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
if diff := cmp.Diff(tt.config, got, eqNode); diff != "" {
tst.Error(diff)
}
}
})
}
}
|