From 7943cacf38fa4e3d55e6669b63015c5845c8b7cf Mon Sep 17 00:00:00 2001 From: absolutelightning Date: Tue, 26 Sep 2023 18:54:59 +0530 Subject: [PATCH] added helper test --- consul/helper.go | 21 ++++----------------- consul/helper_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++++- go.sum | 2 ++ 4 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 consul/helper_test.go diff --git a/consul/helper.go b/consul/helper.go index 2efb91b0..32628092 100644 --- a/consul/helper.go +++ b/consul/helper.go @@ -4,8 +4,9 @@ package consul import ( - "encoding/json" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "golang.org/x/text/cases" + "golang.org/x/text/language" "reflect" "strings" ) @@ -33,7 +34,8 @@ func formatKey(key string) string { if token == "tls" { keyToReturn += strings.ToUpper(token) } else { - keyToReturn += strings.ToTitle(token) + caser := cases.Title(language.English) + keyToReturn += caser.String(token) } } return keyToReturn @@ -64,21 +66,6 @@ func formatKeys(config interface{}, formatFunc func(string) string) (interface{} newSlice = append(newSlice, newElem) } return newSlice, nil - } else if isStruct(config) { - var modifiedStruct map[string]interface{} - jsonValue, err := json.Marshal(config) - if err != nil { - return nil, err - } - err = json.Unmarshal(jsonValue, &modifiedStruct) - if err != nil { - return nil, err - } - formattedStructKeys, err := formatKeys(modifiedStruct, formatKey) - if err != nil { - return nil, err - } - return formattedStructKeys, nil } else if isSetSchema(config) { valueList := config.(*schema.Set).List() if len(valueList) > 0 { diff --git a/consul/helper_test.go b/consul/helper_test.go new file mode 100644 index 00000000..92705df8 --- /dev/null +++ b/consul/helper_test.go @@ -0,0 +1,41 @@ +package consul + +import ( + "bytes" + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/stretchr/testify/require" + "testing" +) + +func TestFormatKeysFunc(t *testing.T) { + dummySetSchema := new(schema.Set) + dummySetSchema.F = func(i interface{}) int { + var buf bytes.Buffer + buf.WriteString(fmt.Sprintf("%s-", i)) + return hashcode.String(buf.String()) + } + dummySetSchema.Add(map[string]interface{}{"test_set_key": "test_value"}) + dataSet := []map[string]interface{}{ + { + "input": map[string]interface{}{ + "test_key": "value1", + "test_tls_key": "value2", + }, + "expected": map[string]interface{}{ + "TestKey": "value1", + "TestTLSKey": "value2", + }, + }, + { + "input": dummySetSchema, + "expected": map[string]interface{}{"TestSetKey": "test_value"}, + }, + } + for _, testCase := range dataSet { + res, err := formatKeys(testCase["input"], formatKey) + require.NoError(t, err) + require.Equal(t, testCase["expected"], res) + } +} diff --git a/go.mod b/go.mod index df391ba3..b27e07c9 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ require ( github.com/hashicorp/errwrap v1.1.0 github.com/hashicorp/terraform-plugin-sdk v1.17.2 github.com/mitchellh/mapstructure v1.5.0 + github.com/stretchr/testify v1.8.3 ) require ( @@ -12,7 +13,9 @@ require ( cloud.google.com/go/compute/metadata v0.2.3 // indirect cloud.google.com/go/iam v0.12.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) require ( @@ -91,7 +94,7 @@ require ( golang.org/x/net v0.12.0 // indirect golang.org/x/oauth2 v0.6.0 // indirect golang.org/x/sys v0.10.0 // indirect - golang.org/x/text v0.11.0 // indirect + golang.org/x/text v0.11.0 golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect google.golang.org/api v0.114.0 // indirect google.golang.org/appengine v1.6.7 // indirect diff --git a/go.sum b/go.sum index f704096c..093d0fe6 100644 --- a/go.sum +++ b/go.sum @@ -601,6 +601,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/posener/complete v1.2.1/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E= github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo= @@ -652,6 +653,7 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= +github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=