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.") jww.INFO.Println("Attempting to write configuration to file.")
var configType string var configType string
if v.configType != "" { ext := filepath.Ext(filename)
configType = v.configType if ext != "" {
} else {
ext := filepath.Ext(filename)
if len(ext) <= 1 {
return fmt.Errorf("filename: %s requires valid extension", filename)
}
configType = ext[1:] 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) { if !stringInSlice(configType, SupportedExts) {

View file

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