fix version command so that it will work with all supported config formats and added tests

This commit is contained in:
Joel Scoble 2014-11-21 23:17:17 -06:00 committed by spf13
parent 6c25cd529f
commit 0eeaa4c84e
2 changed files with 84 additions and 1 deletions

View file

@ -87,7 +87,19 @@ func getDateFormat() string {
if params == nil {
return time.RFC3339
}
parms := params.(map[string]interface{})
// var typMapIfaceIface = reflect.TypeOf(map[interface{}{}]interface{}{})
// var typMapStringIface = reflect.TypeOf(map[string]interface{}{})
parms := map[string]interface{}{}
switch params.(type) {
case map[interface{}]interface{}:
for k, v := range params.(map[interface{}]interface{}) {
parms[k.(string)] = v
}
case map[string]interface{}:
parms = params.(map[string]interface{})
}
layout := parms["DateFormat"]
if layout == nil || layout == "" {
return time.RFC3339

71
commands/version_test.go Normal file
View file

@ -0,0 +1,71 @@
package commands
import (
"io/ioutil"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
// config json
var JSONConfig = []byte(`{
"params": {
"DateFormat": "Jan 2 2006"
}
}`)
// config toml
var TOMLConfig = []byte(`
[params]
DateFormat = "Jan 2 2006"
`)
// config yaml
var YAMLConfig = []byte(`
params:
DateFormat: "Jan 2 2006"
`)
var config map[string]interface{} = make(map[string]interface{})
func TestGetDateFormatJSON(t *testing.T) {
jsonFile, _ := ioutil.TempFile("", "config.json")
fname := jsonFile.Name()
jsonFile.Write(JSONConfig)
jsonFile.Close()
viper.SetConfigFile(fname)
viper.SetConfigType("json")
viper.ReadInConfig()
dateFmt := getDateFormat()
assert.Equal(t, "Jan 2 2006", dateFmt)
}
func TestGetDateFormatTOML(t *testing.T) {
viper.Reset()
tomlFile, _ := ioutil.TempFile("", "config.toml")
fname := tomlFile.Name()
tomlFile.Write(TOMLConfig)
tomlFile.Close()
viper.SetConfigFile(fname)
viper.SetConfigType("toml")
viper.ReadInConfig()
dateFmt := getDateFormat()
assert.Equal(t, "Jan 2 2006", dateFmt)
}
func TestGetDateFormatYAML(t *testing.T) {
viper.Reset()
yamlFile, _ := ioutil.TempFile("", "config.yaml")
fname := yamlFile.Name()
yamlFile.Write(YAMLConfig)
yamlFile.Close()
viper.SetConfigFile(fname)
viper.SetConfigType("yaml")
viper.ReadInConfig()
dateFmt := getDateFormat()
assert.Equal(t, "Jan 2 2006", dateFmt)
}