Skip to content

Commit

Permalink
add scoped function integration tests
Browse files Browse the repository at this point in the history
Add a locals blocks to the terraformlike tests, with normal and scoped
function calls.
  • Loading branch information
jbardin committed Nov 2, 2023
1 parent 1ce4917 commit 916ac48
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions integrationtest/terraformlike_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/json"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)

// TestTerraformLike parses both a native syntax and a JSON representation
Expand Down Expand Up @@ -53,10 +54,14 @@ func TestTerraformLike(t *testing.T) {
Name string `hcl:"name,label"`
Providers hcl.Expression `hcl:"providers"`
}
type Locals struct {
Config hcl.Body `hcl:",remain"`
}
type Root struct {
Variables []*Variable `hcl:"variable,block"`
Resources []*Resource `hcl:"resource,block"`
Modules []*Module `hcl:"module,block"`
Locals []*Locals `hcl:"locals,block"`
}
instanceDecode := &hcldec.ObjectSpec{
"image_id": &hcldec.AttrSpec{
Expand Down Expand Up @@ -343,6 +348,57 @@ func TestTerraformLike(t *testing.T) {
t.Errorf("wrong value traversal [1] type %T; want hcl.TraverseAttr", vt[1])
}
})

t.Run("locals", func(t *testing.T) {
locals := root.Locals[0]
attrs, diags := locals.Config.JustAttributes()
if diags.HasErrors() {
t.Fatal(diags)
}

ctx := &hcl.EvalContext{
Functions: map[string]function.Function{
"func": function.New(&function.Spec{
Params: []function.Parameter{{Type: cty.String}},
Type: function.StaticReturnType(cty.String),
Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
return cty.StringVal("func_result"), nil
},
}),
"scoped::func": function.New(&function.Spec{
Params: []function.Parameter{{Type: cty.String}},
Type: function.StaticReturnType(cty.String),
Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
return cty.StringVal("scoped::func_result"), nil
},
}),
},
}

res := attrs["func_result"]
funcVal, diags := res.Expr.Value(ctx)
if diags.HasErrors() {
t.Fatal(diags)
}

wantVal := cty.StringVal("func_result")

if !funcVal.RawEquals(wantVal) {
t.Errorf("expected %#v, got %#v", wantVal, funcVal)
}

res = attrs["scoped_func_result"]
funcVal, diags = res.Expr.Value(ctx)
if diags.HasErrors() {
t.Fatal(diags)
}

wantVal = cty.StringVal("scoped::func_result")

if !funcVal.RawEquals(wantVal) {
t.Errorf("expected %#v, got %#v", wantVal, funcVal)
}
})
})
}
}
Expand All @@ -352,6 +408,11 @@ const terraformLikeNativeSyntax = `
variable "image_id" {
}
locals {
func_result = func("arg")
scoped_func_result = scoped::func("arg")
}
resource "happycloud_instance" "test" {
instance_type = "z3.weedy"
image_id = var.image_id
Expand Down Expand Up @@ -400,6 +461,10 @@ const terraformLikeJSON = `
"variable": {
"image_id": {}
},
"locals": {
"func_result": "${func(\"arg\")}",
"scoped_func_result": "${scoped::func(\"arg\")}"
},
"resource": {
"happycloud_instance": {
"test": {
Expand Down

0 comments on commit 916ac48

Please sign in to comment.