fix: Avoid crash on test failure in BinaryOpExpr in function call (#37071)

* fix: Avoid crash on test failure in BinaryOpExpr in function call

* add changelog entry
pull/36922/head^2
Radek Simko 1 year ago committed by GitHub
parent e1ab6530ab
commit 4e00e1ecdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
kind: BUG FIXES
body: Avoid crash on test failure in comparison in function call
time: 2025-05-16T11:42:51.289379+01:00
custom:
Issue: "37071"

@ -342,6 +342,69 @@ func TestDiagnostic(t *testing.T) {
[red][reset]
[red][reset] LHS not equal to RHS
[red][reset]
`,
},
"error originating from failed wrapped test assertion by function": {
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Test assertion failed",
Detail: "Example crash",
Subject: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 6, Byte: 5},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
},
Expression: &hclsyntax.FunctionCallExpr{
Name: "tobool",
Args: []hclsyntax.Expression{
&hclsyntax.BinaryOpExpr{
Op: hclsyntax.OpEqual,
LHS: &hclsyntax.LiteralValueExpr{
Val: cty.ObjectVal(map[string]cty.Value{
"inner": cty.StringVal("str1"),
"extra": cty.StringVal("str2"),
}),
},
RHS: &hclsyntax.LiteralValueExpr{
Val: cty.ObjectVal(map[string]cty.Value{
"inner": cty.StringVal("str11"),
"extra": cty.StringVal("str21"),
}),
},
SrcRange: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 6, Byte: 5},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
},
},
},
},
EvalContext: &hcl.EvalContext{
Variables: map[string]cty.Value{},
Functions: map[string]function.Function{
"tobool": function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "param_0",
Type: cty.String,
},
},
}),
},
},
// This is simulating what the test assertion expression
// type would generate on evaluation, by implementing the
// same interface it uses.
Extra: diagnosticCausedByTestFailure{true},
},
`[red][reset]
[red][reset] [bold][red]Error: [reset][bold]Test assertion failed[reset]
[red][reset]
[red][reset] on test.tf line 1:
[red][reset] 1: test [underline]source[reset] code
[red][reset]
[red][reset] Example crash
[red][reset]
`,
},
}

@ -446,7 +446,9 @@ func NewDiagnostic(diag tfdiags.Diagnostic, sources map[string][]byte) *Diagnost
// If the test assertion is a binary expression, we'll include the human-readable
// formatted LHS and RHS values in the diagnostic snippet.
diagnostic.Snippet.TestAssertionExpr = formatRunBinaryDiag(ctx, fromExpr.Expression)
diagnostic.Snippet.TestAssertionExpr.ShowVerbose = testDiag.IsTestVerboseMode()
if diagnostic.Snippet.TestAssertionExpr != nil {
diagnostic.Snippet.TestAssertionExpr.ShowVerbose = testDiag.IsTestVerboseMode()
}
}
}

Loading…
Cancel
Save