opentofu/internal/configs/synth_body_test.go
Martin Atkins 31349a9c3a Move configs/ to internal/configs/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

66 lines
1.4 KiB
Go

package configs
import (
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
)
func TestSynthBodyContent(t *testing.T) {
tests := map[string]struct {
Values map[string]cty.Value
Schema *hcl.BodySchema
DiagCount int
}{
"empty": {
Values: map[string]cty.Value{},
Schema: &hcl.BodySchema{},
DiagCount: 0,
},
"missing required attribute": {
Values: map[string]cty.Value{},
Schema: &hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{
Name: "nonexist",
Required: true,
},
},
},
DiagCount: 1, // missing required attribute
},
"missing optional attribute": {
Values: map[string]cty.Value{},
Schema: &hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{
Name: "nonexist",
},
},
},
DiagCount: 0,
},
"extraneous attribute": {
Values: map[string]cty.Value{
"foo": cty.StringVal("unwanted"),
},
Schema: &hcl.BodySchema{},
DiagCount: 1, // unsupported attribute
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
body := SynthBody("synth", test.Values)
_, diags := body.Content(test.Schema)
if got, want := len(diags), test.DiagCount; got != want {
t.Errorf("wrong number of diagnostics %d; want %d", got, want)
for _, diag := range diags {
t.Logf("- %s", diag)
}
}
})
}
}