-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Truncate diff of very long texts if not in verbose mode (fix #12406) #12634
Open
devdanzin
wants to merge
11
commits into
pytest-dev:main
Choose a base branch
from
devdanzin:huge_text_diff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65
−8
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9a3c384
Avoid creating a diff of very long texts if not in verbose mode.
devdanzin 5a239d7
Add changelog entry.
devdanzin c06185e
Add myself to AUTHORS.
devdanzin c17e498
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6e22c94
Use constants from truncate.py instead of magical numbers in _diff_te…
devdanzin a580e6f
Merge.
devdanzin 57e0c78
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8f96cf3
Merge branch 'main' into huge_text_diff
devdanzin 83164fb
Fix the logic in _diff_text, add a test and update others.
devdanzin a48c40d
Formatting.
devdanzin e41e39a
Merge branch 'pytest-dev:main' into huge_text_diff
devdanzin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
The ``_diff_text()`` function now truncates texts before diffing them | ||
if not in verbose mode. -- by :user:`devdanzin`. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
from _pytest._io.pprint import PrettyPrinter | ||
from _pytest._io.saferepr import saferepr | ||
from _pytest._io.saferepr import saferepr_unlimited | ||
from _pytest.assertion.truncate import DEFAULT_MAX_CHARS | ||
from _pytest.assertion.truncate import DEFAULT_MAX_LINES | ||
from _pytest.config import Config | ||
|
||
|
||
|
@@ -285,17 +287,18 @@ | |
explanation: list[str] = [] | ||
|
||
if verbose < 1: | ||
i = 0 # just in case left or right has zero length | ||
for i in range(min(len(left), len(right))): | ||
if left[i] != right[i]: | ||
leading_skipped = 0 # just in case left or right has zero length | ||
for leading_skipped in range(min(len(left), len(right))): | ||
if left[leading_skipped] != right[leading_skipped]: | ||
break | ||
if i > 42: | ||
i -= 10 # Provide some context | ||
if leading_skipped > 42: | ||
leading_skipped -= 10 # Provide some context | ||
explanation = [ | ||
f"Skipping {i} identical leading characters in diff, use -v to show" | ||
f"Skipping {leading_skipped} identical leading characters in diff, use -v to show" | ||
] | ||
left = left[i:] | ||
right = right[i:] | ||
left = left[leading_skipped:] | ||
right = right[leading_skipped:] | ||
i = 0 | ||
if len(left) == len(right): | ||
for i in range(len(left)): | ||
if left[-i] != right[-i]: | ||
|
@@ -308,6 +311,21 @@ | |
] | ||
left = left[:-i] | ||
right = right[:-i] | ||
shortest = min(left, right, key=lambda x: len(x)) | ||
lines = j = start = 0 | ||
if shortest.count("\n") >= DEFAULT_MAX_LINES: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm under the impression the code can be made simpler by skipping max chars first, then dealing with max lines... 🤔 |
||
# Keep only DEFAULT_MAX_LINES, usually 8, lines | ||
if 10 < leading_skipped < 42: # We didn't skip equal leading characters | ||
start += leading_skipped - 10 | ||
for j, c in enumerate(shortest[start:], start=start - 1): | ||
if c == "\n": | ||
lines += 1 | ||
if lines > DEFAULT_MAX_LINES: | ||
break | ||
else: | ||
j = len(max(left, right, key=lambda x: len(x))) | ||
left = left[start : min(DEFAULT_MAX_CHARS, len(left), j)] | ||
right = right[start : min(DEFAULT_MAX_CHARS, len(right), j)] | ||
keepends = True | ||
if left.isspace() or right.isspace(): | ||
left = repr(str(left)) | ||
|
This file contains 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
Oops, something went wrong.
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.
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 afraid this PR got outdated after #12766.
I think the path forward would be for
_diff_text()
to receive both "max lines" and "max chars" by parameter instead (they beingint | None
, withNone
meaning "no limits").