-
Notifications
You must be signed in to change notification settings - Fork 1.7k
assert: truncate very long objects in test failure messages #1646
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
Conversation
ff41e65
to
2f8b16d
Compare
2f8b16d
to
1eb5fc0
Compare
return Fail(t, fmt.Sprintf("%s does not contain %s", truncatingFormat("%#v", list), truncatingFormat("%#v", subset)), msgAndArgs...) | ||
} | ||
if !ObjectsAreEqual(ev.Interface(), av.Interface()) { | ||
return Fail(t, fmt.Sprintf("%#v does not contain %#v", list, subset), msgAndArgs...) | ||
return Fail(t, fmt.Sprintf("%s does not contain %s", truncatingFormat("%#v", list), truncatingFormat("%#v", subset)), msgAndArgs...) |
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.
I'm a bit worried about these 2
We have 2 list that could be huge that would be truncated here, but displayed on the same line.
Maybe it works because of the max/2-200, but I feel like the line might difficult to read because of the lack of \n that we have for almost all others failures
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.
I agree about the hard to read part, but that would be a different issue. Again the only aim here is to show strings which previously got swallowed.
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.
I understand your point. But mine wasn't about readability.
I just wanted to make sure that comparing two slices that very long won't lead to a line that cannot be displayed.
I feel like that tge point of the max/2-200 but I wanted to make sure it would work.
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.
Yes, that doesn't sit 100% with me either. When we follow this up with #1235 and reduce the max length much further I will be very confident.
Thank you @ccoVeille. I've updated the PR to address some comments and responded with a defence in the others. |
Summary
Truncate object representations in test failure output messages that would cause the message to be too long to scan.
This is a different approach than that in #1559. I feel this is more appropriate because is it safer. The other approach has the potential to allocate excessive memory and to print excessive output to the console.
Changes
truncatingFormat
function to truncate potentially long objects in assertions which format expected/actual values in full.truncatingFormat
.truncatingFormat
by almost half to support failure messages showing both long expected and long actual values on the same line.NotSubset
from%q
to%#v
to match that ofSubset
.Motivation
So that people don't see no output in the failure message when making assertions (such as
Len
) against very long objects.Related issues
Closes #1525