Skip to content

Commit

Permalink
added helper test
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed Sep 26, 2023
1 parent e265a5e commit 7943cac
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
21 changes: 4 additions & 17 deletions consul/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions consul/helper_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ 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 (
cloud.google.com/go/compute v1.18.0 // indirect
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 (
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 7943cac

Please sign in to comment.