Skip to content

Commit d0b9daa

Browse files
committed
more work, need to figure out how to do hcl equivalency
1 parent 5ce3a3f commit d0b9daa

File tree

4 files changed

+104
-7
lines changed

4 files changed

+104
-7
lines changed

acctest/config.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func DefaultConfigValueFuncs() map[string]ConfigValueFunc {
4747
// handle single and multi-line strings
4848
util.KeyFN(""): func(v interface{}) string {
4949
if strings.Contains(v.(string), "\n") {
50-
return fmt.Sprintf("<<EOD\n%s\nEOD", v.(string))
50+
return fmt.Sprintf("<<EOD\n%s\nEOD\n", v.(string))
5151
} else {
5252
return fmt.Sprintf("%q", v.(string))
5353
}
@@ -89,6 +89,20 @@ func DefaultConfigValueFuncs() map[string]ConfigValueFunc {
8989
}
9090
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
9191
},
92+
util.KeyFN(make([]map[string]interface{}, 0)): func(v interface{}) string {
93+
formatted := make([]string, 0)
94+
for _, v := range v.([]map[string]interface{}) {
95+
formatted = append(formatted, ConfigValue(v))
96+
}
97+
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
98+
},
99+
util.KeyFN(make([]map[string]string, 0)): func(v interface{}) string {
100+
formatted := make([]string, 0)
101+
for _, v := range v.([]map[string]string) {
102+
formatted = append(formatted, ConfigValue(v))
103+
}
104+
return fmt.Sprintf("[\n%s\n]", strings.Join(formatted, ",\n"))
105+
},
92106

93107
// maps
94108

acctest/config_test.go

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ package acctest_test
33
import (
44
"fmt"
55
"math"
6-
"reflect"
76
"testing"
87
"time"
98

109
"github.com/dcarbone/terraform-plugin-framework-utils/v3/acctest"
10+
"github.com/hashicorp/hcl/v2"
11+
"github.com/hashicorp/hcl/v2/hclparse"
1112
)
1213

1314
func TestConfigValue_Defaults(t *testing.T) {
1415
type convTest struct {
15-
name string
16-
in interface{}
17-
out interface{}
16+
name string
17+
looseMatch bool
18+
in interface{}
19+
out interface{}
20+
schema *hcl.BodySchema
1821
}
1922

2023
theTests := []convTest{
@@ -70,7 +73,8 @@ line`,
7073
out: `<<EOD
7174
multi
7275
line
73-
EOD`,
76+
EOD
77+
`,
7478
},
7579
{
7680
name: "duration-to-string",
@@ -109,14 +113,75 @@ EOD`,
109113
` + fmt.Sprintf("%f", float64(5)) + `
110114
]`,
111115
},
116+
{
117+
name: "slice-map-string-interface",
118+
in: []map[string]interface{}{
119+
{"key1": []string{"k1v1", "k1v2"}, "key2": "k2v2"},
120+
{"key3": 5},
121+
},
122+
looseMatch: true,
123+
schema: &hcl.BodySchema{
124+
Attributes: []hcl.AttributeSchema{
125+
{
126+
Name: "testvar",
127+
Required: true,
128+
},
129+
},
130+
},
131+
out: `[
132+
{
133+
key1 = [
134+
"k1v1",
135+
"k1v2"
136+
]
137+
key2 = "k2v2"
138+
},
139+
{
140+
key3 = 5
141+
}
142+
]`,
143+
},
112144
}
113145

114146
// todo: use biggerer brain to figure out how to test map -> string verification
115147

116148
for _, theT := range theTests {
117149
t.Run(theT.name, func(t *testing.T) {
150+
151+
// generate output
118152
out := acctest.ConfigValue(theT.in)
119-
if !reflect.DeepEqual(out, theT.out) {
153+
154+
// ensure output can be parsed by hcl parser
155+
hp := hclparse.NewParser()
156+
157+
// turn output into a block definition for the parser
158+
ho := fmt.Sprintf("testvar = %s", out)
159+
160+
// attempt parse
161+
hf, d := hp.ParseHCL([]byte(ho), fmt.Sprintf("%s-hcl", theT.name))
162+
163+
// check diagnostics for errors
164+
if d.HasErrors() {
165+
t.Logf("Failed to parse generated HCL: %v", d.Error())
166+
t.Log(ho)
167+
t.Fail()
168+
return
169+
}
170+
171+
if theT.schema != nil {
172+
if _, d := hf.Body.Content(theT.schema); d.HasErrors() {
173+
t.Logf("Generated HCL does not conform to schema: %v", d.Error())
174+
t.Logf(ho)
175+
t.Fail()
176+
return
177+
}
178+
}
179+
180+
if theT.looseMatch {
181+
return
182+
}
183+
184+
if out != theT.out {
120185
t.Log("Output does not match expected")
121186
t.Logf("Input: %v", theT.in)
122187
t.Logf("Expected: %v", theT.out)

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ require (
88
)
99

1010
require (
11+
github.com/agext/levenshtein v1.2.1 // indirect
12+
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
1113
github.com/fatih/color v1.13.0 // indirect
1214
github.com/golang/protobuf v1.5.2 // indirect
1315
github.com/hashicorp/go-hclog v1.2.1 // indirect
16+
github.com/hashicorp/hcl/v2 v2.16.0 // indirect
1417
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
1518
github.com/mattn/go-colorable v0.1.12 // indirect
1619
github.com/mattn/go-isatty v0.0.14 // indirect
1720
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
21+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
1822
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
1923
github.com/vmihailenco/tagparser v0.1.1 // indirect
24+
github.com/zclconf/go-cty v1.12.1 // indirect
2025
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
2126
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
27+
golang.org/x/text v0.4.0 // indirect
2228
google.golang.org/appengine v1.6.5 // indirect
2329
google.golang.org/protobuf v1.28.1 // indirect
2430
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
2+
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
3+
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
4+
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
15
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
26
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
37
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -12,6 +16,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
1216
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
1317
github.com/hashicorp/go-hclog v1.2.1 h1:YQsLlGDJgwhXFpucSPyVbCBviQtjlHv3jLTlp8YmtEw=
1418
github.com/hashicorp/go-hclog v1.2.1/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
19+
github.com/hashicorp/hcl/v2 v2.16.0 h1:MPq1q615H+9wBAdE3EbwEd6imSohElrIguuasbQruB0=
20+
github.com/hashicorp/hcl/v2 v2.16.0/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng=
1521
github.com/hashicorp/terraform-plugin-framework v1.1.1 h1:PbnEKHsIU8KTTzoztHQGgjZUWx7Kk8uGtpGMMc1p+oI=
1622
github.com/hashicorp/terraform-plugin-framework v1.1.1/go.mod h1:DyZPxQA+4OKK5ELxFIIcqggcszqdWWUpTLPHAhS/tkY=
1723
github.com/hashicorp/terraform-plugin-go v0.14.3 h1:nlnJ1GXKdMwsC8g1Nh05tK2wsC3+3BL/DBBxFEki+j0=
@@ -31,6 +37,8 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
3137
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
3238
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
3339
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
40+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
41+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
3442
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3543
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3644
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -40,6 +48,8 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
4048
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
4149
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
4250
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
51+
github.com/zclconf/go-cty v1.12.1 h1:PcupnljUm9EIvbgSHQnHhUr3fO6oFmkOrvs2BAFNXXY=
52+
github.com/zclconf/go-cty v1.12.1/go.mod h1:s9IfD1LK5ccNMSWCVFCE2rJfHiZgi7JijgeWIMfhLvA=
4353
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4454
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
4555
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -55,6 +65,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9w
5565
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5666
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5767
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
68+
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
69+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
5870
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5971
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6072
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=

0 commit comments

Comments
 (0)