From 0b87306ec6826312c51f01e3cc2d5b914233ed0b Mon Sep 17 00:00:00 2001 From: Benjamin Schubert Date: Thu, 21 Dec 2023 16:29:06 +0000 Subject: [PATCH] fixup! fixup! fixup! fixup! fixup! fixup! Add syntactic highlighting to the error explanations --- src/_pytest/_io/terminalwriter.py | 4 +++- src/_pytest/assertion/util.py | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index 1b11539db..2b2f49e9a 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -223,12 +223,14 @@ class TerminalWriter: style=os.getenv("PYTEST_THEME"), ), ) + # pygments terminal formatter may add a newline when there wasn't one. + # We don't want this, remove. if highlighted[-1] == "\n" and source[-1] != "\n": highlighted = highlighted[:-1] # Some lexers will not set the initial color explicitly # which may lead to the previous color being propagated to the - # start of the expression + # start of the expression, so reset first. return "\x1b[0m" + highlighted except pygments.util.ClassNotFound: raise UsageError( diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index f810a60dd..fdaa792a3 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -513,7 +513,6 @@ def _compare_eq_dict( ) explanation.extend( highlighter(pprint.pformat({k: left[k] for k in extra_left})) - .strip() .splitlines() ) extra_right = set_right - set_left @@ -525,7 +524,6 @@ def _compare_eq_dict( ) explanation.extend( highlighter(pprint.pformat({k: right[k] for k in extra_right})) - .strip() .splitlines() ) return explanation