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

add backwards compatibility for pytest hook wrapper #23781

Merged
merged 12 commits into from
Jul 15, 2024
28 changes: 28 additions & 0 deletions python_files/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the MIT License.
import json
import os
import subprocess
import sys
from typing import Any, Dict, List, Optional

Expand All @@ -12,6 +13,32 @@
from . import expected_discovery_test_output, helpers # noqa: E402


@pytest.fixture
def pluggy_version_resource_setup_teardown():
# Setup to switch plugin versions
subprocess.run(["pip", "install", "pluggy==1.1.0"])
yield
# switch back to a newer version
subprocess.run(["pip", "install", "pluggy==1.2.0"])
print("Tearing down pluggy version")


def test_pluggy_old_version(pluggy_version_resource_setup_teardown):
print("Running test_example")
try:
helpers.runner(
[
os.fspath(helpers.TEST_DATA_PATH / "simple_pytest.py"),
"--collect-only",
]
)
assert True
except Exception as e:
print(f"Exception: {e}")
assert False
# If an error or assertion failure occurs above, the teardown will still execute.


def test_import_error():
"""Test pytest discovery on a file that has a pytest marker but does not import pytest.

Expand All @@ -23,6 +50,7 @@ def test_import_error():
Keyword arguments:
tmp_path -- pytest fixture that creates a temporary directory.
"""
subprocess.run(["pip", "install", "pluggy==1.1.0"])
file_path = helpers.TEST_DATA_PATH / "error_pytest_import.txt"
with helpers.text_to_python_file(file_path) as p:
actual: Optional[List[Dict[str, Any]]] = helpers.runner(["--collect-only", os.fspath(p)])
Expand Down
20 changes: 17 additions & 3 deletions python_files/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import pathlib
import sys
import traceback


import pluggy
import pytest

script_dir = pathlib.Path(__file__).parent.parent
Expand Down Expand Up @@ -892,8 +891,23 @@ def send_post_request(
)


def pluggy_version():
return pluggy.__version__


# pluggy version 1.1.0 and before use hookwrapper so convert this arg to wrapper to ensure compatibility for all versions <=1.1.0.
def compat_hookimpl(*args, **kwargs):
if pluggy_version() <= "1.1.0":
print(
eleanorjboyd marked this conversation as resolved.
Show resolved Hide resolved
f"pluggy version is, {pluggy_version()}, since this is <= 1.1.0, convert wrapper arg to hookwrapper if it exists for compatibility."
)
arg_exists = kwargs.pop("wrapper", False)
kwargs["hookwrapper"] = arg_exists
return pluggy.HookimplMarker("pytest")(*args, **kwargs)


class DeferPlugin:
@pytest.hookimpl(wrapper=True)
@compat_hookimpl(wrapper=True)
def pytest_xdist_auto_num_workers(self, config: pytest.Config) -> Generator[None, int, int]:
"""determine how many workers to use based on how many tests were selected in the test explorer"""
return min((yield), len(config.option.file_or_dir))
eleanorjboyd marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading