From 717d682741fa3aeb6d3ce1a670e60b3908e71444 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Fri, 10 Jan 2020 15:35:19 +0000 Subject: [PATCH] PR comments --- viper.go | 14 ++++++------- viper_test.go | 54 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/viper.go b/viper.go index 2c7f42f..e10f239 100644 --- a/viper.go +++ b/viper.go @@ -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) { diff --git a/viper_test.go b/viper_test.go index 4d35923..6c9a84a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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) }