-
Notifications
You must be signed in to change notification settings - Fork 96
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
Add convenience functions for ErrorsCount
, WarningsCount
, Errors
andWarnings
#392
Changes from all commits
7d98308
5bc3988
8a148e7
a2e97bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
diag: `ErrorsCount`, `WarningsCount`, `Errors` and `Warnings` functions have been added to `diag.Diagnostics` | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
) | ||
|
@@ -518,3 +519,179 @@ func TestDiagnosticsHasError(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestDiagnosticsErrorsCount(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
diags diag.Diagnostics | ||
expected int | ||
} | ||
tests := map[string]testCase{ | ||
"nil": { | ||
diags: nil, | ||
expected: 0, | ||
}, | ||
"empty": { | ||
diags: diag.Diagnostics{}, | ||
expected: 0, | ||
}, | ||
"errors": { | ||
diags: diag.Diagnostics{ | ||
diag.NewErrorDiagnostic("Error Summary", "Error detail."), | ||
diag.NewWarningDiagnostic("Warning Summary", "Warning detail."), | ||
}, | ||
expected: 1, | ||
}, | ||
"warnings": { | ||
diags: diag.Diagnostics{ | ||
diag.NewWarningDiagnostic("Error Summary", "Error detail."), | ||
}, | ||
expected: 0, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
got := test.diags.ErrorsCount() | ||
|
||
if diff := cmp.Diff(test.expected, got); diff != "" { | ||
t.Fatalf("expected: %q, got: %q", test.expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDiagnosticsWarningsCount(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
diags diag.Diagnostics | ||
expected int | ||
} | ||
tests := map[string]testCase{ | ||
"nil": { | ||
diags: nil, | ||
expected: 0, | ||
}, | ||
"empty": { | ||
diags: diag.Diagnostics{}, | ||
expected: 0, | ||
}, | ||
"errors": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Similar to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added additional test coverage. |
||
diags: diag.Diagnostics{ | ||
diag.NewErrorDiagnostic("Error Summary", "Error detail."), | ||
diag.NewWarningDiagnostic("Warning Summary", "Warning detail."), | ||
}, | ||
expected: 1, | ||
}, | ||
"warnings": { | ||
diags: diag.Diagnostics{ | ||
diag.NewErrorDiagnostic("Error Summary", "Error detail."), | ||
}, | ||
expected: 0, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
got := test.diags.WarningsCount() | ||
|
||
if diff := cmp.Diff(test.expected, got); diff != "" { | ||
t.Fatalf("expected: %q, got: %q", test.expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDiagnosticsErrors(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
diags diag.Diagnostics | ||
expected diag.Diagnostics | ||
} | ||
tests := map[string]testCase{ | ||
"nil": { | ||
diags: nil, | ||
expected: diag.Diagnostics{}, | ||
}, | ||
"empty": { | ||
diags: diag.Diagnostics{}, | ||
expected: diag.Diagnostics{}, | ||
}, | ||
"errors": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Might be good to check what happens when:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added additional test coverage. |
||
diags: diag.Diagnostics{ | ||
diag.NewErrorDiagnostic("Error Summary", "Error detail."), | ||
diag.NewWarningDiagnostic("Warning Summary", "Warning detail."), | ||
}, | ||
expected: diag.Diagnostics{ | ||
diag.NewErrorDiagnostic("Error Summary", "Error detail."), | ||
}, | ||
}, | ||
"warnings": { | ||
diags: diag.Diagnostics{ | ||
diag.NewWarningDiagnostic("Warning Summary", "Warning detail."), | ||
}, | ||
expected: diag.Diagnostics{}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
got := test.diags.Errors() | ||
|
||
if diff := cmp.Diff(test.expected, got); diff != "" { | ||
t.Fatalf("expected: %q, got: %q", test.expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDiagnosticsWarnings(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
diags diag.Diagnostics | ||
expected diag.Diagnostics | ||
} | ||
tests := map[string]testCase{ | ||
"nil": { | ||
diags: nil, | ||
expected: diag.Diagnostics{}, | ||
}, | ||
"empty": { | ||
diags: diag.Diagnostics{}, | ||
expected: diag.Diagnostics{}, | ||
}, | ||
"errors": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Might be good to check what happens when:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added additional test coverage. |
||
diags: diag.Diagnostics{ | ||
diag.NewErrorDiagnostic("Error Summary", "Error detail."), | ||
}, | ||
expected: diag.Diagnostics{}, | ||
}, | ||
"warnings": { | ||
diags: diag.Diagnostics{ | ||
diag.NewErrorDiagnostic("Error Summary", "Error detail."), | ||
diag.NewWarningDiagnostic("Warning Summary", "Warning detail."), | ||
}, | ||
expected: diag.Diagnostics{ | ||
diag.NewWarningDiagnostic("Warning Summary", "Warning detail."), | ||
}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
got := test.diags.Warnings() | ||
|
||
if diff := cmp.Diff(test.expected, got); diff != "" { | ||
t.Fatalf("expected: %q, got: %q", test.expected, got) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Similar to the
Errors
testing, I think we should cover some additional cases just to ensure it returns0
instead of panics, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added additional test coverage.