Skip to content

Commit

Permalink
don't throw error when config not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
iamolegga committed Jun 19, 2020
1 parent faf21a8 commit 82e8644
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion enviper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func New(v *viper.Viper) *Enviper {
// The difference between enviper and viper is in automatic overriding data from file by data from env variables
func (e *Enviper) Unmarshal(rawVal interface{}) error {
if err := e.Viper.ReadInConfig(); err != nil {
return err
switch err.(type) {
case viper.ConfigFileNotFoundError:
// do nothing
default:
return err
}
}
e.readEnvs(rawVal)
return e.Viper.Unmarshal(rawVal)
Expand Down
18 changes: 17 additions & 1 deletion enviper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,25 @@ func (s *UnmarshalSuite) SetupTest() {
func (s *UnmarshalSuite) TearDownTest() {}
func (s *UnmarshalSuite) TearDownSuite() {}

func (s *UnmarshalSuite) TestThrowsError() {
func (s *UnmarshalSuite) TestNotThrowsErrorWhenNoConfig() {
var c Config
e := enviper.New(viper.New())
s.Nil(e.Unmarshal(&c))
s.NotNil(c)
}

func (s *UnmarshalSuite) TestThrowsErrorWhenBrokenConfig() {
var c Config
v := viper.New()

cwd, _ := os.Getwd()
p := path.Join(cwd, "test_config.yaml")
ioutil.WriteFile(p, []byte("qwerty"), 0777)
defer os.Remove(p)

e := enviper.New(v)
e.AddConfigPath(cwd)
e.SetConfigName("test_config")
s.NotNil(e.Unmarshal(&c))
}

Expand Down

0 comments on commit 82e8644

Please sign in to comment.