MCPcopy
hub / github.com/hashicorp/hcl / LoadTestFile

Method LoadTestFile

cmd/hclspecsuite/test_file.go:42–139  ·  view source on GitHub ↗
(filename string)

Source from the content-addressed store, hash-verified

40}
41
42func (r *Runner) LoadTestFile(filename string) (*TestFile, hcl.Diagnostics) {
43 f, diags := r.parser.ParseHCLFile(filename)
44 if diags.HasErrors() {
45 return nil, diags
46 }
47
48 content, moreDiags := f.Body.Content(testFileSchema)
49 diags = append(diags, moreDiags...)
50 if moreDiags.HasErrors() {
51 return nil, diags
52 }
53
54 ret := &TestFile{
55 ResultType: cty.DynamicPseudoType,
56 }
57
58 if typeAttr, exists := content.Attributes["result_type"]; exists {
59 ty, moreDiags := typeexpr.TypeConstraint(typeAttr.Expr)
60 diags = append(diags, moreDiags...)
61 if !moreDiags.HasErrors() {
62 ret.ResultType = ty
63 }
64 ret.ResultTypeRange = typeAttr.Expr.Range()
65 }
66
67 if resultAttr, exists := content.Attributes["result"]; exists {
68 resultVal, moreDiags := resultAttr.Expr.Value(nil)
69 diags = append(diags, moreDiags...)
70 if !moreDiags.HasErrors() {
71 resultVal, err := convert.Convert(resultVal, ret.ResultType)
72 if err != nil {
73 diags = diags.Append(&hcl.Diagnostic{
74 Severity: hcl.DiagError,
75 Summary: "Invalid result value",
76 Detail: fmt.Sprintf("The result value does not conform to the given result type: %s.", err),
77 Subject: resultAttr.Expr.Range().Ptr(),
78 })
79 } else {
80 ret.Result = resultVal
81 }
82 }
83 ret.ResultRange = resultAttr.Expr.Range()
84 }
85
86 for _, block := range content.Blocks {
87 switch block.Type {
88
89 case "traversals":
90 if ret.ChecksTraversals {
91 // Indicates a duplicate traversals block
92 diags = diags.Append(&hcl.Diagnostic{
93 Severity: hcl.DiagError,
94 Summary: "Duplicate \"traversals\" block",
95 Detail: "Only one traversals block is expected.",
96 Subject: &block.TypeRange,
97 })
98 continue
99 }

Callers 1

runTestMethod · 0.95

Calls 10

decodeTraversalsBlockMethod · 0.95
TypeConstraintFunction · 0.92
ParseHCLFileMethod · 0.80
HasErrorsMethod · 0.80
PtrMethod · 0.80
ContentMethod · 0.65
RangeMethod · 0.65
ValueMethod · 0.65
AppendMethod · 0.45

Tested by

no test coverage detected