shithub: hugo

Download patch

ref: 0eeaa4c84e08dd27a913c5a64c1d307f9559e985
parent: 6c25cd529f9d65db4a0b70b8c1518bb7eeec19a6
author: Joel Scoble <[email protected]>
date: Fri Nov 21 18:17:17 EST 2014

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

--- a/commands/version.go
+++ b/commands/version.go
@@ -87,7 +87,19 @@
 	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
--- /dev/null
+++ b/commands/version_test.go
@@ -1,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)
+}