assert: keep the beginning of long strings when truncating - #65176
Closed
arhxam wants to merge 1 commit into
Closed
Conversation
`addEllipsis()` truncates the long single-line `actual`/`expected` values shown in `AssertionError`'s custom inspect output. For a string longer than `kMaxLongStringLength` (512) it called `StringPrototypeSlice(string, kMaxLongStringLength)`, which drops the first 512 characters and keeps the tail, then appends `...` as if the end had been cut off. The three sibling truncations in the same file and the multi-line branch of this same function all keep the beginning of the string instead. Pass a start index of `0` so the first 512 characters are kept, which matches the sibling behaviour and the trailing `...`. The existing tests used homogeneous strings (`'A'.repeat(n)`), so the head and tail were indistinguishable and the buggy tail length was baked into the expectations; update those and add a heterogeneous regression test that actually distinguishes the beginning from the end. Signed-off-by: Arham Wani <arhamwani765@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
addEllipsis()inlib/internal/assert/assertion_error.jstruncates thelong single-line
actual/expectedvalues shown byAssertionError'scustom
util.inspectoutput. For a string longer thankMaxLongStringLength(512) it did:StringPrototypeSlice(string, 512)drops the first 512 characters andkeeps the tail, then appends
...as if the end had been cut off —so the ellipsis is misleading and the wrong part of the string is shown.
Reproduction on current
main:The three sibling truncations in the same file and the multi-line branch
of this very function all keep the beginning of the string. This
changes the single-line branch to
StringPrototypeSlice(string, 0, kMaxLongStringLength)so the first 512 characters are kept, consistentwith the trailing
....Why the existing tests did not catch it
The existing tests truncated homogeneous strings (
'A'.repeat(n)), wherethe head and tail are identical, so the buggy tail length
(
n - 512) was baked into the expectations (e.g.'A'.repeat(488),substring(0, 9_488)). Those expectations are corrected to512, and anew heterogeneous regression test (
'a'.repeat(300) + 'b'.repeat(300)) isadded that actually distinguishes the beginning from the end.
Verification
main, then confirmed thefix flips it (the new test fails on unpatched source, passes with the fix).
python3 tools/test.pyon alltest/parallel/test-assert*.js(14 files)and
test-util-inspect.js: all pass.