From c91abe48bae12ce597ce737c19b8e71e4ab8603e Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 20 Feb 2020 18:51:41 -0300 Subject: [PATCH] Assorted improvements following up #6658 --- src/_pytest/_code/code.py | 21 ++++++++++++++------- src/_pytest/_io/__init__.py | 2 +- testing/conftest.py | 4 +++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index fd38b950c..9d65289df 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -1048,28 +1048,35 @@ class ReprEntry(TerminalRepr): character, as doing so might break line continuations. """ - indent_size = 4 - - def is_fail(line): - return line.startswith("{} ".format(FormattedExcinfo.fail_marker)) - if not self.lines: return # separate indents and source lines that are not failures: we want to # highlight the code but not the indentation, which may contain markers # such as "> assert 0" + fail_marker = "{} ".format(FormattedExcinfo.fail_marker) + indent_size = len(fail_marker) indents = [] source_lines = [] + failure_lines = [] + seeing_failures = False for line in self.lines: - if not is_fail(line): + is_source_line = not line.startswith(fail_marker) + if is_source_line: + assert not seeing_failures, ( + "Unexpected failure lines between source lines:\n" + + "\n".join(self.lines) + ) indents.append(line[:indent_size]) source_lines.append(line[indent_size:]) + else: + seeing_failures = True + failure_lines.append(line) tw._write_source(source_lines, indents) # failure lines are always completely red and bold - for line in (x for x in self.lines if is_fail(x)): + for line in failure_lines: tw.line(line, bold=True, red=True) def toterminal(self, tw: TerminalWriter) -> None: diff --git a/src/_pytest/_io/__init__.py b/src/_pytest/_io/__init__.py index f56579806..28ddc7b78 100644 --- a/src/_pytest/_io/__init__.py +++ b/src/_pytest/_io/__init__.py @@ -26,7 +26,7 @@ class TerminalWriter(BaseTerminalWriter): self.line(indent + new_line) def _highlight(self, source): - """Highlight the given source code according to the "code_highlight" option""" + """Highlight the given source code if we have markup support""" if not self.hasmarkup: return source try: diff --git a/testing/conftest.py b/testing/conftest.py index 90cdcb869..58386b162 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -192,6 +192,8 @@ def color_mapping(): output = result.stdout.str() assert "test session starts" in output assert "\x1b[1m" in output - pytest.skip("doing limited testing because lacking ordered markup") + pytest.skip( + "doing limited testing because lacking ordered markup on py35" + ) return ColorMapping