Skip to content

Commit dcce20f

Browse files
authored
Improve testing of diagnostic comparer (#36455)
* Update simple comparer tests to show that sources (Subject or Context) don't impact matching diags * Add comparer that uses sources (Subject or Context) when comparing diags * Add test cases that show concrete types do affect comparison * Fix test case names * Remove `DiagnosticComparerWithSource`
1 parent 17f4dcf commit dcce20f

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

internal/tfdiags/compare.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ import "github.com/google/go-cmp/cmp"
1414
//
1515
// Example usage:
1616
//
17-
// cmp.Diff(diag1, diag2, tfdiags.DiagnosticComparer())
18-
var DiagnosticComparer cmp.Option = cmp.Comparer(diagnosticComparer)
17+
// cmp.Diff(diag1, diag2, tfdiags.DiagnosticComparer)
18+
var DiagnosticComparer cmp.Option = cmp.Comparer(diagnosticComparerSimple)
1919

20-
func diagnosticComparer(l, r Diagnostic) bool {
20+
// diagnosticComparerSimple returns false when a difference is identified between
21+
// the two Diagnostic arguments.
22+
func diagnosticComparerSimple(l, r Diagnostic) bool {
2123
if l.Severity() != r.Severity() {
2224
return false
2325
}
2426
if l.Description() != r.Description() {
2527
return false
2628
}
2729

30+
// Do the diagnostics originate from the same attribute name, if any?
2831
lp := GetAttribute(l)
2932
rp := GetAttribute(r)
3033
if len(lp) != len(rp) {

internal/tfdiags/compare_test.go

+49-7
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ func TestDiagnosticComparer(t *testing.T) {
2424
diag2 Diagnostic
2525
expectDiff bool
2626
}{
27+
// Correctly identifying things that match
2728
"reports that identical diagnostics match": {
2829
diag1: hclDiagnostic{&baseError},
2930
diag2: hclDiagnostic{&baseError},
3031
expectDiff: false,
3132
},
33+
// Correctly identifies when things don't match
34+
"reports that diagnostics don't match if the concrete type differs": {
35+
diag1: hclDiagnostic{&baseError},
36+
diag2: makeRPCFriendlyDiag(hclDiagnostic{&baseError}),
37+
expectDiff: true,
38+
},
3239
"reports that diagnostics don't match if severity differs": {
3340
diag1: hclDiagnostic{&baseError},
3441
diag2: func() Diagnostic {
@@ -65,19 +72,55 @@ func TestDiagnosticComparer(t *testing.T) {
6572
}(),
6673
expectDiff: true,
6774
},
68-
"reports that diagnostics don't match if attribute path missing from one differs": {
75+
"reports that diagnostics don't match if attribute path is missing from one": {
6976
diag1: func() Diagnostic {
7077
return AttributeValue(Error, "summary here", "detail here", cty.Path{cty.GetAttrStep{Name: "foobar1"}})
7178
}(),
7279
diag2: func() Diagnostic {
73-
d := hcl.Diagnostic{
74-
Severity: hcl.DiagError,
75-
Summary: "summary here",
76-
Detail: "detail here",
80+
return AttributeValue(Error, "summary here", "detail here", cty.Path{})
81+
}(),
82+
expectDiff: true,
83+
},
84+
// Scenarios where diagnostics will be considered equavalent, even if they aren't fully the same
85+
"reports that diagnostics match even if sources (Subject) are different; ignored in simple comparison": {
86+
diag1: hclDiagnostic{&baseError},
87+
diag2: func() Diagnostic {
88+
d := baseError
89+
d.Subject = &hcl.Range{
90+
Filename: "foobar.tf",
91+
Start: hcl.Pos{
92+
Line: 0,
93+
Column: 0,
94+
Byte: 0,
95+
},
96+
End: hcl.Pos{
97+
Line: 1,
98+
Column: 1,
99+
Byte: 1,
100+
},
101+
}
102+
return hclDiagnostic{&d}
103+
}(),
104+
},
105+
"reports that diagnostics match even if sources (Context) are different; ignored in simple comparison": {
106+
diag1: hclDiagnostic{&baseError},
107+
diag2: func() Diagnostic {
108+
d := baseError
109+
d.Context = &hcl.Range{
110+
Filename: "foobar.tf",
111+
Start: hcl.Pos{
112+
Line: 0,
113+
Column: 0,
114+
Byte: 0,
115+
},
116+
End: hcl.Pos{
117+
Line: 1,
118+
Column: 1,
119+
Byte: 1,
120+
},
77121
}
78122
return hclDiagnostic{&d}
79123
}(),
80-
expectDiff: true,
81124
},
82125
}
83126

@@ -94,5 +137,4 @@ func TestDiagnosticComparer(t *testing.T) {
94137
}
95138
})
96139
}
97-
98140
}

0 commit comments

Comments
 (0)