Skip to content

assert: fix failure message hidden when a diff line exceeds 64KiB#1925

Open
malikov73 wants to merge 1 commit into
stretchr:masterfrom
malikov73:fix-long-line-diff
Open

assert: fix failure message hidden when a diff line exceeds 64KiB#1925
malikov73 wants to merge 1 commit into
stretchr:masterfrom
malikov73:fix-long-line-diff

Conversation

@malikov73

Copy link
Copy Markdown

Summary

Bug fix. Fixes #746.

indentMessageLines uses bufio.Scanner, which errors out on lines longer than bufio.MaxScanTokenSize (64KiB). When that happens, the entire labeled content is replaced with cannot display message: bufio.Scanner: token too long, so the user sees neither the diff nor the values — only an empty Error: section.

truncatingFormat protects the expected:/actual: representations, but diff() output is not truncated, so this still reproduces today, e.g.:

longLine := strings.Repeat("a", bufio.MaxScanTokenSize)
assert.Equal(t, longLine+"expected", longLine+"actual")

prints

    Error Trace:	example_test.go:12
    Error:      	cannot display message: bufio.Scanner: token too long
    Test:       	TestExample

The same happens for structs with a long string field (spew prints strings on one line) and for a long single-line user message in msgAndArgs.

Changes

  • indentMessageLines splits the message with strings.Split instead of bufio.Scanner, so there is no line-length limit and the cannot display message failure mode is gone. bufio.ScanLines semantics are preserved: the empty element after a trailing final "\n" is ignored and "\r\n" is treated as a line ending (a trailing "\r" is stripped from each line).
  • Regression test TestEqualLongDiffLines (fails on master with cannot display message: bufio.Scanner: token too long, passes with the fix).

No API changes; output is byte-for-byte identical for messages that the scanner could already handle.

 stretchr#746)

indentMessageLines used bufio.Scanner, which errors out on lines longer
than bufio.MaxScanTokenSize (64KiB) and replaced the whole failure
message with "cannot display message: bufio.Scanner: token too long".
This happened e.g. on assert.Equal of long single-line strings or of
structs with a long string field, because diff() output is not
truncated.

Split the message manually instead, preserving bufio.ScanLines
semantics (trailing final newline ignored, \r\n treated as a line
ending).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diff is empty when lines are too long

1 participant