Skip to content

Commit b4e27ae

Browse files
author
Abhijeet
authored
test_suite: refactor schema validation of diagnostic file range, pos (#750)
1 parent 314d236 commit b4e27ae

1 file changed

Lines changed: 80 additions & 24 deletions

File tree

cmd/hclspecsuite/test_file.go

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88

99
"github.com/zclconf/go-cty/cty"
1010
"github.com/zclconf/go-cty/cty/convert"
11+
"github.com/zclconf/go-cty/cty/gocty"
1112

1213
"github.com/hashicorp/hcl/v2"
1314
"github.com/hashicorp/hcl/v2/ext/typeexpr"
14-
"github.com/hashicorp/hcl/v2/gohcl"
1515
)
1616

1717
type TestFile struct {
@@ -240,36 +240,92 @@ func (r *Runner) decodeDiagnosticsBlock(block *hcl.Block) ([]*TestFileExpectDiag
240240
return ret, diags
241241
}
242242

243-
func (r *Runner) decodeRangeFromBody(body hcl.Body) (hcl.Range, hcl.Body, hcl.Diagnostics) {
244-
type RawPos struct {
245-
Line int `hcl:"line"`
246-
Column int `hcl:"column"`
247-
Byte int `hcl:"byte"`
243+
func (r *Runner) decodePosFromBody(body hcl.Body) (hcl.Pos, hcl.Diagnostics) {
244+
pos := hcl.Pos{}
245+
var diags hcl.Diagnostics
246+
247+
posBody, moreDiags := body.Content(testFilePosSchema)
248+
diags = append(diags, moreDiags...)
249+
250+
if attr, ok := posBody.Attributes["line"]; ok {
251+
val, moreDiags := attr.Expr.Value(nil)
252+
diags = append(diags, moreDiags...)
253+
254+
if !moreDiags.HasErrors() {
255+
if err := gocty.FromCtyValue(val, &pos.Line); err != nil {
256+
diags = diags.Append(&hcl.Diagnostic{
257+
Severity: hcl.DiagError,
258+
Summary: "Invalid line number",
259+
Detail: fmt.Sprintf("The line number must be an integer: %s", err),
260+
Subject: posBody.Attributes["line"].Expr.Range().Ptr(),
261+
})
262+
}
263+
}
248264
}
249-
type RawRange struct {
250-
From RawPos `hcl:"from,block"`
251-
To RawPos `hcl:"to,block"`
252-
Remain hcl.Body `hcl:",remain"`
265+
266+
if attr, ok := posBody.Attributes["column"]; ok {
267+
val, moreDiags := attr.Expr.Value(nil)
268+
diags = append(diags, moreDiags...)
269+
270+
if !moreDiags.HasErrors() {
271+
if err := gocty.FromCtyValue(val, &pos.Column); err != nil {
272+
diags = diags.Append(&hcl.Diagnostic{
273+
Severity: hcl.DiagError,
274+
Summary: "Invalid column number",
275+
Detail: fmt.Sprintf("The column number must be an integer: %s", err),
276+
Subject: posBody.Attributes["column"].Expr.Range().Ptr(),
277+
})
278+
}
279+
}
280+
}
281+
282+
if attr, ok := posBody.Attributes["byte"]; ok {
283+
val, moreDiags := attr.Expr.Value(nil)
284+
diags = append(diags, moreDiags...)
285+
286+
if !moreDiags.HasErrors() {
287+
if err := gocty.FromCtyValue(val, &pos.Byte); err != nil {
288+
diags = diags.Append(&hcl.Diagnostic{
289+
Severity: hcl.DiagError,
290+
Summary: "Invalid byte position",
291+
Detail: fmt.Sprintf("The byte position must be an integer: %s", err),
292+
Subject: posBody.Attributes["byte"].Expr.Range().Ptr(),
293+
})
294+
}
295+
}
253296
}
254297

255-
var raw RawRange
256-
diags := gohcl.DecodeBody(body, nil, &raw)
298+
return pos, diags
299+
}
300+
301+
func (r *Runner) decodeRangeFromBody(body hcl.Body) (hcl.Range, hcl.Body, hcl.Diagnostics) {
302+
var diags hcl.Diagnostics
303+
304+
rangeBody, remain, moreDiags := body.PartialContent(testFileRangeSchema)
257305

258-
return hcl.Range{
306+
diags = append(diags, moreDiags...)
307+
if rangeBody == nil {
308+
return hcl.Range{}, nil, diags
309+
}
310+
311+
var Range hcl.Range
312+
for _, block := range rangeBody.Blocks {
313+
switch block.Type {
259314
// We intentionally omit Filename here, because the test spec doesn't
260315
// need to specify that explicitly: we can infer it to be the file
261316
// path we pass to hcldec.
262-
Start: hcl.Pos{
263-
Line: raw.From.Line,
264-
Column: raw.From.Column,
265-
Byte: raw.From.Byte,
266-
},
267-
End: hcl.Pos{
268-
Line: raw.To.Line,
269-
Column: raw.To.Column,
270-
Byte: raw.To.Byte,
271-
},
272-
}, raw.Remain, diags
317+
case "from":
318+
Range.Start, moreDiags = r.decodePosFromBody(block.Body)
319+
diags = append(diags, moreDiags...)
320+
case "to":
321+
Range.End, moreDiags = r.decodePosFromBody(block.Body)
322+
diags = append(diags, moreDiags...)
323+
default:
324+
panic(fmt.Sprintf("unsupported block type %q", block.Type))
325+
}
326+
}
327+
328+
return Range, remain, diags
273329
}
274330

275331
var testFileSchema = &hcl.BodySchema{

0 commit comments

Comments
 (0)