mirror of https://github.com/spf13/viper.git
Fix for #549 Add O_CREATE flag to the non-force branch of writeConfig
Also add test for the method
This commit is contained in:
parent
62edee3196
commit
15be5e8700
2
viper.go
2
viper.go
|
@ -1310,7 +1310,7 @@ func (v *Viper) writeConfig(filename string, force bool) error {
|
|||
flags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
|
||||
} else {
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
flags = os.O_WRONLY
|
||||
flags = os.O_CREATE | os.O_WRONLY
|
||||
} else {
|
||||
return fmt.Errorf("File: %s exists. Use WriteConfig to overwrite.", filename)
|
||||
}
|
||||
|
|
|
@ -987,6 +987,26 @@ p_ppu = 0.55
|
|||
p_batters.batter.type = Regular
|
||||
`)
|
||||
|
||||
func TestSafeWriteConfigProperties(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.SafeWriteConfigAs("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 TestWriteConfigProperties(t *testing.T) {
|
||||
v := New()
|
||||
fs := afero.NewMemMapFs()
|
||||
|
|
Loading…
Reference in New Issue