Skip to content

Commit

Permalink
tfdiags: add Warnings method (#36392)
Browse files Browse the repository at this point in the history
Adds a new method to the tfdiags API: Warnings() returns a new Diagnostics list containing only diagnostics with a severity of Warning.
  • Loading branch information
bschaatsbergen authored Jan 23, 2025
1 parent b16a509 commit 61acb0c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 75 deletions.
15 changes: 1 addition & 14 deletions internal/command/junit/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func junitXMLTestReport(suite *moduletest.Suite, suiteRunnerStopped bool, source
if run.Status == moduletest.Error && run.Diagnostics.HasWarnings() {
// If the test case errored, then all Error diags are in the "error" element
// Therefore we'd only need to include warnings in "system-err"
systemErrDiags = getWarnings(run.Diagnostics)
systemErrDiags = run.Diagnostics.Warnings()
}

if run.Status != moduletest.Error {
Expand Down Expand Up @@ -354,16 +354,3 @@ func getDiagString(diags tfdiags.Diagnostics, sources map[string][]byte) string
}
return diagsStr.String()
}

func getWarnings(diags tfdiags.Diagnostics) tfdiags.Diagnostics {
// Collect warnings
warnDiags := tfdiags.Diagnostics{}
if diags.HasWarnings() {
for _, d := range diags {
if d.Severity() == tfdiags.Warning {
warnDiags = warnDiags.Append(d)
}
}
}
return warnDiags
}
61 changes: 0 additions & 61 deletions internal/command/junit/junit_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import (
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/internal/moduletest"
"github.com/hashicorp/terraform/internal/tfdiags"
)

func Test_TestJUnitXMLFile_save(t *testing.T) {
Expand Down Expand Up @@ -70,64 +67,6 @@ func Test_TestJUnitXMLFile_save(t *testing.T) {
}
}

func Test_getWarnings(t *testing.T) {
errorDiag := &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "error",
Detail: "this is an error",
}

warnDiag := &hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "warning",
Detail: "this is a warning",
}

cases := map[string]struct {
diags tfdiags.Diagnostics
expected tfdiags.Diagnostics
}{
"empty diags": {
diags: tfdiags.Diagnostics{},
expected: tfdiags.Diagnostics{},
},
"nil diags": {
diags: nil,
expected: tfdiags.Diagnostics{},
},
"all error diags": {
diags: func() tfdiags.Diagnostics {
var d tfdiags.Diagnostics
d = d.Append(errorDiag, errorDiag, errorDiag)
return d
}(),
expected: tfdiags.Diagnostics{},
},
"mixture of error and warning diags": {
diags: func() tfdiags.Diagnostics {
var d tfdiags.Diagnostics
d = d.Append(errorDiag, errorDiag, warnDiag) // 1 warning
return d
}(),
expected: func() tfdiags.Diagnostics {
var d tfdiags.Diagnostics
d = d.Append(warnDiag) // 1 warning
return d
}(),
},
}

for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
warnings := getWarnings(tc.diags)

if diff := cmp.Diff(tc.expected, warnings, tfdiags.DiagnosticComparer); diff != "" {
t.Errorf("wrong diagnostics\n%s", diff)
}
})
}
}

func Test_suiteFilesAsSortedList(t *testing.T) {
cases := map[string]struct {
Suite *moduletest.Suite
Expand Down
11 changes: 11 additions & 0 deletions internal/tfdiags/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ func diagnosticsForError(err error) []Diagnostic {
}
}

// Warnings returns a Diagnostics list containing only diagnostics with a severity of Warning.
func (diags Diagnostics) Warnings() Diagnostics {
var warns = Diagnostics{}
for _, diag := range diags {
if diag.Severity() == Warning {
warns = append(warns, diag)
}
}
return warns
}

// HasErrors returns true if any of the diagnostics in the list have
// a severity of Error.
func (diags Diagnostics) HasErrors() bool {
Expand Down
71 changes: 71 additions & 0 deletions internal/tfdiags/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl/v2"
)

Expand Down Expand Up @@ -474,3 +475,73 @@ func TestDiagnosticsNonFatalErr(t *testing.T) {
}
})
}

func TestWarnings(t *testing.T) {
errorDiag := &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "something bad happened",
Detail: "details of the error",
}

warnDiag := &hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "something bad happened",
Detail: "details of the warning",
}

cases := map[string]struct {
diags Diagnostics
expected Diagnostics
}{
"empty diags": {
diags: Diagnostics{},
expected: Diagnostics{},
},
"nil diags": {
diags: nil,
expected: Diagnostics{},
},
"all error diags": {
diags: func() Diagnostics {
var d Diagnostics
d = d.Append(errorDiag, errorDiag, errorDiag)
return d
}(),
expected: Diagnostics{},
},
"mixture of error and warning diags": {
diags: func() Diagnostics {
var d Diagnostics
d = d.Append(errorDiag, errorDiag, warnDiag)
return d
}(),
expected: func() Diagnostics {
var d Diagnostics
d = d.Append(warnDiag)
return d
}(),
},
"empty error diags": {
diags: func() Diagnostics {
var d Diagnostics
d = d.Append(warnDiag, warnDiag)
return d
}(),
expected: func() Diagnostics {
var d Diagnostics
d = d.Append(warnDiag, warnDiag)
return d
}(),
},
}

for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
warnings := tc.diags.Warnings()

if diff := cmp.Diff(tc.expected, warnings, DiagnosticComparer); diff != "" {
t.Errorf("wrong diagnostics\n%s", diff)
}
})
}
}

0 comments on commit 61acb0c

Please sign in to comment.