| 1 |
import importlib.machinery |
| 2 |
import importlib.util |
| 3 |
from pathlib import Path |
| 4 |
from typing import List, Optional |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
|
| 9 |
@pytest.hookimpl |
| 10 |
def pytest_collect_file(file_path: Path, parent: pytest.Collector) -> Optional[pytest.Collector]: |
| 11 |
"""Support for loading our check-* scripts as if they were modules.""" |
| 12 |
if file_path.name.startswith('check-'): |
| 13 |
# Pretend that test/verify/check-name is called like test.verify.check_name |
| 14 |
modname = 'test.verify.' + file_path.name.replace('-', '_') |
| 15 |
loader = importlib.machinery.SourceFileLoader(modname, str(file_path)) |
| 16 |
spec = importlib.util.spec_from_loader(loader.name, loader) |
| 17 |
assert spec is not None |
| 18 |
module = importlib.util.module_from_spec(spec) |
| 19 |
|
| 20 |
loader.exec_module(module) |
| 21 |
|
| 22 |
# Return the tree node with our module pre-loaded inside of it |
| 23 |
collector = pytest.Module.from_parent(parent, path=file_path) |
| 24 |
collector._obj = module |
| 25 |
return collector |
| 26 |
|
| 27 |
return None |
| 28 |
|
| 29 |
|
| 30 |
@pytest.hookimpl |
| 31 |
def pytest_collection_modifyitems(session: pytest.Session, items: List[pytest.Item]) -> None: |
| 32 |
"""Sorts the tests to place all non-destructive tests together""" |
| 33 |
assert isinstance(items, list) |
| 34 |
|
| 35 |
def is_nondestructive(item: pytest.Item) -> bool: |
| 36 |
attr = '_testlib__nondestructive' |
| 37 |
assert isinstance(item, pytest.Function) |
| 38 |
assert isinstance(item.parent, pytest.Class | pytest.Module) |
| 39 |
return getattr(item.obj, attr, getattr(item.parent.obj, attr, False)) |
| 40 |
|
| 41 |
# put the destructive tests last under the assumption that they're slower |
| 42 |
items.sort(key=is_nondestructive, reverse=True) |
| 43 |
|
| 44 |
|
| 45 |
@pytest.hookimpl |
| 46 |
def pytest_configure(config: pytest.Config) -> None: |
| 47 |
"""Tweaks test distribution for long-running tasks |
| 48 |
|
| 49 |
pytest-xdist sends large chunks of tasks to the workers to reduce |
| 50 |
latency, but since our tasks are long, this isn't helpful. It also |
| 51 |
means that we can end up with a large string of very slow tests on |
| 52 |
one worker. Disable it, if possible. |
| 53 |
|
| 54 |
https://github.com/pytest-dev/pytest-xdist/issues/855 |
| 55 |
""" |
| 56 |
try: |
| 57 |
# If parallel enabled and maxschedchunk not otherwise given... |
| 58 |
if config.option.numprocesses and config.option.maxschedchunk is None: |
| 59 |
config.option.maxschedchunk = 1 |
| 60 |
except AttributeError: |
| 61 |
pass # no pytest-xdist plugin installed, or plugin is too old |