From f5d4ae2e76fa68a1032329325a2af628d7cc4dcd Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 1 Jul 2024 13:28:26 -0700 Subject: [PATCH 01/11] add backwards compatibility for pytest hook wrapper --- python_files/vscode_pytest/__init__.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index a7b197ca26a5..74c6a8a2c3b9 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -7,8 +7,7 @@ import pathlib import sys import traceback - - +import pluggy import pytest script_dir = pathlib.Path(__file__).parent.parent @@ -892,8 +891,20 @@ def send_post_request( ) +def pluggy_version(): + return pluggy.__version__ + + +def compat_hookimpl(*args, **kwargs): + if pluggy_version() >= "1.1.0": + kwargs["wrapper"] = kwargs.pop("hookwrapper", False) + else: + kwargs["hookwrapper"] = kwargs.pop("wrapper", False) + 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)) From 66dc81c8c433eb9a574f5692af3dc8fa7adf7e18 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 10 Jul 2024 10:56:44 -0700 Subject: [PATCH 02/11] Add compatibility for pluggy version 1.1.0 in pytest --- .../tests/pytestadapter/test_discovery.py | 28 +++++++++++++++++++ python_files/vscode_pytest/__init__.py | 11 +++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/python_files/tests/pytestadapter/test_discovery.py b/python_files/tests/pytestadapter/test_discovery.py index f8c4890658c9..7cf98ba3e4a6 100644 --- a/python_files/tests/pytestadapter/test_discovery.py +++ b/python_files/tests/pytestadapter/test_discovery.py @@ -2,6 +2,7 @@ # Licensed under the MIT License. import json import os +import subprocess import sys from typing import Any, Dict, List, Optional @@ -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. @@ -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)]) diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index 74c6a8a2c3b9..cb2d0cbf00a7 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -895,11 +895,14 @@ 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": - kwargs["wrapper"] = kwargs.pop("hookwrapper", False) - else: - kwargs["hookwrapper"] = kwargs.pop("wrapper", False) + if pluggy_version() <= "1.1.0": + print( + 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) From 3e0bf38d2d8cdef00a99f4617150bdcf08de5954 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 10 Jul 2024 13:58:03 -0700 Subject: [PATCH 03/11] remove print --- python_files/vscode_pytest/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index cb2d0cbf00a7..25214148d186 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -898,9 +898,6 @@ def 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( - 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) From 44cb53f0500404e33617d6433b866d38455f1b53 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 10 Jul 2024 16:55:19 -0700 Subject: [PATCH 04/11] attempt w/o test --- .../tests/pytestadapter/test_discovery.py | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/python_files/tests/pytestadapter/test_discovery.py b/python_files/tests/pytestadapter/test_discovery.py index 7cf98ba3e4a6..f176ace70b53 100644 --- a/python_files/tests/pytestadapter/test_discovery.py +++ b/python_files/tests/pytestadapter/test_discovery.py @@ -13,30 +13,30 @@ 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. +# @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(): From 2eb5efeeeb6ab4179856fb3cb1d2b41e6084b84f Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 11 Jul 2024 11:19:28 -0700 Subject: [PATCH 05/11] attempt with hookwrapper instead --- python_files/vscode_pytest/__init__.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index 25214148d186..e9c8376044f0 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -891,20 +891,20 @@ def send_post_request( ) -def pluggy_version(): - return pluggy.__version__ +# 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": - arg_exists = kwargs.pop("wrapper", False) - kwargs["hookwrapper"] = arg_exists - return pluggy.HookimplMarker("pytest")(*args, **kwargs) +# # 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": +# arg_exists = kwargs.pop("wrapper", False) +# kwargs["hookwrapper"] = arg_exists +# return pluggy.HookimplMarker("pytest")(*args, **kwargs) class DeferPlugin: - @compat_hookimpl(wrapper=True) + @pytest.hookimpl(hookwrapper=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)) From f7e70ff26df87f3afb3a59bb66b0e4524fa3ab7f Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 11 Jul 2024 11:38:24 -0700 Subject: [PATCH 06/11] remove accidental subprocess call --- python_files/tests/pytestadapter/test_discovery.py | 13 ++++++++++--- python_files/vscode_pytest/__init__.py | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/python_files/tests/pytestadapter/test_discovery.py b/python_files/tests/pytestadapter/test_discovery.py index f176ace70b53..4cb9e6515545 100644 --- a/python_files/tests/pytestadapter/test_discovery.py +++ b/python_files/tests/pytestadapter/test_discovery.py @@ -16,10 +16,18 @@ # @pytest.fixture # def pluggy_version_resource_setup_teardown(): # # Setup to switch plugin versions -# subprocess.run(["pip", "install", "pluggy==1.1.0"]) +# try: +# subprocess.run(["pip", "install", "pluggy==1.1.0"]) +# except Exception as e: +# print(f"Exception while installing pluggy==1.1.0: {e}") +# assert False # yield # # switch back to a newer version -# subprocess.run(["pip", "install", "pluggy==1.2.0"]) +# try: +# subprocess.run(["pip", "install", "pluggy==1.2.0"]) +# except Exception as e: +# print(f"Exception while installing pluggy==1.2.0: {e}") +# assert False # print("Tearing down pluggy version") @@ -50,7 +58,6 @@ 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)]) diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index e9c8376044f0..5eb19686977c 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -7,7 +7,8 @@ import pathlib import sys import traceback -import pluggy + +# import pluggy import pytest script_dir = pathlib.Path(__file__).parent.parent From fcc0ea1731506dd07242ecbe065dc095462e1f77 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 11 Jul 2024 11:46:30 -0700 Subject: [PATCH 07/11] re-add test for old versioning --- .../tests/pytestadapter/test_discovery.py | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/python_files/tests/pytestadapter/test_discovery.py b/python_files/tests/pytestadapter/test_discovery.py index 4cb9e6515545..3e671d4c0d5f 100644 --- a/python_files/tests/pytestadapter/test_discovery.py +++ b/python_files/tests/pytestadapter/test_discovery.py @@ -13,38 +13,38 @@ from . import expected_discovery_test_output, helpers # noqa: E402 -# @pytest.fixture -# def pluggy_version_resource_setup_teardown(): -# # Setup to switch plugin versions -# try: -# subprocess.run(["pip", "install", "pluggy==1.1.0"]) -# except Exception as e: -# print(f"Exception while installing pluggy==1.1.0: {e}") -# assert False -# yield -# # switch back to a newer version -# try: -# subprocess.run(["pip", "install", "pluggy==1.2.0"]) -# except Exception as e: -# print(f"Exception while installing pluggy==1.2.0: {e}") -# assert False -# 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. +@pytest.fixture +def pluggy_version_resource_setup_teardown(): + # Setup to switch plugin versions + try: + subprocess.run(["pip", "install", "pluggy==1.1.0"], timeout=40) + except Exception as e: + print(f"Exception while installing pluggy==1.1.0: {e}") + assert False + yield + # switch back to a newer version + try: + subprocess.run(["pip", "install", "pluggy==1.2.0"], timeout=40) + except Exception as e: + print(f"Exception while installing pluggy==1.2.0: {e}") + assert False + 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(): From bb258776cfafdc24a6b8fe764aaa5a87aa1fff74 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 15 Jul 2024 13:07:29 -0700 Subject: [PATCH 08/11] remove failing test --- .../tests/pytestadapter/test_discovery.py | 34 ------------------- 1 file changed, 34 deletions(-) diff --git a/python_files/tests/pytestadapter/test_discovery.py b/python_files/tests/pytestadapter/test_discovery.py index 3e671d4c0d5f..3dcb8dc8d54b 100644 --- a/python_files/tests/pytestadapter/test_discovery.py +++ b/python_files/tests/pytestadapter/test_discovery.py @@ -13,40 +13,6 @@ from . import expected_discovery_test_output, helpers # noqa: E402 -@pytest.fixture -def pluggy_version_resource_setup_teardown(): - # Setup to switch plugin versions - try: - subprocess.run(["pip", "install", "pluggy==1.1.0"], timeout=40) - except Exception as e: - print(f"Exception while installing pluggy==1.1.0: {e}") - assert False - yield - # switch back to a newer version - try: - subprocess.run(["pip", "install", "pluggy==1.2.0"], timeout=40) - except Exception as e: - print(f"Exception while installing pluggy==1.2.0: {e}") - assert False - 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. From dfc00b9fe201051d26d976d5a0450497dbdaf5ab Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 15 Jul 2024 13:09:00 -0700 Subject: [PATCH 09/11] remove unneeded import --- python_files/tests/pytestadapter/test_discovery.py | 1 - python_files/vscode_pytest/__init__.py | 13 ------------- 2 files changed, 14 deletions(-) diff --git a/python_files/tests/pytestadapter/test_discovery.py b/python_files/tests/pytestadapter/test_discovery.py index 3dcb8dc8d54b..f8c4890658c9 100644 --- a/python_files/tests/pytestadapter/test_discovery.py +++ b/python_files/tests/pytestadapter/test_discovery.py @@ -2,7 +2,6 @@ # Licensed under the MIT License. import json import os -import subprocess import sys from typing import Any, Dict, List, Optional diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index 5eb19686977c..f94360283760 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -891,19 +891,6 @@ def send_post_request( file=sys.stderr, ) - -# 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": -# arg_exists = kwargs.pop("wrapper", False) -# kwargs["hookwrapper"] = arg_exists -# return pluggy.HookimplMarker("pytest")(*args, **kwargs) - - class DeferPlugin: @pytest.hookimpl(hookwrapper=True) def pytest_xdist_auto_num_workers(self, config: pytest.Config) -> Generator[None, int, int]: From c5df335e0f20c21f4f9362bc52e552c914fdcdf3 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 15 Jul 2024 13:09:34 -0700 Subject: [PATCH 10/11] remove import --- python_files/vscode_pytest/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index f94360283760..81422da85541 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -7,8 +7,6 @@ import pathlib import sys import traceback - -# import pluggy import pytest script_dir = pathlib.Path(__file__).parent.parent @@ -891,6 +889,7 @@ def send_post_request( file=sys.stderr, ) + class DeferPlugin: @pytest.hookimpl(hookwrapper=True) def pytest_xdist_auto_num_workers(self, config: pytest.Config) -> Generator[None, int, int]: From a517713a029d4c6246d3ecf8319fce4723e4e877 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 15 Jul 2024 13:15:32 -0700 Subject: [PATCH 11/11] org imports --- python_files/vscode_pytest/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index 92cbbd2acb81..656846513c93 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -17,6 +17,7 @@ TypedDict, Union, ) + import pytest script_dir = pathlib.Path(__file__).parent.parent