Skip to content

Commit 4e00e1e

Browse files
authored
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
1 parent e1ab653 commit 4e00e1e

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: Avoid crash on test failure in comparison in function call
3+
time: 2025-05-16T11:42:51.289379+01:00
4+
custom:
5+
Issue: "37071"

internal/command/format/diagnostic_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,69 @@ func TestDiagnostic(t *testing.T) {
342342
[red]│[reset]
343343
[red]│[reset] LHS not equal to RHS
344344
[red]╵[reset]
345+
`,
346+
},
347+
"error originating from failed wrapped test assertion by function": {
348+
&hcl.Diagnostic{
349+
Severity: hcl.DiagError,
350+
Summary: "Test assertion failed",
351+
Detail: "Example crash",
352+
Subject: &hcl.Range{
353+
Filename: "test.tf",
354+
Start: hcl.Pos{Line: 1, Column: 6, Byte: 5},
355+
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
356+
},
357+
Expression: &hclsyntax.FunctionCallExpr{
358+
Name: "tobool",
359+
Args: []hclsyntax.Expression{
360+
&hclsyntax.BinaryOpExpr{
361+
Op: hclsyntax.OpEqual,
362+
LHS: &hclsyntax.LiteralValueExpr{
363+
Val: cty.ObjectVal(map[string]cty.Value{
364+
"inner": cty.StringVal("str1"),
365+
"extra": cty.StringVal("str2"),
366+
}),
367+
},
368+
RHS: &hclsyntax.LiteralValueExpr{
369+
Val: cty.ObjectVal(map[string]cty.Value{
370+
"inner": cty.StringVal("str11"),
371+
"extra": cty.StringVal("str21"),
372+
}),
373+
},
374+
SrcRange: hcl.Range{
375+
Filename: "test.tf",
376+
Start: hcl.Pos{Line: 1, Column: 6, Byte: 5},
377+
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
378+
},
379+
},
380+
},
381+
},
382+
EvalContext: &hcl.EvalContext{
383+
Variables: map[string]cty.Value{},
384+
Functions: map[string]function.Function{
385+
"tobool": function.New(&function.Spec{
386+
Params: []function.Parameter{
387+
{
388+
Name: "param_0",
389+
Type: cty.String,
390+
},
391+
},
392+
}),
393+
},
394+
},
395+
// This is simulating what the test assertion expression
396+
// type would generate on evaluation, by implementing the
397+
// same interface it uses.
398+
Extra: diagnosticCausedByTestFailure{true},
399+
},
400+
`[red]╷[reset]
401+
[red]│[reset] [bold][red]Error: [reset][bold]Test assertion failed[reset]
402+
[red]│[reset]
403+
[red]│[reset] on test.tf line 1:
404+
[red]│[reset] 1: test [underline]source[reset] code
405+
[red]│[reset]
406+
[red]│[reset] Example crash
407+
[red]╵[reset]
345408
`,
346409
},
347410
}

internal/command/views/json/diagnostic.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ func NewDiagnostic(diag tfdiags.Diagnostic, sources map[string][]byte) *Diagnost
446446
// If the test assertion is a binary expression, we'll include the human-readable
447447
// formatted LHS and RHS values in the diagnostic snippet.
448448
diagnostic.Snippet.TestAssertionExpr = formatRunBinaryDiag(ctx, fromExpr.Expression)
449-
diagnostic.Snippet.TestAssertionExpr.ShowVerbose = testDiag.IsTestVerboseMode()
449+
if diagnostic.Snippet.TestAssertionExpr != nil {
450+
diagnostic.Snippet.TestAssertionExpr.ShowVerbose = testDiag.IsTestVerboseMode()
451+
}
450452
}
451453

452454
}

0 commit comments

Comments
 (0)