1
|
|
|
package configuration |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"github.com/vvval/go-metadata-scanner/util" |
5
|
|
|
"gopkg.in/yaml.v2" |
6
|
|
|
"reflect" |
7
|
|
|
"testing" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
func TestAppLoad(t *testing.T) { |
11
|
|
|
type check struct { |
12
|
|
|
conf testConfig |
13
|
|
|
file string |
14
|
|
|
exp testConfig |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
set := []check{ |
18
|
|
|
{testConfig{"s", []string{"a1", "a2"}}, "", testConfig{"s", []string{"a1", "a2"}}}, |
19
|
|
|
{testConfig{"s", []string{"a3", "a4"}}, "./fixtures/test4.yaml", testConfig{"s", []string{"a3", "a4"}}}, |
20
|
|
|
{testConfig{"s", []string{}}, "./fixtures/test1.yaml", testConfig{"str", []string{"arr1", "arr2"}}}, |
21
|
|
|
{testConfig{"s", []string{"a5", "a6"}}, "./fixtures/test1.yaml", testConfig{"str", []string{"arr1", "arr2", "a5", "a6"}}}, |
22
|
|
|
{testConfig{"s", []string{"a7", "a8"}}, "./fixtures/test2.yaml", testConfig{"str", []string{"a7", "a8"}}}, |
23
|
|
|
{testConfig{"s", []string{"a9", "a10"}}, "./fixtures/test3.yaml", testConfig{"s", []string{"arr1", "arr2", "a9", "a10"}}}, |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
for i, s := range set { |
27
|
|
|
l := Load(s.conf, s.file) |
28
|
|
|
if !reflect.DeepEqual(l, s.exp) { |
29
|
|
|
t.Errorf("load failed (line `%d`):\ngot `%s`\nexpected `%s`", i, l, s.exp) |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
type testSchema struct { |
35
|
|
|
StringValue string `yaml:"string"` |
36
|
|
|
ArrayValue []string `yaml:"array"` |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
func (c testConfig) Schema() Schema { |
40
|
|
|
return testSchema{} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
func (s testSchema) Parse(data []byte) (Config, error) { |
44
|
|
|
err := yaml.Unmarshal(data, &s) |
45
|
|
|
if err != nil { |
46
|
|
|
return testConfig{}, err |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return testConfig{s.StringValue, s.ArrayValue}, nil |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
func (c testConfig) MergeDefault(conf Config) Config { |
53
|
|
|
if len(c.stringValue) == 0 { |
54
|
|
|
c.stringValue = conf.(testConfig).stringValue |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
c.arrayValue = util.UniqueValues(append(c.arrayValue, conf.(testConfig).arrayValue...)) |
58
|
|
|
|
59
|
|
|
return c |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
type testConfig struct { |
63
|
|
|
stringValue string |
64
|
|
|
arrayValue []string |
65
|
|
|
} |
66
|
|
|
|