forked from gookit/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissues_test.go
138 lines (115 loc) · 2.56 KB
/
issues_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
package config_test
import (
"testing"
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/ini"
"github.com/gookit/config/v2/yaml"
"github.com/gookit/config/v2/yamlv3"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil"
"github.com/stretchr/testify/assert"
)
// https://github.com/gookit/config/issues/37
func TestIssues_37(t *testing.T) {
is := assert.New(t)
c := config.New("test")
c.AddDriver(yaml.Driver)
err := c.LoadStrings(config.JSON, `
{
"lang": {
"allowed": {
"en": "ddd"
}
}
}
`)
is.NoError(err)
dump.Println(c.Data())
is.Panics(func() {
_ = c.LoadStrings(config.Yaml, `
lang:
allowed:
en: "666"
`)
})
}
// https://github.com/gookit/config/issues/37
func TestIssues37_yaml_v3(t *testing.T) {
is := assert.New(t)
c := config.New("test")
c.AddDriver(yamlv3.Driver)
err := c.LoadStrings(config.JSON, `
{
"lang": {
"allowed": {
"en": "ddd"
}
}
}
`)
is.NoError(err)
dump.Println(c.Data())
err = c.LoadStrings(config.Yaml, `
lang:
newKey: hhh
allowed:
en: "666"
`)
is.NoError(err)
dump.Println(c.Data())
}
// BindStruct doesn't seem to work with env var substitution
// https://github.com/gookit/config/issues/46
func TestIssues_46(t *testing.T) {
is := assert.New(t)
c := config.New("test").WithOptions(config.ParseEnv)
err := c.LoadStrings(config.JSON, `
{
"http": {
"port": "${HTTP_PORT|8080}"
}
}
`)
is.NoError(err)
dump.Println(c.Data())
val, _ := c.GetValue("http")
mp := val.(map[string]interface{})
dump.Println(mp)
is.Equal("${HTTP_PORT|8080}", mp["port"])
smp := c.StringMap("http")
dump.Println(smp)
is.Contains(smp, "port")
is.Equal("8080", smp["port"])
type Http struct {
Port int
}
h := &Http{}
err = c.BindStruct("http", h)
is.NoError(err)
dump.Println(h)
is.Equal(8080, h.Port)
testutil.MockEnvValue("HTTP_PORT", "19090", func(_ string) {
h := &Http{}
err = c.BindStruct("http", h)
is.NoError(err)
dump.Println(h)
is.Equal(19090, h.Port)
})
}
// https://github.com/gookit/config/issues/46
func TestIssues_59(t *testing.T) {
is := assert.New(t)
c := config.NewWithOptions("test", config.ParseEnv)
c.AddDriver(ini.Driver)
err := c.LoadFiles("testdata/ini_base.ini")
is.NoError(err)
dump.Println(c.Data())
dumpfile := "testdata/issues59.ini"
out := fsutil.MustCreateFile(dumpfile, 0666, 0666)
_, err = c.DumpTo(out, config.Ini)
is.NoError(err)
str := string(fsutil.MustReadFile(dumpfile))
is.Contains(str, "name = app")
is.Contains(str, "key1 = val1")
}