Review updates to utilize afero for file checks and updated checks on unit tests

This commit is contained in:
javaducky 2019-12-03 19:14:08 -06:00 committed by Márk Sági-Kazár
parent a708479794
commit 3a19b6e0d9
2 changed files with 11 additions and 36 deletions

View file

@ -23,6 +23,7 @@ import (
"bytes" "bytes"
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -123,14 +124,6 @@ func (faee ConfigFileAlreadyExistsError) Error() string {
return fmt.Sprintf("Config File %q Already Exists", string(faee)) 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 // A DecoderConfigOption can be passed to viper.Unmarshal to configure
// mapstructure.DecoderConfig options // mapstructure.DecoderConfig options
type DecoderConfigOption func(*mapstructure.DecoderConfig) type DecoderConfigOption func(*mapstructure.DecoderConfig)
@ -1354,7 +1347,7 @@ func (v *Viper) WriteConfig() error {
func SafeWriteConfig() error { return v.SafeWriteConfig() } func SafeWriteConfig() error { return v.SafeWriteConfig() }
func (v *Viper) SafeWriteConfig() error { func (v *Viper) SafeWriteConfig() error {
if len(v.configPaths) < 1 { 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)) return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType))
} }
@ -1368,8 +1361,8 @@ func (v *Viper) WriteConfigAs(filename string) error {
// SafeWriteConfigAs writes current configuration to a given filename if it does not exist. // SafeWriteConfigAs writes current configuration to a given filename if it does not exist.
func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) } func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) }
func (v *Viper) SafeWriteConfigAs(filename string) error { func (v *Viper) SafeWriteConfigAs(filename string) error {
handle, err := v.fs.Stat(filename) alreadyExists, err := afero.Exists(v.fs, filename)
if handle != nil && err == nil { if alreadyExists && err == nil {
return ConfigFileAlreadyExistsError(filename) return ConfigFileAlreadyExistsError(filename)
} }
return v.writeConfig(filename, false) return v.writeConfig(filename, false)

View file

@ -1391,17 +1391,10 @@ func TestSafeWriteConfig(t *testing.T) {
v.AddConfigPath("/test") v.AddConfigPath("/test")
v.SetConfigName("c") v.SetConfigName("c")
v.SetConfigType("yaml") v.SetConfigType("yaml")
err := v.ReadConfig(bytes.NewBuffer(yamlExample)) require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)))
if err != nil { require.NoError(t, v.SafeWriteConfig())
t.Fatal(err)
}
if err = v.SafeWriteConfig(); err != nil {
t.Fatal(err)
}
read, err := afero.ReadFile(fs, "/test/c.yaml") read, err := afero.ReadFile(fs, "/test/c.yaml")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
assert.Equal(t, yamlWriteExpected, read) assert.Equal(t, yamlWriteExpected, read)
} }
@ -1411,12 +1404,7 @@ func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) {
v.SetFs(fs) v.SetFs(fs)
v.SetConfigName("c") v.SetConfigName("c")
v.SetConfigType("yaml") v.SetConfigType("yaml")
err := v.SafeWriteConfig() require.EqualError(t, v.SafeWriteConfig(), "Missing configuration for 'configPath'")
if err == nil {
t.Fatal("Expected exception")
}
_, ok := err.(MissingConfigurationError)
assert.True(t, ok, "Expected MissingConfigurationError")
} }
func TestSafeWriteConfigWithExistingFile(t *testing.T) { func TestSafeWriteConfigWithExistingFile(t *testing.T) {
@ -1428,9 +1416,7 @@ func TestSafeWriteConfigWithExistingFile(t *testing.T) {
v.SetConfigName("c") v.SetConfigName("c")
v.SetConfigType("yaml") v.SetConfigType("yaml")
err := v.SafeWriteConfig() err := v.SafeWriteConfig()
if err == nil { require.Error(t, err)
t.Fatal("Expected exception")
}
_, ok := err.(ConfigFileAlreadyExistsError) _, ok := err.(ConfigFileAlreadyExistsError)
assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") assert.True(t, ok, "Expected ConfigFileAlreadyExistsError")
} }
@ -1443,9 +1429,7 @@ func TestSafeWriteAsConfig(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err = v.SafeWriteConfigAs("/test/c.yaml"); err != nil { require.NoError(t, v.SafeWriteConfigAs("/test/c.yaml"))
t.Fatal(err)
}
if _, err = afero.ReadFile(fs, "/test/c.yaml"); err != nil { if _, err = afero.ReadFile(fs, "/test/c.yaml"); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -1457,9 +1441,7 @@ func TestSafeWriteConfigAsWithExistingFile(t *testing.T) {
fs.Create("/test/c.yaml") fs.Create("/test/c.yaml")
v.SetFs(fs) v.SetFs(fs)
err := v.SafeWriteConfigAs("/test/c.yaml") err := v.SafeWriteConfigAs("/test/c.yaml")
if err == nil { require.Error(t, err)
t.Fatal("Expected exception")
}
_, ok := err.(ConfigFileAlreadyExistsError) _, ok := err.(ConfigFileAlreadyExistsError)
assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") assert.True(t, ok, "Expected ConfigFileAlreadyExistsError")
} }