Skip to content

Commit

Permalink
assert: handle TokenTooLong error scenario while formatting assertion…
Browse files Browse the repository at this point in the history
… messages

As pointed out in stretchr#1525, when the assertion message is too long, it gets
completely truncated in the final output. This is because,
`bufio.Scanner.Scan()` has a default `MaxScanTokenSize` set to `65536`
characters (64 * 1024). The `Scan()` function return false whenever the
line being scanned exceeds that max limit. This leads to the final
assertion message being truncated.

This commit fixes that by manually setting the internal scan buffer size
to `len(message) + 1` to make sure that above scenario never occurs.

Fixes stretchr#1525
  • Loading branch information
arjunmahishi committed Mar 3, 2024
1 parent bb548d0 commit 7f5f3e3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
14 changes: 9 additions & 5 deletions assert/assertions.go
Expand Up @@ -301,13 +301,17 @@ func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
func indentMessageLines(message string, longestLabelLen int) string {
outBuf := new(bytes.Buffer)

for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ {
// no need to align first line because it starts at the correct location (after the label)
if i != 0 {
// append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab
msgScanner := bufio.NewScanner(strings.NewReader(message))
msgScanner.Buffer([]byte{}, len(message)+1)

first := true
for msgScanner.Scan() {
if !first {
outBuf.WriteString("\n\t" + strings.Repeat(" ", longestLabelLen+1) + "\t")
}
outBuf.WriteString(scanner.Text())

outBuf.WriteString(msgScanner.Text())
first = false
}

return outBuf.String()
Expand Down
42 changes: 42 additions & 0 deletions assert/assertions_test.go
Expand Up @@ -3192,3 +3192,45 @@ func TestErrorAs(t *testing.T) {
})
}
}

func Test_indentMessageLines(t *testing.T) {
tt := []struct {
name string
msg string
longestLabelLen int
expected string
}{
{
name: "single line",
msg: "Hello World\n",
longestLabelLen: 11,
expected: "Hello World",
},
{
name: "multi line",
msg: "Hello\nWorld\n",
longestLabelLen: 11,
expected: "Hello\n\t \tWorld",
},
{
name: "single line - extreamly long",
msg: strings.Repeat("hello ", 20000),
longestLabelLen: 11,
expected: strings.Repeat("hello ", 20000),
},
{
name: "multi line - extreamly long",
msg: strings.Repeat("hello\n", 20000),
longestLabelLen: 3,
expected: strings.TrimSpace(
strings.TrimPrefix(strings.Repeat("\thello\n\t ", 20000), "\t"),
),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
Equal(t, tc.expected, indentMessageLines(tc.msg, tc.longestLabelLen))
})
}
}

0 comments on commit 7f5f3e3

Please sign in to comment.