PR comments

This commit is contained in:
Gustavo Bazan 2020-01-10 15:35:19 +00:00
parent 8f40d15c56
commit 717d682741
No known key found for this signature in database
GPG key ID: D02D8BF462322985
2 changed files with 51 additions and 17 deletions

View file

@ -1420,14 +1420,14 @@ func (v *Viper) writeConfig(filename string, force bool) error {
jww.INFO.Println("Attempting to write configuration to file.")
var configType string
if v.configType != "" {
configType = v.configType
} else {
ext := filepath.Ext(filename)
if len(ext) <= 1 {
return fmt.Errorf("filename: %s requires valid extension", filename)
}
ext := filepath.Ext(filename)
if ext != "" {
configType = ext[1:]
} else {
configType = v.configType
}
if configType == "" {
return fmt.Errorf("config type could not be determined for %s", filename)
}
if !stringInSlice(configType, SupportedExts) {

View file

@ -1311,79 +1311,113 @@ func TestWriteConfig(t *testing.T) {
fs := afero.NewMemMapFs()
testCases := map[string]struct {
configName string
configType string
inConfigType string
outConfigType string
fileName string
input []byte
expectedContent []byte
}{
"hcl with file extension": {
configName: "c",
configType: "hcl",
inConfigType: "hcl",
outConfigType: "hcl",
fileName: "c.hcl",
input: hclExample,
expectedContent: hclWriteExpected,
},
"hcl without file extension": {
configName: "c",
configType: "hcl",
inConfigType: "hcl",
outConfigType: "hcl",
fileName: "c",
input: hclExample,
expectedContent: hclWriteExpected,
},
"hcl with file extension and mismatch type": {
configName: "c",
inConfigType: "hcl",
outConfigType: "json",
fileName: "c.hcl",
input: hclExample,
expectedContent: hclWriteExpected,
},
"json with file extension": {
configName: "c",
configType: "json",
inConfigType: "json",
outConfigType: "json",
fileName: "c.json",
input: jsonExample,
expectedContent: jsonWriteExpected,
},
"json without file extension": {
configName: "c",
configType: "json",
inConfigType: "json",
outConfigType: "json",
fileName: "c",
input: jsonExample,
expectedContent: jsonWriteExpected,
},
"json with file extension and mismatch type": {
configName: "c",
inConfigType: "json",
outConfigType: "hcl",
fileName: "c.json",
input: jsonExample,
expectedContent: jsonWriteExpected,
},
"properties with file extension": {
configName: "c",
configType: "properties",
inConfigType: "properties",
outConfigType: "properties",
fileName: "c.properties",
input: propertiesExample,
expectedContent: propertiesWriteExpected,
},
"properties without file extension": {
configName: "c",
configType: "properties",
inConfigType: "properties",
outConfigType: "properties",
fileName: "c",
input: propertiesExample,
expectedContent: propertiesWriteExpected,
},
"yaml with file extension": {
configName: "c",
configType: "yaml",
inConfigType: "yaml",
outConfigType: "yaml",
fileName: "c.yaml",
input: yamlExample,
expectedContent: yamlWriteExpected,
},
"yaml without file extension": {
configName: "c",
configType: "yaml",
inConfigType: "yaml",
outConfigType: "yaml",
fileName: "c",
input: yamlExample,
expectedContent: yamlWriteExpected,
},
"yaml with file extension and mismatch type": {
configName: "c",
inConfigType: "yaml",
outConfigType: "json",
fileName: "c.yaml",
input: yamlExample,
expectedContent: yamlWriteExpected,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
v := New()
v.SetFs(fs)
v.SetConfigName(tc.fileName)
v.SetConfigType(tc.configType)
v.SetConfigType(tc.inConfigType)
err := v.ReadConfig(bytes.NewBuffer(tc.input))
if err != nil {
t.Fatal(err)
}
v.SetConfigType(tc.outConfigType)
if err := v.WriteConfigAs(tc.fileName); err != nil {
t.Fatal(err)
}