Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish reference origin collection for all constraints #241

Merged
merged 13 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions decoder/expr_list_ref_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import (
"context"

"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2"
)

func (list List) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
eType, ok := list.expr.(*hclsyntax.TupleConsExpr)
if !ok {
elems, diags := hcl.ExprList(list.expr)
if diags.HasErrors() {
return reference.Origins{}
}

if len(eType.Exprs) == 0 || list.cons.Elem == nil {
if len(elems) == 0 || list.cons.Elem == nil {
return reference.Origins{}
}

origins := make(reference.Origins, 0)

for _, elemExpr := range eType.Exprs {
for _, elemExpr := range elems {
expr := newExpression(list.pathCtx, elemExpr, list.cons.Elem)
if e, ok := expr.(ReferenceOriginsExpression); ok {
origins = append(origins, e.ReferenceOrigins(ctx, allowSelfRefs)...)
Expand Down
353 changes: 353 additions & 0 deletions decoder/expr_list_ref_origins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,353 @@
package decoder

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/json"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)

func TestCollectRefOrigins_exprList_hcl(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
expectedRefOrigins reference.Origins
}{
{
"expression mismatch",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.String},
},
},
},
`attr = foo.bar
`,
reference.Origins{},
},
{
"no origins",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.String},
},
},
},
`attr = ["noot"]
`,
reference.Origins{},
},
{
"one origin",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.String},
},
},
},
`attr = [foo.bar]
`,
reference.Origins{
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "foo"},
lang.AttrStep{Name: "bar"},
},
Range: hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
Constraints: reference.OriginConstraints{
{OfType: cty.String},
},
},
},
},
{
"multiple origins",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.Number},
},
},
},
`attr = [foo, bar]
`,
reference.Origins{
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "foo"},
},
Range: hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
},
Constraints: reference.OriginConstraints{
{OfType: cty.Number},
},
},
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "bar"},
},
Range: hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 14, Byte: 13},
End: hcl.Pos{Line: 1, Column: 17, Byte: 16},
},
Constraints: reference.OriginConstraints{
{OfType: cty.Number},
},
},
},
},
{
"multiple origins with skipped invalid expression",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.Number},
},
},
},
`attr = [foo, "noot", bar]
`,
reference.Origins{
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "foo"},
},
Range: hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
},
Constraints: reference.OriginConstraints{
{OfType: cty.Number},
},
},
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "bar"},
},
Range: hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
End: hcl.Pos{Line: 1, Column: 25, Byte: 24},
},
Constraints: reference.OriginConstraints{
{OfType: cty.Number},
},
},
},
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
bodySchema := &schema.BodySchema{
Attributes: tc.attrSchema,
}

f, diags := hclsyntax.ParseConfig([]byte(tc.cfg), "test.hcl", hcl.InitialPos)
if len(diags) > 0 {
t.Error(diags)
}
d := testPathDecoder(t, &PathContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.hcl": f,
},
})

origins, err := d.CollectReferenceOrigins()
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedRefOrigins, origins, ctydebug.CmpOptions); diff != "" {
t.Fatalf("unexpected origins: %s", diff)
}
})
}
}

func TestCollectRefOrigins_exprList_json(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
expectedRefOrigins reference.Origins
}{
{
"expression mismatch",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.String},
},
},
},
`{"attr": "foo.bar"}`,
reference.Origins{},
},
{
"no origins",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.String},
},
},
},
`{"attr":[42]}`,
reference.Origins{},
},
{
"one origin",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.String},
},
},
},
`{"attr":["foo.bar"]}`,
reference.Origins{
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "foo"},
lang.AttrStep{Name: "bar"},
},
Range: hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 11, Byte: 10},
End: hcl.Pos{Line: 1, Column: 18, Byte: 17},
},
Constraints: reference.OriginConstraints{
{OfType: cty.String},
},
},
},
},
{
"multiple origins",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.Number},
},
},
},
`{"attr":["${foo}", "${bar}"]}`,
reference.Origins{
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "foo"},
},
Range: hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 13, Byte: 12},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
Constraints: reference.OriginConstraints{
{OfType: cty.Number},
},
},
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "bar"},
},
Range: hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 23, Byte: 22},
End: hcl.Pos{Line: 1, Column: 26, Byte: 25},
},
Constraints: reference.OriginConstraints{
{OfType: cty.Number},
},
},
},
},
{
"multiple origins with skipped invalid expression",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Reference{OfType: cty.Number},
},
},
},
`{"attr":["${foo}", 42, "${bar}"]}`,
reference.Origins{
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "foo"},
},
Range: hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 13, Byte: 12},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
Constraints: reference.OriginConstraints{
{OfType: cty.Number},
},
},
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "bar"},
},
Range: hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 27, Byte: 26},
End: hcl.Pos{Line: 1, Column: 30, Byte: 29},
},
Constraints: reference.OriginConstraints{
{OfType: cty.Number},
},
},
},
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
bodySchema := &schema.BodySchema{
Attributes: tc.attrSchema,
}

f, diags := json.ParseWithStartPos([]byte(tc.cfg), "test.hcl.json", hcl.InitialPos)
if len(diags) > 0 {
t.Error(diags)
}
d := testPathDecoder(t, &PathContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.hcl.json": f,
},
})

origins, err := d.CollectReferenceOrigins()
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedRefOrigins, origins, ctydebug.CmpOptions); diff != "" {
t.Fatalf("unexpected origins: %s", diff)
}
})
}
}
Loading