diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index 656846513c93..2764d1c89782 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -19,6 +19,7 @@ ) import pytest +from pluggy import Result script_dir = pathlib.Path(__file__).parent.parent sys.path.append(os.fspath(script_dir)) @@ -889,11 +890,25 @@ def send_post_request( class DeferPlugin: @pytest.hookimpl(hookwrapper=True) - def pytest_xdist_auto_num_workers(self, config: pytest.Config) -> Generator[None, int, int]: + def pytest_xdist_auto_num_workers( + self, config: pytest.Config + ) -> Generator[None, Result[int], None]: """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)) + outcome = yield + result = min(outcome.get_result(), len(config.option.file_or_dir)) + if result == 1: + result = 0 + outcome.force_result(result) def pytest_plugin_registered(plugin: object, manager: pytest.PytestPluginManager): - if manager.hasplugin("xdist") and not isinstance(plugin, DeferPlugin): - manager.register(DeferPlugin()) + plugin_name = "vscode_xdist" + if ( + # only register the plugin if xdist is enabled: + manager.hasplugin("xdist") + # prevent infinite recursion: + and not isinstance(plugin, DeferPlugin) + # prevent this plugin from being registered multiple times: + and not manager.hasplugin(plugin_name) + ): + manager.register(DeferPlugin(), name=plugin_name)