From b5460ecc815ad6b7e4fa89ddd1f8cba4d73e0874 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 31 May 2020 14:14:50 -0300 Subject: [PATCH 1/3] Fix compatibility with pytest 5.4 Fix #194 Close pytest-dev/pytest#7287 --- pytest_sugar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pytest_sugar.py b/pytest_sugar.py index 328ac75..b6b2aa3 100644 --- a/pytest_sugar.py +++ b/pytest_sugar.py @@ -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( From 3e9d60395a06e7716c036c17c100ab4fe604c2ea Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 31 May 2020 14:40:22 -0300 Subject: [PATCH 2/3] Fix line number report for doctests in pytest>=3.10 Fix #134 --- pytest_sugar.py | 27 +++++++++++++++++++-------- test_sugar.py | 4 ---- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/pytest_sugar.py b/pytest_sugar.py index b6b2aa3..29e240f 100644 --- a/pytest_sugar.py +++ b/pytest_sugar.py @@ -531,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 '', @@ -595,6 +588,24 @@ def _get_decoded_crashline(self, report): return crashline + def _get_lineno_from_report(self, report): + # Doctest failure reports changed the attribute where longrepr were stored in pytest>3.10 + # to 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. diff --git a/test_sugar.py b/test_sugar.py index 626fa10..32cb8c3 100644 --- a/test_sugar.py +++ b/test_sugar.py @@ -551,10 +551,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 """ From e5eacfd3f1a1e862e7537a5e786ac3ca68fab655 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 31 May 2020 15:06:50 -0300 Subject: [PATCH 3/3] Fix QA --- pytest_sugar.py | 8 ++++---- test_sugar.py | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pytest_sugar.py b/pytest_sugar.py index 29e240f..faca615 100644 --- a/pytest_sugar.py +++ b/pytest_sugar.py @@ -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 @@ -589,8 +589,8 @@ def _get_decoded_crashline(self, report): return crashline def _get_lineno_from_report(self, report): - # Doctest failure reports changed the attribute where longrepr were stored in pytest>3.10 - # to reprlocation_lines, a list of (ReprFileLocation, lines) + # 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 diff --git a/test_sugar.py b/test_sugar.py index 32cb8c3..28e9f97 100644 --- a/test_sugar.py +++ b/test_sugar.py @@ -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"