Skip to content

Commit 469e808

Browse files
committed
decoder: Test reference collection for template expressions
1 parent db42eb4 commit 469e808

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

decoder/expr_any_ref_origins_test.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,3 +1054,170 @@ func TestCollectRefOrigins_exprAny_operators_json(t *testing.T) {
10541054
})
10551055
}
10561056
}
1057+
1058+
func TestCollectRefOrigins_exprAny_templates_hcl(t *testing.T) {
1059+
testCases := []struct {
1060+
testName string
1061+
attrSchema map[string]*schema.AttributeSchema
1062+
cfg string
1063+
expectedRefOrigins reference.Origins
1064+
}{
1065+
{
1066+
"template expression",
1067+
map[string]*schema.AttributeSchema{
1068+
"attr": {
1069+
Constraint: schema.AnyExpression{
1070+
OfType: cty.String,
1071+
},
1072+
},
1073+
},
1074+
`attr = "${var.foo}_${var.bar}"
1075+
`,
1076+
reference.Origins{
1077+
reference.LocalOrigin{
1078+
Addr: lang.Address{
1079+
lang.RootStep{Name: "var"},
1080+
lang.AttrStep{Name: "foo"},
1081+
},
1082+
Range: hcl.Range{
1083+
Filename: "test.tf",
1084+
Start: hcl.Pos{Line: 1, Column: 11, Byte: 10},
1085+
End: hcl.Pos{Line: 1, Column: 18, Byte: 17},
1086+
},
1087+
Constraints: reference.OriginConstraints{
1088+
{
1089+
OfType: cty.String,
1090+
},
1091+
},
1092+
},
1093+
reference.LocalOrigin{
1094+
Addr: lang.Address{
1095+
lang.RootStep{Name: "var"},
1096+
lang.AttrStep{Name: "bar"},
1097+
},
1098+
Range: hcl.Range{
1099+
Filename: "test.tf",
1100+
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
1101+
End: hcl.Pos{Line: 1, Column: 29, Byte: 28},
1102+
},
1103+
Constraints: reference.OriginConstraints{
1104+
{
1105+
OfType: cty.String,
1106+
},
1107+
},
1108+
},
1109+
},
1110+
},
1111+
}
1112+
1113+
for i, tc := range testCases {
1114+
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
1115+
bodySchema := &schema.BodySchema{
1116+
Attributes: tc.attrSchema,
1117+
}
1118+
1119+
f, diags := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos)
1120+
if len(diags) > 0 {
1121+
t.Error(diags)
1122+
}
1123+
d := testPathDecoder(t, &PathContext{
1124+
Schema: bodySchema,
1125+
Files: map[string]*hcl.File{
1126+
"test.tf": f,
1127+
},
1128+
})
1129+
1130+
origins, err := d.CollectReferenceOrigins()
1131+
if err != nil {
1132+
t.Fatal(err)
1133+
}
1134+
1135+
if diff := cmp.Diff(tc.expectedRefOrigins, origins, ctydebug.CmpOptions); diff != "" {
1136+
t.Fatalf("unexpected origins: %s", diff)
1137+
}
1138+
})
1139+
}
1140+
}
1141+
1142+
func TestCollectRefOrigins_exprAny_templates_json(t *testing.T) {
1143+
testCases := []struct {
1144+
testName string
1145+
attrSchema map[string]*schema.AttributeSchema
1146+
cfg string
1147+
expectedRefOrigins reference.Origins
1148+
}{
1149+
{
1150+
"template expression",
1151+
map[string]*schema.AttributeSchema{
1152+
"attr": {
1153+
Constraint: schema.AnyExpression{
1154+
OfType: cty.String,
1155+
},
1156+
},
1157+
},
1158+
`{"attr": "${var.foo}_${var.bar}"}`,
1159+
reference.Origins{
1160+
reference.LocalOrigin{
1161+
Addr: lang.Address{
1162+
lang.RootStep{Name: "var"},
1163+
lang.AttrStep{Name: "foo"},
1164+
},
1165+
Range: hcl.Range{
1166+
Filename: "test.tf.json",
1167+
Start: hcl.Pos{Line: 1, Column: 13, Byte: 12},
1168+
End: hcl.Pos{Line: 1, Column: 20, Byte: 19},
1169+
},
1170+
Constraints: reference.OriginConstraints{
1171+
{
1172+
OfType: cty.DynamicPseudoType,
1173+
},
1174+
},
1175+
},
1176+
reference.LocalOrigin{
1177+
Addr: lang.Address{
1178+
lang.RootStep{Name: "var"},
1179+
lang.AttrStep{Name: "bar"},
1180+
},
1181+
Range: hcl.Range{
1182+
Filename: "test.tf.json",
1183+
Start: hcl.Pos{Line: 1, Column: 24, Byte: 23},
1184+
End: hcl.Pos{Line: 1, Column: 31, Byte: 30},
1185+
},
1186+
Constraints: reference.OriginConstraints{
1187+
{
1188+
OfType: cty.DynamicPseudoType,
1189+
},
1190+
},
1191+
},
1192+
},
1193+
},
1194+
}
1195+
1196+
for i, tc := range testCases {
1197+
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
1198+
bodySchema := &schema.BodySchema{
1199+
Attributes: tc.attrSchema,
1200+
}
1201+
1202+
f, diags := json.ParseWithStartPos([]byte(tc.cfg), "test.tf.json", hcl.InitialPos)
1203+
if len(diags) > 0 {
1204+
t.Error(diags)
1205+
}
1206+
d := testPathDecoder(t, &PathContext{
1207+
Schema: bodySchema,
1208+
Files: map[string]*hcl.File{
1209+
"test.tf.json": f,
1210+
},
1211+
})
1212+
1213+
origins, err := d.CollectReferenceOrigins()
1214+
if err != nil {
1215+
t.Fatal(err)
1216+
}
1217+
1218+
if diff := cmp.Diff(tc.expectedRefOrigins, origins, ctydebug.CmpOptions); diff != "" {
1219+
t.Fatalf("unexpected origins: %s", diff)
1220+
}
1221+
})
1222+
}
1223+
}

0 commit comments

Comments
 (0)