Skip to content

Commit 429a28e

Browse files
authored
Merge pull request #6782 from nicoddemus/code-highlight-followup
Assorted improvements following up #6658
2 parents aae0579 + c91abe4 commit 429a28e

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/_pytest/_code/code.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,28 +1041,35 @@ def _write_entry_lines(self, tw: TerminalWriter) -> None:
10411041
character, as doing so might break line continuations.
10421042
"""
10431043

1044-
indent_size = 4
1045-
1046-
def is_fail(line):
1047-
return line.startswith("{} ".format(FormattedExcinfo.fail_marker))
1048-
10491044
if not self.lines:
10501045
return
10511046

10521047
# separate indents and source lines that are not failures: we want to
10531048
# highlight the code but not the indentation, which may contain markers
10541049
# such as "> assert 0"
1050+
fail_marker = "{} ".format(FormattedExcinfo.fail_marker)
1051+
indent_size = len(fail_marker)
10551052
indents = []
10561053
source_lines = []
1054+
failure_lines = []
1055+
seeing_failures = False
10571056
for line in self.lines:
1058-
if not is_fail(line):
1057+
is_source_line = not line.startswith(fail_marker)
1058+
if is_source_line:
1059+
assert not seeing_failures, (
1060+
"Unexpected failure lines between source lines:\n"
1061+
+ "\n".join(self.lines)
1062+
)
10591063
indents.append(line[:indent_size])
10601064
source_lines.append(line[indent_size:])
1065+
else:
1066+
seeing_failures = True
1067+
failure_lines.append(line)
10611068

10621069
tw._write_source(source_lines, indents)
10631070

10641071
# failure lines are always completely red and bold
1065-
for line in (x for x in self.lines if is_fail(x)):
1072+
for line in failure_lines:
10661073
tw.line(line, bold=True, red=True)
10671074

10681075
def toterminal(self, tw: TerminalWriter) -> None:

src/_pytest/_io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _write_source(self, lines: List[str], indents: Sequence[str] = ()) -> None:
2626
self.line(indent + new_line)
2727

2828
def _highlight(self, source):
29-
"""Highlight the given source code according to the "code_highlight" option"""
29+
"""Highlight the given source code if we have markup support"""
3030
if not self.hasmarkup:
3131
return source
3232
try:

testing/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ def requires_ordered_markup(cls, result: RunResult):
192192
output = result.stdout.str()
193193
assert "test session starts" in output
194194
assert "\x1b[1m" in output
195-
pytest.skip("doing limited testing because lacking ordered markup")
195+
pytest.skip(
196+
"doing limited testing because lacking ordered markup on py35"
197+
)
196198

197199
return ColorMapping

0 commit comments

Comments
 (0)