Skip to content

Commit

Permalink
Review updates to utilize afero for file checks and updated checks on…
Browse files Browse the repository at this point in the history
… unit tests
  • Loading branch information
javaducky authored and sagikazarmark committed Dec 6, 2019
1 parent a708479 commit 3a19b6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 36 deletions.
15 changes: 4 additions & 11 deletions viper.go
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -123,14 +124,6 @@ func (faee ConfigFileAlreadyExistsError) Error() string {
return fmt.Sprintf("Config File %q Already Exists", string(faee))
}

// MissingConfigurationError denotes a required configuration setting has not been provided.
type MissingConfigurationError string

// Error returns the formatted error when a required configuration element has not been provided.
func (mce MissingConfigurationError) Error() string {
return fmt.Sprintf("Missing Configuration for %q", string(mce))
}

// A DecoderConfigOption can be passed to viper.Unmarshal to configure
// mapstructure.DecoderConfig options
type DecoderConfigOption func(*mapstructure.DecoderConfig)
Expand Down Expand Up @@ -1354,7 +1347,7 @@ func (v *Viper) WriteConfig() error {
func SafeWriteConfig() error { return v.SafeWriteConfig() }
func (v *Viper) SafeWriteConfig() error {
if len(v.configPaths) < 1 {
return MissingConfigurationError("configPath")
return errors.New("Missing configuration for 'configPath'")
}
return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType))
}
Expand All @@ -1368,8 +1361,8 @@ func (v *Viper) WriteConfigAs(filename string) error {
// SafeWriteConfigAs writes current configuration to a given filename if it does not exist.
func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) }
func (v *Viper) SafeWriteConfigAs(filename string) error {
handle, err := v.fs.Stat(filename)
if handle != nil && err == nil {
alreadyExists, err := afero.Exists(v.fs, filename)
if alreadyExists && err == nil {
return ConfigFileAlreadyExistsError(filename)
}
return v.writeConfig(filename, false)
Expand Down
32 changes: 7 additions & 25 deletions viper_test.go
Expand Up @@ -1391,17 +1391,10 @@ func TestSafeWriteConfig(t *testing.T) {
v.AddConfigPath("/test")
v.SetConfigName("c")
v.SetConfigType("yaml")
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
if err != nil {
t.Fatal(err)
}
if err = v.SafeWriteConfig(); err != nil {
t.Fatal(err)
}
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)))
require.NoError(t, v.SafeWriteConfig())
read, err := afero.ReadFile(fs, "/test/c.yaml")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Equal(t, yamlWriteExpected, read)
}

Expand All @@ -1411,12 +1404,7 @@ func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) {
v.SetFs(fs)
v.SetConfigName("c")
v.SetConfigType("yaml")
err := v.SafeWriteConfig()
if err == nil {
t.Fatal("Expected exception")
}
_, ok := err.(MissingConfigurationError)
assert.True(t, ok, "Expected MissingConfigurationError")
require.EqualError(t, v.SafeWriteConfig(), "Missing configuration for 'configPath'")
}

func TestSafeWriteConfigWithExistingFile(t *testing.T) {
Expand All @@ -1428,9 +1416,7 @@ func TestSafeWriteConfigWithExistingFile(t *testing.T) {
v.SetConfigName("c")
v.SetConfigType("yaml")
err := v.SafeWriteConfig()
if err == nil {
t.Fatal("Expected exception")
}
require.Error(t, err)
_, ok := err.(ConfigFileAlreadyExistsError)
assert.True(t, ok, "Expected ConfigFileAlreadyExistsError")
}
Expand All @@ -1443,9 +1429,7 @@ func TestSafeWriteAsConfig(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err = v.SafeWriteConfigAs("/test/c.yaml"); err != nil {
t.Fatal(err)
}
require.NoError(t, v.SafeWriteConfigAs("/test/c.yaml"))
if _, err = afero.ReadFile(fs, "/test/c.yaml"); err != nil {
t.Fatal(err)
}
Expand All @@ -1457,9 +1441,7 @@ func TestSafeWriteConfigAsWithExistingFile(t *testing.T) {
fs.Create("/test/c.yaml")
v.SetFs(fs)
err := v.SafeWriteConfigAs("/test/c.yaml")
if err == nil {
t.Fatal("Expected exception")
}
require.Error(t, err)
_, ok := err.(ConfigFileAlreadyExistsError)
assert.True(t, ok, "Expected ConfigFileAlreadyExistsError")
}
Expand Down

0 comments on commit 3a19b6e

Please sign in to comment.