Skip to content

Commit 916ac48

Browse files
committed
add scoped function integration tests
Add a locals blocks to the terraformlike tests, with normal and scoped function calls.
1 parent 1ce4917 commit 916ac48

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

integrationtest/terraformlike_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hashicorp/hcl/v2/hclsyntax"
1717
"github.com/hashicorp/hcl/v2/json"
1818
"github.com/zclconf/go-cty/cty"
19+
"github.com/zclconf/go-cty/cty/function"
1920
)
2021

2122
// TestTerraformLike parses both a native syntax and a JSON representation
@@ -53,10 +54,14 @@ func TestTerraformLike(t *testing.T) {
5354
Name string `hcl:"name,label"`
5455
Providers hcl.Expression `hcl:"providers"`
5556
}
57+
type Locals struct {
58+
Config hcl.Body `hcl:",remain"`
59+
}
5660
type Root struct {
5761
Variables []*Variable `hcl:"variable,block"`
5862
Resources []*Resource `hcl:"resource,block"`
5963
Modules []*Module `hcl:"module,block"`
64+
Locals []*Locals `hcl:"locals,block"`
6065
}
6166
instanceDecode := &hcldec.ObjectSpec{
6267
"image_id": &hcldec.AttrSpec{
@@ -343,6 +348,57 @@ func TestTerraformLike(t *testing.T) {
343348
t.Errorf("wrong value traversal [1] type %T; want hcl.TraverseAttr", vt[1])
344349
}
345350
})
351+
352+
t.Run("locals", func(t *testing.T) {
353+
locals := root.Locals[0]
354+
attrs, diags := locals.Config.JustAttributes()
355+
if diags.HasErrors() {
356+
t.Fatal(diags)
357+
}
358+
359+
ctx := &hcl.EvalContext{
360+
Functions: map[string]function.Function{
361+
"func": function.New(&function.Spec{
362+
Params: []function.Parameter{{Type: cty.String}},
363+
Type: function.StaticReturnType(cty.String),
364+
Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
365+
return cty.StringVal("func_result"), nil
366+
},
367+
}),
368+
"scoped::func": function.New(&function.Spec{
369+
Params: []function.Parameter{{Type: cty.String}},
370+
Type: function.StaticReturnType(cty.String),
371+
Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
372+
return cty.StringVal("scoped::func_result"), nil
373+
},
374+
}),
375+
},
376+
}
377+
378+
res := attrs["func_result"]
379+
funcVal, diags := res.Expr.Value(ctx)
380+
if diags.HasErrors() {
381+
t.Fatal(diags)
382+
}
383+
384+
wantVal := cty.StringVal("func_result")
385+
386+
if !funcVal.RawEquals(wantVal) {
387+
t.Errorf("expected %#v, got %#v", wantVal, funcVal)
388+
}
389+
390+
res = attrs["scoped_func_result"]
391+
funcVal, diags = res.Expr.Value(ctx)
392+
if diags.HasErrors() {
393+
t.Fatal(diags)
394+
}
395+
396+
wantVal = cty.StringVal("scoped::func_result")
397+
398+
if !funcVal.RawEquals(wantVal) {
399+
t.Errorf("expected %#v, got %#v", wantVal, funcVal)
400+
}
401+
})
346402
})
347403
}
348404
}
@@ -352,6 +408,11 @@ const terraformLikeNativeSyntax = `
352408
variable "image_id" {
353409
}
354410
411+
locals {
412+
func_result = func("arg")
413+
scoped_func_result = scoped::func("arg")
414+
}
415+
355416
resource "happycloud_instance" "test" {
356417
instance_type = "z3.weedy"
357418
image_id = var.image_id
@@ -400,6 +461,10 @@ const terraformLikeJSON = `
400461
"variable": {
401462
"image_id": {}
402463
},
464+
"locals": {
465+
"func_result": "${func(\"arg\")}",
466+
"scoped_func_result": "${scoped::func(\"arg\")}"
467+
},
403468
"resource": {
404469
"happycloud_instance": {
405470
"test": {

0 commit comments

Comments
 (0)