Skip to content

Conversation

brackendawson
Copy link
Collaborator

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

  • Make the message line indenter show an appropriate error if it fails to scan a line from the message.
  • Use the existing truncatingFormat function to truncate potentially long objects in assertions which format expected/actual values in full.
  • Parameterize the format string used by truncatingFormat.
  • Reduce the limit used by truncatingFormat by almost half to support failure messages showing both long expected and long actual values on the same line.
  • Change the format of NotSubset from %q to %#v to match that of Subset.

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

@dolmen dolmen added pkg-assert Change related to package testify/assert bug enhancement: output format Enhancement related to formatting of messages must-rebase assert.Len labels Jun 2, 2025
@dolmen dolmen changed the title Truncate very long objects in test failure messages assert: truncate very long objects in test failure messages Jun 2, 2025
@brackendawson brackendawson removed must-rebase enhancement: output format Enhancement related to formatting of messages labels Sep 16, 2025
Comment on lines +1036 to +1039
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...)
Copy link
Collaborator

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

Copy link
Collaborator Author

@brackendawson brackendawson Sep 18, 2025

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.

Copy link
Collaborator

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.

Copy link
Collaborator Author

@brackendawson brackendawson Sep 19, 2025

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.

@brackendawson
Copy link
Collaborator Author

Thank you @ccoVeille. I've updated the PR to address some comments and responded with a defence in the others.

@brackendawson brackendawson merged commit 49bcd2a into stretchr:master Sep 19, 2025
9 checks passed
@brackendawson brackendawson deleted the token-too-long branch September 19, 2025 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert.Len bug pkg-assert Change related to package testify/assert
Projects
None yet
Development

Successfully merging this pull request may close these issues.

assert/require.Len doesn't print anything if the slice is too long
3 participants