-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile_test.go
156 lines (144 loc) · 3.11 KB
/
file_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
package venom
import (
"encoding/json"
"io/ioutil"
"os"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRegisterExtension(t *testing.T) {
testIO := []struct {
tc string
key string
loader IOFileLoader
}{
{
tc: "should load JSONLoader",
key: jsonKey,
loader: JSONLoader,
},
}
for _, test := range testIO {
t.Run(test.tc, func(t *testing.T) {
RegisterExtension(test.key, test.loader)
assert.Contains(t, extensionMap, test.key)
})
}
}
func getJSONFileErr(file string) error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
d := make(map[string]interface{})
return json.Unmarshal(data, &d)
}
func TestLoadFile(t *testing.T) {
testIO := []struct {
tc string
filename string
err error
expect ConfigMap
}{
{
tc: "should load JSON file",
filename: "testdata/config.json",
expect: ConfigMap{
"foo": "bar",
"level": 5.0,
},
},
{
tc: "should load JSON file with nested objects",
filename: "testdata/config_nested.json",
expect: ConfigMap{
"foo": "bar",
"level": 5.0,
"log": ConfigMap{
"level": "info",
"file": "/usr/local/example.log",
},
},
},
{
tc: "should error on non-existent file",
filename: "testdata/missing.config.json",
err: &os.PathError{
Op: "open",
Path: "testdata/missing.config.json",
Err: syscall.ENOENT,
},
expect: nil,
},
{
tc: "should error on unknown file extension",
filename: "testdata/config.xml",
err: ErrNoFileLoader{ext: "xml"},
expect: nil,
},
{
tc: "should error on invalid file contents",
filename: "testdata/invalid/config.bad.json",
err: getJSONFileErr("testdata/invalid/config.bad.json"),
expect: nil,
},
}
for _, test := range testIO {
t.Run(test.tc, func(t *testing.T) {
v := New()
err := v.LoadFile(test.filename)
assertEqualErrors(t, test.err, err)
st := v.Store.(*DefaultConfigStore)
assert.EqualValues(t, test.expect, st.config[FileLevel])
})
}
}
func TestLoadDirectory(t *testing.T) {
testIO := []struct {
tc string
dir string
recurse bool
err error
expect ConfigMap
}{
{
tc: "should load single directory",
dir: "testdata",
recurse: false,
expect: ConfigMap{
"foo": "bar",
"level": 5.0,
"log": ConfigMap{
"level": "info",
"file": "/usr/local/example.log",
},
},
},
{
tc: "should recursively load directories",
dir: "testdata/sub",
recurse: true,
expect: ConfigMap{
"foo": "baz",
"level": 5.0,
"and": "another",
},
},
{
tc: "should error if directory contains invalid files",
dir: "testdata/invalid",
recurse: false,
err: getJSONFileErr("testdata/invalid/config.bad.json"),
},
}
for _, test := range testIO {
t.Run(test.tc, func(t *testing.T) {
v := New()
err := v.LoadDirectory(test.dir, test.recurse)
assertEqualErrors(t, test.err, err)
st := v.Store.(*DefaultConfigStore)
assert.EqualValues(t, st.config[FileLevel], test.expect)
})
}
}