From d0b3f63be2339a8b9ca1e66e7a86a9c25871544a Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Sat, 26 Oct 2019 11:58:02 -0300 Subject: [PATCH] Fix comparison against installed PyTest version With the current code, the comparison `pytest.__version__ >= '3.8'` returns the wrong result when using PyTest 3.10, as `'3.10' < '3.8'` if we use Python's string comparison. Having `PYTEST_VERSION` as an integer tuple allows us to have simpler comparisons. --- src/pytest_cov/plugin.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index fb7a8f54..287daab0 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -12,6 +12,9 @@ from . import engine +PYTEST_VERSION = tuple(map(int, pytest.__version__.split('.')[:3])) + + class CoverageError(Exception): """Indicates that our coverage is too low""" @@ -260,7 +263,7 @@ def pytest_runtestloop(self, session): message = 'Failed to generate report: %s\n' % exc session.config.pluginmanager.getplugin("terminalreporter").write( 'WARNING: %s\n' % message, red=True, bold=True) - if pytest.__version__ >= '3.8': + if PYTEST_VERSION >= (3, 8): warnings.warn(pytest.PytestWarning(message)) else: session.config.warn(code='COV-2', message=message) @@ -274,7 +277,7 @@ def pytest_terminal_summary(self, terminalreporter): if self._disabled: message = 'Coverage disabled via --no-cov switch!' terminalreporter.write('WARNING: %s\n' % message, red=True, bold=True) - if pytest.__version__ >= '3.8': + if PYTEST_VERSION >= (3, 8): warnings.warn(pytest.PytestWarning(message)) else: terminalreporter.config.warn(code='COV-1', message=message)