Skip to content

Commit bab293a

Browse files
committed
decoder: add test for semtok edge case & make it pass
1 parent 998e3b9 commit bab293a

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

decoder/semantic_tokens.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (d *PathDecoder) tokensForBody(ctx context.Context, body *hclsyntax.Body, b
9494

9595
ec := ExprConstraints(attrSchema.Expr)
9696
countAvailable := icontext.ActiveCountFromContext(ctx)
97-
log.Printf("Found Expression: countAvailable %q ", countAvailable)
97+
log.Printf("Found Expression: countAvailable %t", countAvailable)
9898
tokens = append(tokens, d.tokensForExpression(ctx, attr.Expr, ec)...)
9999
}
100100

@@ -176,9 +176,9 @@ func (d *PathDecoder) tokensForExpression(ctx context.Context, expr hclsyntax.Ex
176176
}
177177
countAvailable := icontext.ActiveCountFromContext(ctx)
178178
// TODO why is countAvailable not true here?
179-
log.Printf("Found Expression: %q / %q - %v+", countAvailable, address.Equals(countIndexAttr), address)
179+
log.Printf("Found Expression: %t / %t - %v+", countAvailable, address.Equals(countIndexAttr), address)
180180
// if address.Equals(countIndexAttr) && countAvailable {
181-
if address.Equals(countIndexAttr) {
181+
if address.Equals(countIndexAttr) && countAvailable {
182182
traversal := eType.AsTraversal()
183183
tokens = append(tokens, lang.SemanticToken{
184184
Type: lang.TokenTraversalStep,

decoder/semantic_tokens_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,3 +1053,135 @@ resource "vault_auth_backend" "blah" {
10531053
t.Fatalf("unexpected tokens: %s", diff)
10541054
}
10551055
}
1056+
1057+
func TestDecoder_SemanticTokensInFile_extensions_countUndeclared(t *testing.T) {
1058+
bodySchema := &schema.BodySchema{
1059+
Blocks: map[string]*schema.BlockSchema{
1060+
"resource": {
1061+
Body: &schema.BodySchema{
1062+
Extensions: &schema.BodyExtensions{
1063+
Count: true,
1064+
},
1065+
Attributes: map[string]*schema.AttributeSchema{
1066+
"cpu_count": {
1067+
Expr: schema.LiteralTypeOnly(cty.Number),
1068+
},
1069+
},
1070+
},
1071+
Labels: []*schema.LabelSchema{
1072+
{
1073+
Name: "type",
1074+
IsDepKey: true,
1075+
SemanticTokenModifiers: lang.SemanticTokenModifiers{
1076+
lang.TokenModifierDependent,
1077+
},
1078+
},
1079+
{Name: "name"},
1080+
},
1081+
},
1082+
},
1083+
}
1084+
1085+
testCfg := []byte(`
1086+
resource "vault_auth_backend" "blah" {
1087+
cpu_count = count.index
1088+
}
1089+
`)
1090+
1091+
f, pDiags := hclsyntax.ParseConfig(testCfg, "test.tf", hcl.InitialPos)
1092+
if len(pDiags) > 0 {
1093+
t.Fatal(pDiags)
1094+
}
1095+
1096+
d := testPathDecoder(t, &PathContext{
1097+
Schema: bodySchema,
1098+
Files: map[string]*hcl.File{
1099+
"test.tf": f,
1100+
},
1101+
})
1102+
1103+
ctx := context.Background()
1104+
1105+
tokens, err := d.SemanticTokensInFile(ctx, "test.tf")
1106+
if err != nil {
1107+
t.Fatal(err)
1108+
}
1109+
1110+
expectedTokens := []lang.SemanticToken{
1111+
{ // resource
1112+
Type: lang.TokenBlockType,
1113+
Modifiers: []lang.SemanticTokenModifier{},
1114+
Range: hcl.Range{
1115+
Filename: "test.tf",
1116+
Start: hcl.Pos{
1117+
Line: 2,
1118+
Column: 1,
1119+
Byte: 1,
1120+
},
1121+
End: hcl.Pos{
1122+
Line: 2,
1123+
Column: 9,
1124+
Byte: 9,
1125+
},
1126+
},
1127+
},
1128+
{ // vault_auth_backend
1129+
Type: lang.TokenBlockLabel,
1130+
Modifiers: []lang.SemanticTokenModifier{
1131+
lang.TokenModifierDependent,
1132+
},
1133+
Range: hcl.Range{
1134+
Filename: "test.tf",
1135+
Start: hcl.Pos{
1136+
Line: 2,
1137+
Column: 10,
1138+
Byte: 10,
1139+
},
1140+
End: hcl.Pos{
1141+
Line: 2,
1142+
Column: 30,
1143+
Byte: 30,
1144+
},
1145+
},
1146+
},
1147+
{ // blah
1148+
Type: lang.TokenBlockLabel,
1149+
Modifiers: []lang.SemanticTokenModifier{},
1150+
Range: hcl.Range{
1151+
Filename: "test.tf",
1152+
Start: hcl.Pos{
1153+
Line: 2,
1154+
Column: 31,
1155+
Byte: 31,
1156+
},
1157+
End: hcl.Pos{
1158+
Line: 2,
1159+
Column: 37,
1160+
Byte: 37,
1161+
},
1162+
},
1163+
},
1164+
{ // cpu_count
1165+
Type: lang.TokenAttrName,
1166+
Modifiers: lang.SemanticTokenModifiers{},
1167+
Range: hcl.Range{
1168+
Filename: "test.tf",
1169+
Start: hcl.Pos{
1170+
Line: 3,
1171+
Column: 3,
1172+
Byte: 42,
1173+
},
1174+
End: hcl.Pos{
1175+
Line: 3,
1176+
Column: 12,
1177+
Byte: 51,
1178+
},
1179+
},
1180+
},
1181+
}
1182+
1183+
diff := cmp.Diff(expectedTokens, tokens)
1184+
if diff != "" {
1185+
t.Fatalf("unexpected tokens: %s", diff)
1186+
}
1187+
}

0 commit comments

Comments
 (0)