write test

This commit is contained in:
satotake 2017-04-04 01:41:05 +09:00
parent 1b560de7b7
commit 33923af746
2 changed files with 123 additions and 1 deletions

View file

@ -1146,7 +1146,12 @@ func ChainMergeConfigFiles() error { return v.ChainMergeConfigFiles() }
func (v *Viper) ChainMergeConfigFiles() error { func (v *Viper) ChainMergeConfigFiles() error {
jww.INFO.Println("Attempting to chain merge with configFiles") jww.INFO.Println("Attempting to chain merge with configFiles")
for i, filename := range v.configFiles { configFiles, err := v.getConfigFiles()
if err != nil {
return err
}
for i, filename := range configFiles {
if i == 0 { if i == 0 {
v.SetConfigFile(filename) v.SetConfigFile(filename)
err := v.ReadInConfig() err := v.ReadInConfig()
@ -1539,6 +1544,22 @@ func (v *Viper) getConfigFile() (string, error) {
return v.getConfigFile() return v.getConfigFile()
} }
func (v *Viper) getConfigFiles() ([]string, error) {
// if explicitly set, then use it
// else search config file and return as slice
if len(v.configFiles) > 0 {
return v.configFiles, nil
}
cf, err := v.findConfigFile()
if err != nil {
return nil, err
}
v.configFiles = []string{cf}
return v.getConfigFiles()
}
func (v *Viper) searchInPath(in string) (filename string) { func (v *Viper) searchInPath(in string) (filename string) {
jww.DEBUG.Println("Searching for config in ", in) jww.DEBUG.Println("Searching for config in ", in)
for _, ext := range SupportedExts { for _, ext := range SupportedExts {

View file

@ -1152,3 +1152,104 @@ func BenchmarkGetBoolFromMap(b *testing.B) {
} }
} }
} }
func initConfigsForMerge(t *testing.T) (string, string, func()) {
var (
testDirs = []string{`a`, `b`, `c`}
configName = `config.toml`
)
root, err := ioutil.TempDir("", "")
cleanup := true
defer func() {
if cleanup {
os.Chdir("..")
os.RemoveAll(root)
}
}()
assert.Nil(t, err)
err = os.Chdir(root)
assert.Nil(t, err)
for _, dir := range testDirs {
err = os.Mkdir(dir, 0750)
assert.Nil(t, err)
err = ioutil.WriteFile(
path.Join(dir, configName),
[]byte("key = \"value is "+dir+"\"\n"+dir+" = \""+dir+"\"\n"),
0640)
assert.Nil(t, err)
}
cleanup = false
return root, configName, func() {
os.Chdir("..")
os.RemoveAll(root)
}
}
func TestChainMergeConfig(t *testing.T) {
root, config, cleanup := initConfigsForMerge(t)
defer cleanup()
v := New()
v.SetDefault(`key`, `default`)
entries, err := ioutil.ReadDir(root)
var configfiles []string
for i, e := range entries {
if e.IsDir() {
configfile := path.Join(root, e.Name(), config)
configfiles = append(configfiles, configfile)
v.SetConfigFiles(configfiles[:i+1])
err = v.ChainMergeConfigFiles()
assert.Nil(t, err)
assert.Equal(t, `value is `+e.Name(), v.GetString(`key`))
assert.Equal(t, `a`, v.GetString(`a`))
if i == 0 {
assert.Equal(t, ``, v.GetString(`b`))
} else {
assert.Equal(t, `b`, v.GetString(`b`))
}
if i < 2 {
assert.Equal(t, ``, v.GetString(`c`))
} else {
assert.Equal(t, `c`, v.GetString(`c`))
}
}
}
}
func TestNotSetConfigFilesChainMerge(t *testing.T) {
// If not set, behavior is identical to ReadInConfig
root, config, cleanup := initDirs(t)
defer cleanup()
v := New()
v.SetConfigName(config)
v.SetDefault(`key`, `default`)
entries, err := ioutil.ReadDir(root)
for _, e := range entries {
if e.IsDir() {
v.AddConfigPath(e.Name())
}
}
assert.Nil(t, err)
err = v.ChainMergeConfigFiles()
assert.Nil(t, err)
assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`))
}