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:
Daniel Einspanjer 2018-07-31 18:32:14 -04:00
parent 62edee3196
commit 15be5e8700
2 changed files with 21 additions and 1 deletions

View file

@ -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)
}

View file

@ -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()