Skip to content

Commit

Permalink
Assorted improvements following up pytest-dev#6658
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Feb 20, 2020
1 parent d1b5052 commit c91abe4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
21 changes: 14 additions & 7 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,28 +1048,35 @@ def _write_entry_lines(self, tw: TerminalWriter) -> None:
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:
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _write_source(self, lines: List[str], indents: Sequence[str] = ()) -> None:
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:
Expand Down
4 changes: 3 additions & 1 deletion testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def requires_ordered_markup(cls, result: RunResult):
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

0 comments on commit c91abe4

Please sign in to comment.