-
Notifications
You must be signed in to change notification settings - Fork 9
/
fs_test.go
112 lines (102 loc) · 2.3 KB
/
fs_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
// Copyright (c) 2024 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
package fs_test
import (
"errors"
"io/fs"
"testing"
"testing/fstest"
"github.com/nil-go/konf/internal/assert"
kfs "github.com/nil-go/konf/provider/fs"
)
func TestFS_empty(t *testing.T) {
var loader kfs.FS
values, err := loader.Load()
assert.EqualError(t, err, "read file: readfile : invalid argument")
assert.Equal(t, nil, values)
}
func TestFS_Load(t *testing.T) {
t.Parallel()
testcases := []struct {
description string
fs fs.FS
path string
opts []kfs.Option
expected map[string]any
err string
}{
{
description: "empty",
err: "read file: readfile : invalid argument",
},
{
description: "empty path",
fs: fstest.MapFS{
"config.json": {
Data: []byte(`{"p":{"k":"v"}}`),
},
},
err: "read file: open : file does not exist",
},
{
description: "nil fs",
path: "config.json",
expected: map[string]any{
"p": map[string]any{
"k": "v",
},
},
},
{
description: "fs file",
fs: fstest.MapFS{
"config.json": {
Data: []byte(`{"p":{"k":"v"}}`),
},
},
path: "config.json",
expected: map[string]any{
"p": map[string]any{
"k": "v",
},
},
},
{
description: "fs file (not exist)",
fs: fstest.MapFS{},
path: "not_found.json",
err: "read file: open not_found.json: file does not exist",
},
{
description: "unmarshal error",
fs: fstest.MapFS{
"config.json": {
Data: []byte(`{"p":{"k":"v"}}`),
},
},
path: "config.json",
opts: []kfs.Option{
kfs.WithUnmarshal(func([]byte, any) error {
return errors.New("unmarshal error")
}),
},
err: "unmarshal: unmarshal error",
},
}
for _, testcase := range testcases {
t.Run(testcase.description, func(t *testing.T) {
t.Parallel()
values, err := kfs.New(testcase.fs, testcase.path, testcase.opts...).Load()
if testcase.err != "" {
assert.EqualError(t, err, testcase.err)
} else {
assert.NoError(t, err)
assert.Equal(t, testcase.expected, values)
}
})
}
}
func TestFS_String(t *testing.T) {
t.Parallel()
assert.Equal(t, "fs:///config.json", kfs.New(fstest.MapFS{}, "config.json").String())
}