Skip to content

Add DiagnosticComparerRelaxed and tests #37258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions internal/tfdiags/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,32 @@ import "github.com/google/go-cmp/cmp"
// the package github.com/google/go-cmp/cmp.
//
// The comparer relies on the underlying Diagnostic implementing
// [ComparableDiagnostic].
// [ComparableDiagnostic] and compares all possible fields.
//
// Example usage:
//
// cmp.Diff(diag1, diag2, tfdiags.DiagnosticComparer)
var DiagnosticComparer cmp.Option = cmp.Comparer(diagnosticComparerSimple)
var DiagnosticComparer cmp.Option = cmp.Comparer(diagnosticComparerStringent)

// diagnosticComparerSimple returns false when a difference is identified between
// DiagnosticComparerRelaxed returns a cmp.Option that can be used with
// the package github.com/google/go-cmp/cmp.
//

// The comparer checks these match between the diagnostics:
// 1) Severity
// 2) Description
// 3) Attribute cty.Path, if present
//
// # The comparer ignores source Subject and Context data, for easier test assertions.
//
// Example usage:
//
// cmp.Diff(diag1, diag2, tfdiags.DiagnosticComparer)
var DiagnosticComparerRelaxed cmp.Option = cmp.Comparer(diagnosticComparerRelaxed)

// diagnosticComparerStringent returns false when a difference is identified between
// the two Diagnostic arguments.
func diagnosticComparerSimple(l, r Diagnostic) bool {
func diagnosticComparerStringent(l, r Diagnostic) bool {
ld, ok := l.(ComparableDiagnostic)
if !ok {
return false
Expand All @@ -30,3 +46,21 @@ func diagnosticComparerSimple(l, r Diagnostic) bool {

return ld.Equals(rd)
}

// diagnosticComparerRelaxed returns false when a difference is identified between
// the two Diagnostic arguments. This comparer
func diagnosticComparerRelaxed(l, r Diagnostic) bool {
if l.Severity() != r.Severity() {
return false
}
if l.Description() != r.Description() {
return false
}

lp := GetAttribute(l)
rp := GetAttribute(r)
if len(lp) != len(rp) {
return false
}
return lp.Equals(rp)
}
141 changes: 141 additions & 0 deletions internal/tfdiags/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,144 @@ func TestDiagnosticComparer(t *testing.T) {
})
}
}

func TestDiagnosticComparerRelaxed(t *testing.T) {

baseError := hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "error",
Detail: "this is an error",
Subject: &hcl.Range{
Filename: "foobar.tf",
Start: hcl.Pos{
Line: 0,
Column: 0,
Byte: 0,
},
End: hcl.Pos{
Line: 1,
Column: 1,
Byte: 1,
},
},
}

cases := map[string]struct {
diag1 Diagnostic
diag2 Diagnostic
expectDiff bool
}{
// Correctly identifying things that match
"reports that identical diagnostics match": {
diag1: hclDiagnostic{&baseError},
diag2: hclDiagnostic{&baseError},
expectDiff: false,
},
// Correctly identifies when things don't match
"reports that diagnostics don't match if the concrete type differs": {
diag1: hclDiagnostic{&baseError},
diag2: makeRPCFriendlyDiag(hclDiagnostic{&baseError}),
expectDiff: true,
},
"reports that diagnostics don't match if severity differs": {
diag1: hclDiagnostic{&baseError},
diag2: func() Diagnostic {
d := baseError
d.Severity = hcl.DiagWarning
return hclDiagnostic{&d}
}(),
expectDiff: true,
},
"reports that diagnostics don't match if summary differs": {
diag1: hclDiagnostic{&baseError},
diag2: func() Diagnostic {
d := baseError
d.Summary = "altered summary"
return hclDiagnostic{&d}
}(),
expectDiff: true,
},
"reports that diagnostics don't match if detail differs": {
diag1: hclDiagnostic{&baseError},
diag2: func() Diagnostic {
d := baseError
d.Detail = "altered detail"
return hclDiagnostic{&d}
}(),
expectDiff: true,
},
"reports that diagnostics don't match if attribute path differs": {
diag1: func() Diagnostic {
return AttributeValue(Error, "summary here", "detail here", cty.Path{cty.GetAttrStep{Name: "foobar1"}})
}(),
diag2: func() Diagnostic {
return AttributeValue(Error, "summary here", "detail here", cty.Path{cty.GetAttrStep{Name: "foobar2"}})
}(),
expectDiff: true,
},
"reports that diagnostics don't match if attribute path is missing from one": {
diag1: func() Diagnostic {
return AttributeValue(Error, "summary here", "detail here", cty.Path{cty.GetAttrStep{Name: "foobar1"}})
}(),
diag2: func() Diagnostic {
return AttributeValue(Error, "summary here", "detail here", cty.Path{})
}(),
expectDiff: true,
},
// Scenarios where diagnostics will be considered equivalent, even if they aren't fully the same
"reports that diagnostics match even if sources (Subject) are different; ignored in simple comparison": {
diag1: hclDiagnostic{&baseError},
diag2: func() Diagnostic {
d := baseError
d.Subject = &hcl.Range{
Filename: "foobar.tf",
Start: hcl.Pos{
Line: 0,
Column: 0,
Byte: 0,
},
End: hcl.Pos{
Line: 1,
Column: 1,
Byte: 1,
},
}
return hclDiagnostic{&d}
}(),
},
"reports that diagnostics match even if sources (Context) are different; ignored in simple comparison": {
diag1: hclDiagnostic{&baseError},
diag2: func() Diagnostic {
d := baseError
d.Context = &hcl.Range{
Filename: "foobar.tf",
Start: hcl.Pos{
Line: 0,
Column: 0,
Byte: 0,
},
End: hcl.Pos{
Line: 1,
Column: 1,
Byte: 1,
},
}
return hclDiagnostic{&d}
}(),
},
}

for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
output := cmp.Diff(tc.diag1, tc.diag2, DiagnosticComparerRelaxed)

diffFound := output != ""
if diffFound && !tc.expectDiff {
t.Fatalf("unexpected diff detected:\n%s", output)
}
if !diffFound && tc.expectDiff {
t.Fatal("expected a diff but none was detected")
}
})
}
}
Loading