-
Notifications
You must be signed in to change notification settings - Fork 8
/
filedata_test.go
248 lines (197 loc) · 6.69 KB
/
filedata_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
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
package conflate
import (
"errors"
pkgurl "net/url"
"testing"
"github.com/stretchr/testify/assert"
)
var errTest = errors.New("my error")
func testFiledataNew(t *testing.T, data []byte, path string) (filedata, error) {
t.Helper()
url, err := pkgurl.Parse(path)
assert.Nil(t, err)
return newFiledata(data, url)
}
func testFiledataNewAssert(t *testing.T, data []byte, path string) filedata {
t.Helper()
fd, err := testFiledataNew(t, data, path)
assert.Nil(t, err)
return fd
}
func TestFiledata_WrapErrorNil(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalJSON, "myurl")
assert.Nil(t, err)
err = fd.wrapError(nil)
assert.Nil(t, err)
}
func TestFiledata_WrapError(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalJSON, "myurl")
assert.Nil(t, err)
err = fd.wrapError(errTest)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "my error")
assert.Contains(t, err.Error(), "error processing myurl")
}
func TestFiledata_WrapErrorBlank(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalJSON, "")
assert.Nil(t, err)
err2 := fd.wrapError(errTest)
assert.NotNil(t, err2)
assert.Equal(t, errTest, err2)
}
func TestFiledata_JSONAsAny(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalJSON, "file")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_JSONAsUnknown(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalJSON, "file.unknown")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_JSONAsJSON(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalJSON, "file.json")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_JSONAsJSN(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalJSON, "file.jsn")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_JSONAsTOML(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalJSON, "file.toml")
assert.NotNil(t, err)
assert.Nil(t, fd.obj)
}
func TestFiledata_YAMLAsAny(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalYAML, "file")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_YAMLAsUnknown(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalYAML, "file.unknown")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_YAMLAsYAML(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalYAML, "file.yaml")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_YAMLAsYML(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalYAML, "file.yml")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_YAMLAsTOML(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalYAML, "file.toml")
assert.NotNil(t, err)
assert.Nil(t, fd.obj)
}
func TestFiledata_TOMLAsAny(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalTOML, "file")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_TOMLAsUnknown(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalTOML, "file.unknown")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_TOMLAsTOML(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalTOML, "file.toml")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_TOMLAsTML(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalTOML, "file.tml")
assert.Nil(t, err)
assert.Equal(t, fd.obj, testMarshalData)
}
func TestFiledata_TOMLAsJSON(t *testing.T) {
fd, err := testFiledataNew(t, testMarshalTOML, "file.json")
assert.NotNil(t, err)
assert.Nil(t, fd.obj)
}
func TestFiledata_NoIncludes(t *testing.T) {
fd, err := testLoader.wrapFiledata([]byte(`{"x": 1}`))
assert.Nil(t, err)
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}
func TestFiledata_BlankIncludes(t *testing.T) {
fd, err := testLoader.wrapFiledata([]byte(`{"includes":[], "x": 1}`))
assert.Nil(t, err)
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}
func TestFiledata_NullIncludes(t *testing.T) {
_, err := testLoader.wrapFiledata([]byte(`{"includes":null}`))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "not valid against the schema")
}
func TestFiledata_Includes(t *testing.T) {
fd, err := testLoader.wrapFiledata([]byte(`{"includes":["test1", "test2"], "x": 1}`))
assert.Nil(t, err)
assert.Equal(t, fd.includes, []string{"test1", "test2"})
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}
func TestFiledata_ExtractError(t *testing.T) {
old := getSchema
getSchema = func() map[string]interface{} { return map[string]interface{}{} }
defer func() { getSchema = old }()
_, err := testLoader.wrapFiledata([]byte(`{"includes": "not array"}`))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "could not extract includes")
}
func TestFiledata_IncludesError(t *testing.T) {
_, err := testLoader.wrapFiledata([]byte(`{"includes": "not array"}`))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "not valid against the schema")
}
func TestFiledata_Expand(t *testing.T) {
t.Setenv("W", "$W")
t.Setenv("X", `"x"`)
t.Setenv("Y", `y`)
t.Setenv("Z", `$Y`)
b := recursiveExpand([]byte(`{"W":"$W","X":$X,"Y":"$Y","Z":"$Z"}`))
assert.Equal(t, string(b), string(`{"W":"$W","X":"x","Y":"y","Z":"y"}`))
}
func TestFiledatas_Unmarshal(t *testing.T) {
fds := filedatas{
testFiledataNewAssert(t, testMarshalJSON, "file.json"),
testFiledataNewAssert(t, testMarshalYAML, "file.yaml"),
testFiledataNewAssert(t, testMarshalTOML, "file.toml"),
}
assert.Equal(t, fds.objs(), []interface{}{testMarshalData, testMarshalData, testMarshalData})
}
func TestFiledatas_DifferentIncludes(t *testing.T) {
old := Includes
Includes = "use"
defer func() { Includes = old }()
fd, err := testLoader.wrapFiledata([]byte(`{"use":["test1", "test2"], "x": 1}`))
assert.Nil(t, err)
assert.Equal(t, fd.includes, []string{"test1", "test2"})
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}
func TestFiledatas_NoIncludes(t *testing.T) {
old := Includes
Includes = "using"
defer func() { Includes = old }()
fd, err := testLoader.wrapFiledata([]byte(`{"includes":["test1", "test2"]}`))
assert.Nil(t, err)
assert.Empty(t, fd.includes)
assert.Equal(t, fd.obj, map[string]interface{}{"includes": []interface{}{"test1", "test2"}})
}
func TestFiledatas_IgnoreIncludes(t *testing.T) {
old := Includes
Includes = ""
defer func() { Includes = old }()
fd, err := testLoader.wrapFiledata([]byte(`{"":["test1", "test2"]}`))
assert.Nil(t, err)
assert.Empty(t, fd.includes)
assert.Equal(t, fd.obj, map[string]interface{}{"": []interface{}{"test1", "test2"}})
}