-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Change JUnit terraform test
output to include test failure details inside <failure>
elements, use the error message as the message
attribute
#36316
Changes from all commits
855f2e9
132b8bd
d8da87e
b0ea078
5aea43e
080505c
b6798ae
add24c6
0bbcfdd
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 | ||
---|---|---|---|---|
|
@@ -8,7 +8,10 @@ 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) { | ||||
|
@@ -67,6 +70,65 @@ func Test_TestJUnitXMLFile_save(t *testing.T) { | |||
} | ||||
} | ||||
|
||||
func Test_getWarnings(t *testing.T) { | ||||
|
||||
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.
Suggested change
|
||||
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, cmp.Comparer(diagnosticComparer)); diff != "" { | ||||
t.Errorf("wrong diagnostics\n%s", diff) | ||||
} | ||||
}) | ||||
} | ||||
} | ||||
|
||||
func Test_suiteFilesAsSortedList(t *testing.T) { | ||||
cases := map[string]struct { | ||||
Suite *moduletest.Suite | ||||
|
@@ -151,3 +213,21 @@ func Test_suiteFilesAsSortedList(t *testing.T) { | |||
}) | ||||
} | ||||
} | ||||
|
||||
// From internal/backend/remote-state/s3/testing_test.go | ||||
// diagnosticComparer is a Comparer function for use with cmp.Diff to compare two tfdiags.Diagnostic values | ||||
Comment on lines
+217
to
+218
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. Should/could this be moved into the tfdiags package? Or are there other ways that diagnostics are compared in the code base that are a better option? 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. I think moving it into We could follow the pattern of ctydebug - https://github.com/zclconf/go-cty-debug/blob/0d6042c539401a57fc0cca85ded2861d4a5173c4/ctydebug/cmp.go#L19-L42 I don't mean the implementation necessarily but more the fact that it exports a |
||||
func diagnosticComparer(l, r tfdiags.Diagnostic) bool { | ||||
if l.Severity() != r.Severity() { | ||||
return false | ||||
} | ||||
if l.Description() != r.Description() { | ||||
return false | ||||
} | ||||
|
||||
lp := tfdiags.GetAttribute(l) | ||||
rp := tfdiags.GetAttribute(r) | ||||
if len(lp) != len(rp) { | ||||
return false | ||||
} | ||||
return lp.Equals(rp) | ||||
} |
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.
Nitpick - AFAICT the code here would work just as well without
HasWarnings()
- it would return empty slice as nothing would be appended.On a separate note I am somewhat surprised we don't have
Warnings()
method yet ontfdiags.Diagnostics
- so I think it may be another candidate for logic to extract there and just call from here but feel free to leave this for another PR if you like.