Skip to content

Commit

Permalink
Merge pull request #197 from nicoddemus/issue-194
Browse files Browse the repository at this point in the history
Fix regression with pytest 5.4 and line number report for doctests
  • Loading branch information
Teemu authored Jul 6, 2020
2 parents 00b8100 + e5eacfd commit 1e2fa15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
32 changes: 22 additions & 10 deletions pytest_sugar.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
]


def flatten(l):
for x in l:
def flatten(seq):
for x in seq:
if isinstance(x, (list, tuple)):
for y in flatten(x):
yield y
Expand Down Expand Up @@ -238,6 +238,7 @@ def pytest_collectreport(self, report):
self.print_failure(report)

def pytest_sessionstart(self, session):
self._session = session
self._sessionstarttime = py.std.time.time()
verinfo = ".".join(map(str, sys.version_info[:3]))
self.write_line(
Expand Down Expand Up @@ -530,14 +531,7 @@ def summary_stats(self):
else:
path = os.path.dirname(report.location[0])
name = os.path.basename(report.location[0])
# Doctest failure reports have lineno=None at least up to
# pytest==3.0.7, but it is available via longrepr object.
try:
lineno = report.longrepr.reprlocation.lineno
except AttributeError:
lineno = report.location[1]
if lineno is not None:
lineno += 1
lineno = self._get_lineno_from_report(report)
crashline = '%s%s%s:%s %s' % (
colored(path, THEME['path']),
'/' if path else '',
Expand Down Expand Up @@ -594,6 +588,24 @@ def _get_decoded_crashline(self, report):

return crashline

def _get_lineno_from_report(self, report):
# Doctest failures in pytest>3.10 are stored in
# reprlocation_lines, a list of (ReprFileLocation, lines)
try:
location, lines = report.longrepr.reprlocation_lines[0]
return location.lineno
except AttributeError:
pass
# Doctest failure reports have lineno=None at least up to
# pytest==3.0.7, but it is available via longrepr object.
try:
return report.longrepr.reprlocation.lineno
except AttributeError:
lineno = report.location[1]
if lineno is not None:
lineno += 1
return lineno

def summary_failures(self):
# Prevent failure summary from being shown since we already
# show the failure instantly after failure has occurred.
Expand Down
5 changes: 0 additions & 5 deletions test_sugar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import pytest
import re
from distutils.version import LooseVersion
from pytest_sugar import strip_colors

pytest_plugins = "pytester"
Expand Down Expand Up @@ -551,10 +550,6 @@ def doctest(self):

assert result.ret == 1, result.stderr.str()

@pytest.mark.skipif(
LooseVersion(pytest.__version__) >= LooseVersion('3.5'),
reason='Temporarily skipping until #134'
)
def test_doctest_lineno(self, testdir):
""" Test location reported for doctest-modules """

Expand Down

0 comments on commit 1e2fa15

Please sign in to comment.