mirror of
https://github.com/spf13/viper
synced 2025-05-10 22:27:18 +00:00
Update viper_test.go
This commit is contained in:
parent
b1b6c1d559
commit
cc8e02114f
1 changed files with 174 additions and 241 deletions
415
viper_test.go
415
viper_test.go
|
@ -1324,46 +1324,6 @@ var jsonWriteExpected = []byte(`{
|
||||||
"type": "donut"
|
"type": "donut"
|
||||||
}`)
|
}`)
|
||||||
|
|
||||||
func TestWriteConfigJson(t *testing.T) {
|
|
||||||
v := New()
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
|
||||||
v.SetConfigType("json")
|
|
||||||
err := v.ReadConfig(bytes.NewBuffer(jsonExample))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := v.WriteConfigAs("c.json"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
read, err := afero.ReadFile(fs, "c.json")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, jsonWriteExpected, read)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteConfigJsonWithoutFileExtension(t *testing.T) {
|
|
||||||
v := New()
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
|
||||||
v.SetConfigType("json")
|
|
||||||
err := v.ReadConfig(bytes.NewBuffer(jsonExample))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := v.WriteConfigAs("c"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
read, err := afero.ReadFile(fs, "c")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, jsonWriteExpected, read)
|
|
||||||
}
|
|
||||||
|
|
||||||
var propertiesWriteExpected = []byte(`p_id = 0001
|
var propertiesWriteExpected = []byte(`p_id = 0001
|
||||||
p_type = donut
|
p_type = donut
|
||||||
p_name = Cake
|
p_name = Cake
|
||||||
|
@ -1371,178 +1331,6 @@ p_ppu = 0.55
|
||||||
p_batters.batter.type = Regular
|
p_batters.batter.type = Regular
|
||||||
`)
|
`)
|
||||||
|
|
||||||
func TestWriteConfigProperties(t *testing.T) {
|
|
||||||
v := New()
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
|
||||||
v.SetConfigType("properties")
|
|
||||||
err := v.ReadConfig(bytes.NewBuffer(propertiesExample))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := v.WriteConfigAs("c.properties"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
read, err := afero.ReadFile(fs, "c.properties")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, propertiesWriteExpected, read)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteConfigPropertiesWithoutFileExtension(t *testing.T) {
|
|
||||||
v := New()
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
|
||||||
v.SetConfigType("properties")
|
|
||||||
err := v.ReadConfig(bytes.NewBuffer(propertiesExample))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := v.WriteConfigAs("c.properties"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
read, err := afero.ReadFile(fs, "c.properties")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, propertiesWriteExpected, read)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteConfigTOML(t *testing.T) {
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
v := New()
|
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
|
||||||
v.SetConfigType("toml")
|
|
||||||
err := v.ReadConfig(bytes.NewBuffer(tomlExample))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := v.WriteConfigAs("c.toml"); 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("c")
|
|
||||||
v2.SetConfigType("toml")
|
|
||||||
v2.SetConfigFile("c.toml")
|
|
||||||
err = v2.ReadInConfig()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, v.GetString("title"), v2.GetString("title"))
|
|
||||||
assert.Equal(t, v.GetString("owner.bio"), v2.GetString("owner.bio"))
|
|
||||||
assert.Equal(t, v.GetString("owner.dob"), v2.GetString("owner.dob"))
|
|
||||||
assert.Equal(t, v.GetString("owner.organization"), v2.GetString("owner.organization"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteConfigTOMLWithoutFileExtension(t *testing.T) {
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
v := New()
|
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
|
||||||
v.SetConfigType("toml")
|
|
||||||
err := v.ReadConfig(bytes.NewBuffer(tomlExample))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := v.WriteConfigAs("c"); 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("c")
|
|
||||||
v2.SetConfigType("toml")
|
|
||||||
v2.SetConfigFile("c")
|
|
||||||
err = v2.ReadInConfig()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, v.GetString("title"), v2.GetString("title"))
|
|
||||||
assert.Equal(t, v.GetString("owner.bio"), v2.GetString("owner.bio"))
|
|
||||||
assert.Equal(t, v.GetString("owner.dob"), v2.GetString("owner.dob"))
|
|
||||||
assert.Equal(t, v.GetString("owner.organization"), v2.GetString("owner.organization"))
|
|
||||||
}
|
|
||||||
|
|
||||||
var dotenvWriteExpected = []byte(`
|
|
||||||
TITLE="DotEnv Write Example"
|
|
||||||
NAME=Oreo
|
|
||||||
KIND=Biscuit
|
|
||||||
`)
|
|
||||||
|
|
||||||
func TestWriteConfigDotEnv(t *testing.T) {
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
v := New()
|
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
|
||||||
v.SetConfigType("env")
|
|
||||||
err := v.ReadConfig(bytes.NewBuffer(dotenvWriteExpected))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := v.WriteConfigAs("c.env"); 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("c")
|
|
||||||
v2.SetConfigType("env")
|
|
||||||
v2.SetConfigFile("c.env")
|
|
||||||
err = v2.ReadInConfig()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, v.GetString("title"), v2.GetString("title"))
|
|
||||||
assert.Equal(t, v.GetString("type"), v2.GetString("type"))
|
|
||||||
assert.Equal(t, v.GetString("kind"), v2.GetString("kind"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteConfigDotEnvWithoutFileExtension(t *testing.T) {
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
v := New()
|
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
|
||||||
v.SetConfigType("env")
|
|
||||||
err := v.ReadConfig(bytes.NewBuffer(dotenvWriteExpected))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := v.WriteConfigAs("c"); 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("c")
|
|
||||||
v2.SetConfigType("env")
|
|
||||||
v2.SetConfigFile("c")
|
|
||||||
err = v2.ReadInConfig()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, v.GetString("title"), v2.GetString("title"))
|
|
||||||
assert.Equal(t, v.GetString("type"), v2.GetString("type"))
|
|
||||||
assert.Equal(t, v.GetString("kind"), v2.GetString("kind"))
|
|
||||||
}
|
|
||||||
|
|
||||||
var yamlWriteExpected = []byte(`age: 35
|
var yamlWriteExpected = []byte(`age: 35
|
||||||
beard: true
|
beard: true
|
||||||
clothing:
|
clothing:
|
||||||
|
@ -1559,44 +1347,189 @@ hobbies:
|
||||||
name: steve
|
name: steve
|
||||||
`)
|
`)
|
||||||
|
|
||||||
func TestWriteConfigYAML(t *testing.T) {
|
func TestWriteConfig(t *testing.T) {
|
||||||
v := New()
|
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
v.SetFs(fs)
|
testCases := map[string]struct {
|
||||||
v.SetConfigName("c")
|
configName string
|
||||||
v.SetConfigType("yaml")
|
configType string
|
||||||
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
|
fileName string
|
||||||
if err != nil {
|
input []byte
|
||||||
t.Fatal(err)
|
expectedContent []byte
|
||||||
|
}{
|
||||||
|
"json with file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "json",
|
||||||
|
fileName: "c.json",
|
||||||
|
input: jsonExample,
|
||||||
|
expectedContent: jsonWriteExpected,
|
||||||
|
},
|
||||||
|
"json without file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "json",
|
||||||
|
fileName: "c",
|
||||||
|
input: jsonExample,
|
||||||
|
expectedContent: jsonWriteExpected,
|
||||||
|
},
|
||||||
|
"properties with file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "properties",
|
||||||
|
fileName: "c.properties",
|
||||||
|
input: propertiesExample,
|
||||||
|
expectedContent: propertiesWriteExpected,
|
||||||
|
},
|
||||||
|
"properties without file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "properties",
|
||||||
|
fileName: "c",
|
||||||
|
input: propertiesExample,
|
||||||
|
expectedContent: propertiesWriteExpected,
|
||||||
|
},
|
||||||
|
"yaml with file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "yaml",
|
||||||
|
fileName: "c.yaml",
|
||||||
|
input: yamlExample,
|
||||||
|
expectedContent: yamlWriteExpected,
|
||||||
|
},
|
||||||
|
"yaml without file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "yaml",
|
||||||
|
fileName: "c",
|
||||||
|
input: yamlExample,
|
||||||
|
expectedContent: yamlWriteExpected,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
if err := v.WriteConfigAs("c.yaml"); err != nil {
|
for name, tc := range testCases {
|
||||||
t.Fatal(err)
|
t.Run(name, func(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.SetConfigName(tc.fileName)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
read, err := afero.ReadFile(fs, tc.fileName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, tc.expectedContent, read)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
read, err := afero.ReadFile(fs, "c.yaml")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, yamlWriteExpected, read)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteConfigYAMLWithoutFileExtension(t *testing.T) {
|
func TestWriteConfigTOML(t *testing.T) {
|
||||||
v := New()
|
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
v.SetFs(fs)
|
|
||||||
v.SetConfigName("c")
|
testCases := map[string]struct {
|
||||||
v.SetConfigType("yaml")
|
configName string
|
||||||
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
|
configType string
|
||||||
if err != nil {
|
fileName string
|
||||||
t.Fatal(err)
|
input []byte
|
||||||
|
}{
|
||||||
|
"with file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "toml",
|
||||||
|
fileName: "c.toml",
|
||||||
|
input: tomlExample,
|
||||||
|
},
|
||||||
|
"without file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "toml",
|
||||||
|
fileName: "c",
|
||||||
|
input: tomlExample,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
if err := v.WriteConfigAs("c"); err != nil {
|
for name, tc := range testCases {
|
||||||
t.Fatal(err)
|
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("title"), v2.GetString("title"))
|
||||||
|
assert.Equal(t, v.GetString("owner.bio"), v2.GetString("owner.bio"))
|
||||||
|
assert.Equal(t, v.GetString("owner.dob"), v2.GetString("owner.dob"))
|
||||||
|
assert.Equal(t, v.GetString("owner.organization"), v2.GetString("owner.organization"))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
read, err := afero.ReadFile(fs, "c")
|
}
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
func TestWriteConfigDotEnv(t *testing.T) {
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
testCases := map[string]struct {
|
||||||
|
configName string
|
||||||
|
configType string
|
||||||
|
fileName string
|
||||||
|
input []byte
|
||||||
|
}{
|
||||||
|
"with file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "env",
|
||||||
|
fileName: "c.env",
|
||||||
|
input: dotenvExample,
|
||||||
|
},
|
||||||
|
"without file extension": {
|
||||||
|
configName: "c",
|
||||||
|
configType: "env",
|
||||||
|
fileName: "c",
|
||||||
|
input: dotenvExample,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
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("title_dotenv"), v2.GetString("title_dotenv"))
|
||||||
|
assert.Equal(t, v.GetString("type_dotenv"), v2.GetString("type_dotenv"))
|
||||||
|
assert.Equal(t, v.GetString("kind_dotenv"), v2.GetString("kind_dotenv"))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
assert.Equal(t, yamlWriteExpected, read)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSafeWriteConfig(t *testing.T) {
|
func TestSafeWriteConfig(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue