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

Use modern approach to specify hook options #549

Merged
merged 2 commits into from
Sep 22, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Changelog
parallel = true
sigterm = true

* Use modern way to specify hook options to avoid deprecation warnings with pytest >=7.2.


3.0.0 (2021-10-04)
-------------------
Expand Down
7 changes: 0 additions & 7 deletions src/pytest_cov/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@
except ImportError:
from io import StringIO

import pytest

StringIO # pyflakes, this is for re-export


if hasattr(pytest, 'hookimpl'):
hookwrapper = pytest.hookimpl(hookwrapper=True)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hookimpl has been introduced in pytest 2.8, so this compatibility can be dropped.

else:
hookwrapper = pytest.mark.hookwrapper


class SessionWrapper:
def __init__(self, session):
self._session = session
Expand Down
10 changes: 5 additions & 5 deletions src/pytest_cov/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _prepare_cov_source(cov_source):
return None if True in cov_source else [path for path in cov_source if path is not True]


@pytest.mark.tryfirst
@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, args):
options = early_config.known_args_namespace
no_cov = options.no_cov_should_warn = False
Expand Down Expand Up @@ -253,23 +253,23 @@ def pytest_sessionstart(self, session):
if self.options.cov_context == 'test':
session.config.pluginmanager.register(TestContextPlugin(self.cov_controller.cov), '_cov_contexts')

@pytest.hookimpl(optionalhook=True)
def pytest_configure_node(self, node):
"""Delegate to our implementation.

Mark this hook as optional in case xdist is not installed.
"""
if not self._disabled:
self.cov_controller.configure_node(node)
pytest_configure_node.optionalhook = True

@pytest.hookimpl(optionalhook=True)
def pytest_testnodedown(self, node, error):
"""Delegate to our implementation.

Mark this hook as optional in case xdist is not installed.
"""
if not self._disabled:
self.cov_controller.testnodedown(node, error)
pytest_testnodedown.optionalhook = True

def _should_report(self):
return not (self.failed and self.options.no_cov_on_fail)
Expand All @@ -280,7 +280,7 @@ def _failed_cov_total(self):

# we need to wrap pytest_runtestloop. by the time pytest_sessionfinish
# runs, it's too late to set testsfailed
@compat.hookwrapper
@pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(self, session):
yield

Expand Down Expand Up @@ -356,7 +356,7 @@ def pytest_runtest_setup(self, item):
def pytest_runtest_teardown(self, item):
embed.cleanup()

@compat.hookwrapper
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(self, item):
if (item.get_closest_marker('no_cover')
or 'no_cover' in getattr(item, 'fixturenames', ())):
Expand Down
7 changes: 5 additions & 2 deletions tests/test_pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -1761,8 +1761,11 @@ def bad_init():
monkeypatch.setattr(sys, 'stderr', buff)
monkeypatch.setitem(os.environ, 'COV_CORE_SOURCE', 'foobar')
exec(payload)
assert buff.getvalue() == '''pytest-cov: Failed to setup subprocess coverage. Environ: {'COV_CORE_SOURCE': 'foobar'} Exception: SpecificError()
'''
expected = (
"pytest-cov: Failed to setup subprocess coverage. "
"Environ: {'COV_CORE_SOURCE': 'foobar'} Exception: SpecificError()\n"
)
assert buff.getvalue() == expected


def test_double_cov(testdir):
Expand Down