Defer assertion-diff highlighting so JUnit XML stays free of ANSI - #14881
Open
arronKler wants to merge 2 commits into
Open
Defer assertion-diff highlighting so JUnit XML stays free of ANSI#14881arronKler wants to merge 2 commits into
arronKler wants to merge 2 commits into
Conversation
Verbose assertion explanations were highlighted at comparison time, so JUnit XML serialized the raw ANSI codes (as #x1B...) whenever color and -v/-vv were enabled. Mark highlight spans instead and resolve them only when writing to a color terminal. The public exception message and XML report stay plain, and escape sequences that belong to the values under test are left intact. Fixes pytest-dev#12365
for more information, see https://pre-commit.ci
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.
closes #XYZWto the PR description and/or commits.changelogdirectory.AUTHORSin alphabetical order.Fixes #12365.
Problem
With Pygments installed, verbose assertion diffs (
-v/-vv) bake SGR escape sequences into the explanation strings at comparison time.--junitxmlthen writes those codes throughbin_xml_escape(), which turns ESC into#x1Band leaves fragments like#x1B[94m1#x1B[39;49;00min both the failuremessageattribute and the failure body.A previous attempt (#14605) stripped ANSI from
TerminalRepr.__str__. That was the wrong layer: it also removed escape sequences that belong to the values under test (for exampleassert render() == "\x1b[31mred\x1b[0m"), and it never touchedreprcrash.message.Solution
Do not apply Pygments when building the explanation. Wrap highlighted spans in private markers, keep the public
AssertionErrormessage plain, and resolve the markers only when writing to a color terminal. JUnit XML and other plain-text consumers see the original source, so pytest-generated colors disappear from the report while user-provided escape sequences remain visible.