mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
5661ab5991
Previously we just ported over the simple "string", "list", and "map" type hint keywords from the old loader, which exist primarily as hints to the CLI for whether to treat -var=... arguments and environment variables as literal strings or as HCL expressions. However, we've been requested before to allow more specific constraints here because it's generally better UX for a type error to be detected within an expression in a calling "module" block rather than at some point deep inside a third-party module. To allow for more specific constraints, here we use the type constraint expression syntax defined as an extension within HCL, which uses the variable and function call syntaxes to represent types rather than values, like this: - string - number - bool - list(string) - list(any) - list(map(string)) - object({id=string,name=string}) In native HCL syntax this looks like: variable "foo" { type = map(string) } In JSON, this looks like: { "variable": { "foo": { "type": "map(string)" } } } The selection of literal processing or HCL parsing of CLI-set values is now explicit in the model and separate from the type, though it's still derived from the type constraint and thus not directly controllable in configuration. Since this syntax is more complex than the keywords that replaced it, for now the simpler keywords are still supported and "list" and "map" are interpreted as list(any) and map(any) respectively, mimicking how they were interpreted by Terraform 0.11 and earlier. For the time being our documentation should continue to recommend these shorthand versions until we gain more experience with the more-specific type constraints; most users should just make use of the additional primitive type constraints this enables: bool and number. As a result of these more-complete type constraints, we can now type-check the default value at config load time, which has the nice side-effect of allowing us to produce a tailored error message if an override file produces an invalid situation; previously the result was rather confusing because the error message referred to the original definition of the variable and not the overridden parts.
139 lines
3.1 KiB
Go
139 lines
3.1 KiB
Go
package configs
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/hcl2/gohcl"
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestModuleOverrideVariable(t *testing.T) {
|
|
mod, diags := testModuleFromDir("test-fixtures/valid-modules/override-variable")
|
|
assertNoDiagnostics(t, diags)
|
|
if mod == nil {
|
|
t.Fatalf("module is nil")
|
|
}
|
|
|
|
got := mod.Variables
|
|
want := map[string]*Variable{
|
|
"fully_overridden": {
|
|
Name: "fully_overridden",
|
|
Description: "b_override description",
|
|
DescriptionSet: true,
|
|
Default: cty.StringVal("b_override"),
|
|
Type: cty.String,
|
|
ParsingMode: VariableParseLiteral,
|
|
DeclRange: hcl.Range{
|
|
Filename: "test-fixtures/valid-modules/override-variable/primary.tf",
|
|
Start: hcl.Pos{
|
|
Line: 1,
|
|
Column: 1,
|
|
Byte: 0,
|
|
},
|
|
End: hcl.Pos{
|
|
Line: 1,
|
|
Column: 28,
|
|
Byte: 27,
|
|
},
|
|
},
|
|
},
|
|
"partially_overridden": {
|
|
Name: "partially_overridden",
|
|
Description: "base description",
|
|
DescriptionSet: true,
|
|
Default: cty.StringVal("b_override partial"),
|
|
Type: cty.String,
|
|
ParsingMode: VariableParseLiteral,
|
|
DeclRange: hcl.Range{
|
|
Filename: "test-fixtures/valid-modules/override-variable/primary.tf",
|
|
Start: hcl.Pos{
|
|
Line: 7,
|
|
Column: 1,
|
|
Byte: 103,
|
|
},
|
|
End: hcl.Pos{
|
|
Line: 7,
|
|
Column: 32,
|
|
Byte: 134,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
assertResultDeepEqual(t, got, want)
|
|
}
|
|
|
|
func TestModuleOverrideModule(t *testing.T) {
|
|
mod, diags := testModuleFromDir("test-fixtures/valid-modules/override-module")
|
|
assertNoDiagnostics(t, diags)
|
|
if mod == nil {
|
|
t.Fatalf("module is nil")
|
|
}
|
|
|
|
if _, exists := mod.ModuleCalls["example"]; !exists {
|
|
t.Fatalf("no module 'example'")
|
|
}
|
|
if len(mod.ModuleCalls) != 1 {
|
|
t.Fatalf("wrong number of module calls in result %d; want 1", len(mod.ModuleCalls))
|
|
}
|
|
|
|
got := mod.ModuleCalls["example"]
|
|
want := &ModuleCall{
|
|
Name: "example",
|
|
SourceAddr: "./example2-a_override",
|
|
SourceAddrRange: hcl.Range{
|
|
Filename: "test-fixtures/valid-modules/override-module/a_override.tf",
|
|
Start: hcl.Pos{
|
|
Line: 3,
|
|
Column: 12,
|
|
Byte: 31,
|
|
},
|
|
End: hcl.Pos{
|
|
Line: 3,
|
|
Column: 35,
|
|
Byte: 54,
|
|
},
|
|
},
|
|
SourceSet: true,
|
|
DeclRange: hcl.Range{
|
|
Filename: "test-fixtures/valid-modules/override-module/primary.tf",
|
|
Start: hcl.Pos{
|
|
Line: 2,
|
|
Column: 1,
|
|
Byte: 1,
|
|
},
|
|
End: hcl.Pos{
|
|
Line: 2,
|
|
Column: 17,
|
|
Byte: 17,
|
|
},
|
|
},
|
|
}
|
|
|
|
// We're going to extract and nil out our hcl.Body here because DeepEqual
|
|
// is not a useful way to assert on that.
|
|
gotConfig := got.Config
|
|
got.Config = nil
|
|
|
|
assertResultDeepEqual(t, got, want)
|
|
|
|
type content struct {
|
|
Kept *string `hcl:"kept"`
|
|
Foo *string `hcl:"foo"`
|
|
New *string `hcl:"new"`
|
|
Newer *string `hcl:"newer"`
|
|
}
|
|
var gotArgs content
|
|
diags = gohcl.DecodeBody(gotConfig, nil, &gotArgs)
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
wantArgs := content{
|
|
Kept: stringPtr("primary kept"),
|
|
Foo: stringPtr("a_override foo"),
|
|
New: stringPtr("b_override new"),
|
|
Newer: stringPtr("b_override newer"),
|
|
}
|
|
|
|
assertResultDeepEqual(t, gotArgs, wantArgs)
|
|
}
|