Skip to content

Commit

Permalink
Merge pull request #102 from newrelic/tests/fix-config-tests
Browse files Browse the repository at this point in the history
tests(config): fix tests that were overriding actual config files
  • Loading branch information
ctrombley authored Mar 4, 2020
2 parents 652ebaa + 46e7c67 commit 7c86533
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func init() {
// LoadConfig loads the configuration from disk, substituting defaults
// if the file does not exist.
func LoadConfig(configDir string) (*Config, error) {
log.Debug("loading config file")
log.Debugf("loading config file from %s", configDir)

if configDir == "" {
configDir = DefaultConfigDirectory
Expand Down Expand Up @@ -210,7 +210,7 @@ func (c *Config) createFile(path string, cfgViper *viper.Viper) error {
return err
}

err = os.MkdirAll(DefaultConfigDirectory, os.ModePerm)
err = os.MkdirAll(c.configDir, os.ModePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -272,7 +272,7 @@ func (c *Config) set(key string, value interface{}) error {
return err
}

path := fmt.Sprintf("%s/%s.%s", DefaultConfigDirectory, DefaultConfigName, DefaultConfigType)
path := fmt.Sprintf("%s/%s.%s", c.configDir, DefaultConfigName, DefaultConfigType)
if _, err := os.Stat(path); os.IsNotExist(err) {
createErr := c.createFile(path, cfgViper)
if createErr != nil {
Expand All @@ -285,7 +285,9 @@ func (c *Config) set(key string, value interface{}) error {
}
}

*c = config
if err := mergo.Merge(c, config, mergo.WithOverride); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -402,10 +404,8 @@ func readConfig(configDir string) (*viper.Viper, error) {
cfgViper.SetEnvPrefix(DefaultEnvPrefix)
cfgViper.SetConfigName(DefaultConfigName)
cfgViper.SetConfigType(DefaultConfigType)
cfgViper.AddConfigPath(configDir) // adding provided directory as search path
cfgViper.AddConfigPath(DefaultConfigDirectory) // adding home directory as search path
cfgViper.AddConfigPath(".") // current directory to search path
cfgViper.AutomaticEnv() // read in environment variables that match
cfgViper.AddConfigPath(configDir) // adding provided directory as search path
cfgViper.AutomaticEnv() // read in environment variables that match

err := cfgViper.ReadInConfig()
// nolint
Expand Down

0 comments on commit 7c86533

Please sign in to comment.