Switch to new-style pluggy hook wrappers

Fix #11122.
This commit is contained in:
Ran Benita
2023-06-12 22:30:06 +03:00
parent 7008385253
commit b41acaea12
34 changed files with 334 additions and 275 deletions

View File

@@ -1,6 +1,7 @@
import dataclasses
import re
import sys
from typing import Generator
from typing import List
import pytest
@@ -21,11 +22,11 @@ if sys.gettrace():
sys.settrace(orig_trace)
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_collection_modifyitems(items):
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_collection_modifyitems(items) -> Generator[None, None, None]:
"""Prefer faster tests.
Use a hookwrapper to do this in the beginning, so e.g. --ff still works
Use a hook wrapper to do this in the beginning, so e.g. --ff still works
correctly.
"""
fast_items = []
@@ -62,7 +63,7 @@ def pytest_collection_modifyitems(items):
items[:] = fast_items + neutral_items + slow_items + slowest_items
yield
return (yield)
@pytest.fixture

View File

@@ -1040,13 +1040,13 @@ def test_log_set_path(pytester: Pytester) -> None:
"""
import os
import pytest
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_setup(item):
config = item.config
logging_plugin = config.pluginmanager.get_plugin("logging-plugin")
report_file = os.path.join({}, item._request.node.name)
logging_plugin.set_log_path(report_file)
yield
return (yield)
""".format(
repr(report_dir_base)
)

View File

@@ -827,11 +827,11 @@ class TestConftestCustomization:
textwrap.dedent(
"""\
import pytest
@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_pycollect_makemodule():
outcome = yield
mod = outcome.get_result()
mod = yield
mod.obj.hello = "world"
return mod
"""
),
encoding="utf-8",
@@ -855,14 +855,13 @@ class TestConftestCustomization:
textwrap.dedent(
"""\
import pytest
@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_pycollect_makeitem():
outcome = yield
if outcome.excinfo is None:
result = outcome.get_result()
if result:
for func in result:
func._some123 = "world"
result = yield
if result:
for func in result:
func._some123 = "world"
return result
"""
),
encoding="utf-8",

View File

@@ -334,12 +334,11 @@ class TestPrunetraceback:
pytester.makeconftest(
"""
import pytest
@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_make_collect_report():
outcome = yield
rep = outcome.get_result()
rep = yield
rep.headerlines += ["header1"]
outcome.force_result(rep)
return rep
"""
)
result = pytester.runpytest(p)

View File

@@ -1317,7 +1317,7 @@ def test_load_initial_conftest_last_ordering(_config_for_test):
hookimpls = [
(
hookimpl.function.__module__,
"wrapper" if hookimpl.hookwrapper else "nonwrapper",
"wrapper" if (hookimpl.wrapper or hookimpl.hookwrapper) else "nonwrapper",
)
for hookimpl in hc.get_hookimpls()
]

View File

@@ -806,12 +806,12 @@ class TestKeywordSelection:
pytester.makepyfile(
conftest="""
import pytest
@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_pycollect_makeitem(name):
outcome = yield
item = yield
if name == "TestClass":
item = outcome.get_result()
item.extra_keyword_matches.add("xxx")
return item
"""
)
reprec = pytester.inline_run(p.parent, "-s", "-k", keyword)

View File

@@ -85,8 +85,8 @@ def test_clean_up(pytester: Pytester) -> None:
# This is tough to test behaviorally because the cleanup really runs last.
# So the test make several implementation assumptions:
# - Cleanup is done in pytest_unconfigure().
# - Not a hookwrapper.
# So we can add a hookwrapper ourselves to test what it does.
# - Not a hook wrapper.
# So we can add a hook wrapper ourselves to test what it does.
pytester.makefile(".ini", pytest="[pytest]\npythonpath=I_SHALL_BE_REMOVED\n")
pytester.makepyfile(test_foo="""def test_foo(): pass""")
@@ -94,12 +94,14 @@ def test_clean_up(pytester: Pytester) -> None:
after: Optional[List[str]] = None
class Plugin:
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_unconfigure(self) -> Generator[None, None, None]:
nonlocal before, after
before = sys.path.copy()
yield
after = sys.path.copy()
try:
return (yield)
finally:
after = sys.path.copy()
result = pytester.runpytest_inprocess(plugins=[Plugin()])
assert result.ret == 0

View File

@@ -542,10 +542,10 @@ def test_runtest_in_module_ordering(pytester: Pytester) -> None:
@pytest.fixture
def mylist(self, request):
return request.function.mylist
@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_runtest_call(self, item):
try:
(yield).get_result()
yield
except ValueError:
pass
def test_hello1(self, mylist):
@@ -826,12 +826,12 @@ def test_unicode_in_longrepr(pytester: Pytester) -> None:
pytester.makeconftest(
"""\
import pytest
@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport():
outcome = yield
rep = outcome.get_result()
rep = yield
if rep.when == "call":
rep.longrepr = 'ä'
return rep
"""
)
pytester.makepyfile(

View File

@@ -725,12 +725,12 @@ class TestTerminalFunctional:
)
assert result.ret == 0
def test_deselected_with_hookwrapper(self, pytester: Pytester) -> None:
def test_deselected_with_hook_wrapper(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
import pytest
@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_collection_modifyitems(config, items):
yield
deselected = items.pop()

View File

@@ -952,7 +952,7 @@ def test_issue333_result_clearing(pytester: Pytester) -> None:
pytester.makeconftest(
"""
import pytest
@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_runtest_call(item):
yield
assert 0