-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaconf_test.go
71 lines (55 loc) · 1.32 KB
/
yaconf_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
package yaconf
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
)
type testConfig3 struct {
}
func (c *testConfig3) Validate() error {
return fmt.Errorf("invalid")
}
func TestRead(t *testing.T) {
file, err := os.CreateTemp("", "")
require.Nil(t, err)
defer func() {
os.Remove(file.Name())
}()
_, err = file.Write([]byte(`log_level: info`))
require.Nil(t, err)
err = file.Close()
require.Nil(t, err)
// Test success
config := struct {
LogLevel string `yaml:"log_level"`
}{}
err = Read(file.Name(), &config)
require.Nil(t, err)
// Test with errors
config2 := struct {
LogFile string `yaml:"log_file" yaconf:"required"`
}{}
err = Read(file.Name(), &config2)
require.NotNil(t, err)
// Test invalid unmarshal
config3 := struct {
LogLevel int `yaml:"log_level"`
}{}
err = Read(file.Name(), &config3)
require.NotNil(t, err)
// Test invalid default valud
config4 := struct {
Pid int `yaml:"pid" yaconf:"default=yes"`
}{}
err = Read(file.Name(), &config4)
require.NotNil(t, err)
require.Equal(t, "yes is invalid value for int", err.Error())
// Test with custom validator
err = Read(file.Name(), &testConfig3{})
require.NotNil(t, err)
require.Equal(t, "invalid", err.Error())
// Test invalid file
err = Read("/tmp/yaconf/test.yml", &testConfig3{})
require.NotNil(t, err)
}