mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-29 10:21:01 -06:00
5dd6b839d0
We have a few special use-cases in Terraform where an object is constructed from a mixture of different sources, such as a configuration file, command line arguments, and environment variables. To represent this within the HCL model, we introduce a new "synthetic" HCL body type that just represents a map of values that are interpreted as attributes. We then export the previously-private MergeBodies function to allow the synthetic body to be used as an override for a "real" body, which then allows us to combine these various sources together while still retaining the proper source location information for each individual attribute. Since a synthetic body doesn't actually exist in configuration, it does not produce source locations that can be turned into source snippets but we can still use placeholder strings to help the user to understand which of the many different sources a particular value came from.
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package configs
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|