Skip to content

Commit 66dc81c

Browse files
committed
Add compatibility for pluggy version 1.1.0 in pytest
1 parent f5d4ae2 commit 66dc81c

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

python_files/tests/pytestadapter/test_discovery.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Licensed under the MIT License.
33
import json
44
import os
5+
import subprocess
56
import sys
67
from typing import Any, Dict, List, Optional
78

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

1415

16+
@pytest.fixture
17+
def pluggy_version_resource_setup_teardown():
18+
# Setup to switch plugin versions
19+
subprocess.run(["pip", "install", "pluggy==1.1.0"])
20+
yield
21+
# switch back to a newer version
22+
subprocess.run(["pip", "install", "pluggy==1.2.0"])
23+
print("Tearing down pluggy version")
24+
25+
26+
def test_pluggy_old_version(pluggy_version_resource_setup_teardown):
27+
print("Running test_example")
28+
try:
29+
helpers.runner(
30+
[
31+
os.fspath(helpers.TEST_DATA_PATH / "simple_pytest.py"),
32+
"--collect-only",
33+
]
34+
)
35+
assert True
36+
except Exception as e:
37+
print(f"Exception: {e}")
38+
assert False
39+
# If an error or assertion failure occurs above, the teardown will still execute.
40+
41+
1542
def test_import_error():
1643
"""Test pytest discovery on a file that has a pytest marker but does not import pytest.
1744
@@ -23,6 +50,7 @@ def test_import_error():
2350
Keyword arguments:
2451
tmp_path -- pytest fixture that creates a temporary directory.
2552
"""
53+
subprocess.run(["pip", "install", "pluggy==1.1.0"])
2654
file_path = helpers.TEST_DATA_PATH / "error_pytest_import.txt"
2755
with helpers.text_to_python_file(file_path) as p:
2856
actual: Optional[List[Dict[str, Any]]] = helpers.runner(["--collect-only", os.fspath(p)])

python_files/vscode_pytest/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,14 @@ def pluggy_version():
895895
return pluggy.__version__
896896

897897

898+
# pluggy version 1.1.0 and before use hookwrapper so convert this arg to wrapper to ensure compatibility for all versions <=1.1.0.
898899
def compat_hookimpl(*args, **kwargs):
899-
if pluggy_version() >= "1.1.0":
900-
kwargs["wrapper"] = kwargs.pop("hookwrapper", False)
901-
else:
902-
kwargs["hookwrapper"] = kwargs.pop("wrapper", False)
900+
if pluggy_version() <= "1.1.0":
901+
print(
902+
f"pluggy version is, {pluggy_version()}, since this is <= 1.1.0, convert wrapper arg to hookwrapper if it exists for compatibility."
903+
)
904+
arg_exists = kwargs.pop("wrapper", False)
905+
kwargs["hookwrapper"] = arg_exists
903906
return pluggy.HookimplMarker("pytest")(*args, **kwargs)
904907

905908

0 commit comments

Comments
 (0)