@@ -43,6 +43,14 @@ def __call__(self, source: str, lexer: Literal["diff", "python"] = "python") ->
43
43
"""Apply highlighting to the given source."""
44
44
45
45
46
+ def dummy_highlighter (source : str , lexer : Literal ["diff" , "python" ] = "python" ) -> str :
47
+ """Dummy highlighter that returns the text unprocessed.
48
+
49
+ Needed for _notin_text, as the diff gets post-processed to only show the "+" part.
50
+ """
51
+ return source
52
+
53
+
46
54
def format_explanation (explanation : str ) -> str :
47
55
r"""Format an explanation.
48
56
@@ -242,7 +250,7 @@ def _compare_eq_any(
242
250
) -> list [str ]:
243
251
explanation = []
244
252
if istext (left ) and istext (right ):
245
- explanation = _diff_text (left , right , verbose )
253
+ explanation = _diff_text (left , right , highlighter , verbose )
246
254
else :
247
255
from _pytest .python_api import ApproxBase
248
256
@@ -274,7 +282,9 @@ def _compare_eq_any(
274
282
return explanation
275
283
276
284
277
- def _diff_text (left : str , right : str , verbose : int = 0 ) -> list [str ]:
285
+ def _diff_text (
286
+ left : str , right : str , highlighter : _HighlightFunc , verbose : int = 0
287
+ ) -> list [str ]:
278
288
"""Return the explanation for the diff between text.
279
289
280
290
Unless --verbose is used this will skip leading and trailing
@@ -315,10 +325,15 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
315
325
explanation += ["Strings contain only whitespace, escaping them using repr()" ]
316
326
# "right" is the expected base against which we compare "left",
317
327
# see https://github.com/pytest-dev/pytest/issues/3333
318
- explanation += [
319
- line .strip ("\n " )
320
- for line in ndiff (right .splitlines (keepends ), left .splitlines (keepends ))
321
- ]
328
+ explanation .extend (
329
+ highlighter (
330
+ "\n " .join (
331
+ line .strip ("\n " )
332
+ for line in ndiff (right .splitlines (keepends ), left .splitlines (keepends ))
333
+ ),
334
+ lexer = "diff" ,
335
+ ).splitlines ()
336
+ )
322
337
return explanation
323
338
324
339
@@ -586,7 +601,7 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> list[str]:
586
601
head = text [:index ]
587
602
tail = text [index + len (term ) :]
588
603
correct_text = head + tail
589
- diff = _diff_text (text , correct_text , verbose )
604
+ diff = _diff_text (text , correct_text , dummy_highlighter , verbose )
590
605
newdiff = [f"{ saferepr (term , maxsize = 42 )} is contained here:" ]
591
606
for line in diff :
592
607
if line .startswith ("Skipping" ):
0 commit comments