-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_test.go
122 lines (104 loc) · 2.44 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
package gonfig
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewFileProvider(t *testing.T) {
fp := NewFileProvider("file.yml")
require.NotNil(t, fp)
assert.Equal(t, "file.yml", fp.FilePath)
assert.Equal(t, ".yml", fp.FileExt)
assert.False(t, fp.Required)
}
func TestFileProvider_Name(t *testing.T) {
fp := FileProvider{
FileExt: ".json",
}
assert.Equal(t, "File provider (json)", fp.Name())
}
func TestFileProvider_UnmarshalStruct(t *testing.T) {
t.Run("file existence", func(t *testing.T) {
fp := FileProvider{
FilePath: "NotExistingFile.toml",
FileExt: ".toml",
Required: false,
}
var i interface{}
err := fp.UnmarshalStruct(i)
assert.NoError(t, err)
fp.Required = true
err = fp.UnmarshalStruct(i)
assert.Error(t, err)
})
t.Run("unsupported file extension", func(t *testing.T) {
fp := FileProvider{
FileExt: ".ini",
}
var i interface{}
err := fp.UnmarshalStruct(i)
require.Error(t, err)
assert.Truef(
t,
errors.Is(err, ErrUnsupportedFileExt),
"Error must wrap ErrUnsupportedFileExt error",
)
})
t.Run("supported file extensions", func(t *testing.T) {
for _, e := range []string{".json", ".yml", ".yaml", ".toml"} {
s := struct{}{}
fp := FileProvider{
FilePath: "testdata/config" + e,
FileExt: e,
Required: true,
}
err := fp.UnmarshalStruct(&s)
require.NoError(t, err)
}
})
}
func TestFileProvider_Fill(t *testing.T) {
t.Run("should be set", func(t *testing.T) {
for _, e := range []string{".json", ".yml", ".yaml", ".toml"} {
s := struct {
Config struct {
Host string
}
}{}
in, err := NewInput(&s)
require.NoError(t, err)
require.NotNil(t, in)
fp := FileProvider{
FilePath: "testdata/config" + e,
FileExt: e,
Required: true,
}
err = fp.Fill(in)
require.NoError(t, err)
for _, f := range in.Fields {
assert.True(t, f.IsSet)
}
}
})
t.Run("config key", func(t *testing.T) {
for _, e := range []string{".json", ".yml", ".yaml", ".toml"} {
s := struct {
Custom string `json:"custom_key" yaml:"custom_key" toml:"custom_key"`
}{}
in, err := NewInput(&s)
require.NoError(t, err)
require.NotNil(t, in)
fp := FileProvider{
FilePath: "testdata/config" + e,
FileExt: e,
Required: true,
}
err = fp.Fill(in)
require.NoError(t, err)
for _, f := range in.Fields {
assert.True(t, f.IsSet)
}
}
})
}