mirror of
https://github.com/spf13/viper
synced 2025-05-11 22:57:21 +00:00
Update viper_test.go
This commit is contained in:
parent
a77ddcb242
commit
5a23af8e86
1 changed files with 56 additions and 0 deletions
|
@ -1553,6 +1553,62 @@ func TestWriteConfigTOML(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteConfigINI(t *testing.T) {
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
testCases := map[string]struct {
|
||||||
|
configName string
|
||||||
|
configType string
|
||||||
|
fileName string
|
||||||
|
input []byte
|
||||||
|
}{
|
||||||
|
"with file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "ini",
|
||||||
|
fileName: "c.ini",
|
||||||
|
input: iniExample,
|
||||||
|
},
|
||||||
|
"without file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "ini",
|
||||||
|
fileName: "c",
|
||||||
|
input: iniExample,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for name, tc := range testCases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.SetConfigName(tc.configName)
|
||||||
|
v.SetConfigType(tc.configType)
|
||||||
|
err := v.ReadConfig(bytes.NewBuffer(tc.input))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := v.WriteConfigAs(tc.fileName); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The TOML String method does not order the contents.
|
||||||
|
// Therefore, we must read the generated file and compare the data.
|
||||||
|
v2 := New()
|
||||||
|
v2.SetFs(fs)
|
||||||
|
v2.SetConfigName(tc.configName)
|
||||||
|
v2.SetConfigType(tc.configType)
|
||||||
|
v2.SetConfigFile(tc.fileName)
|
||||||
|
err = v2.ReadInConfig()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, v.GetString("NAME"), v2.GetString("NAME"))
|
||||||
|
assert.Equal(t, v.GetString("author.NAME"), v2.GetString("author.NAME"))
|
||||||
|
assert.Equal(t, v.GetString("author.E-MAIL"), v2.GetString("author.E-MAIL"))
|
||||||
|
assert.Equal(t, v.GetString("author.GITHUB"), v2.GetString("author.GITHUB"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteConfigDotEnv(t *testing.T) {
|
func TestWriteConfigDotEnv(t *testing.T) {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
|
|
Loading…
Add table
Reference in a new issue