-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
(🎁) color the diff of strings #13175
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
Comments
Huh, interesting! Here is a quick hack that extends diff --git i/src/_pytest/assertion/util.py w/src/_pytest/assertion/util.py
index 3fe7eb9d8..9a7c6da72 100644
--- i/src/_pytest/assertion/util.py
+++ w/src/_pytest/assertion/util.py
@@ -242,7 +242,7 @@ def _compare_eq_any(
) -> list[str]:
explanation = []
if istext(left) and istext(right):
- explanation = _diff_text(left, right, verbose)
+ explanation = _diff_text(left, right, highlighter, verbose)
else:
from _pytest.python_api import ApproxBase
@@ -274,7 +274,9 @@ def _compare_eq_any(
return explanation
-def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
+def _diff_text(
+ left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0
+) -> list[str]:
"""Return the explanation for the diff between text.
Unless --verbose is used this will skip leading and trailing
@@ -315,10 +317,15 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
explanation += ["Strings contain only whitespace, escaping them using repr()"]
# "right" is the expected base against which we compare "left",
# see https://github.com/pytest-dev/pytest/issues/3333
- explanation += [
- line.strip("\n")
- for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
- ]
+ explanation.extend(
+ highlighter(
+ "\n".join(
+ line.strip("\n")
+ for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
+ ),
+ lexer="diff",
+ ).splitlines()
+ )
return explanation
and from a quick test, that seems to help: @BenjaminSchubert was there a reason you didn't do |
Hey! This was an oversight, I rarely use the direct string diff and thus did not realize I missed something. I think it's a good idea to extend it here too! |
Thanks @BenjaminSchubert, and thanks @KotlinIsland for the detailed report! I opened #13189 to fix this and also make pygments a mandatory dependecy. |
Warning
it is CRITICAL that
pygments
is installed, otherwise the diff won't be colored at allthis is a secret undocumented feature, see: #13174
here we can observe that no coloring has been applied to the string diff
pip list
from the virtual environment you are usingwindows 10
The text was updated successfully, but these errors were encountered: