Skip to content

Commit

Permalink
xdist fixes (microsoft#23791)
Browse files Browse the repository at this point in the history
- Don't use xdist when only running one test. this makes it slightly
faster when running one test, because instead of creating a single
worker it just runs the test in the same process
- fix microsoft#23816
- fix issue where the plugin was being registered multiple times

---------

Co-authored-by: detachhead <detachhead@users.noreply.github.com>
  • Loading branch information
2 people authored and eleanorjboyd committed Jul 30, 2024
1 parent 4eb9b6c commit 0c503c8
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions python_files/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

import pytest
from pluggy import Result

script_dir = pathlib.Path(__file__).parent.parent
sys.path.append(os.fspath(script_dir))
Expand Down Expand Up @@ -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)

0 comments on commit 0c503c8

Please sign in to comment.