Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression with pytest 5.4 and line number report for doctests #197

Merged
merged 3 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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