File: conftest.py

package info (click to toggle)
pytest-xdist 3.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 684 kB
  • sloc: python: 5,497; makefile: 7; sh: 5
file content (68 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from __future__ import annotations

from collections.abc import Generator
import shutil
from typing import Callable

import execnet
import pytest


pytest_plugins = "pytester"


@pytest.fixture(autouse=True)
def _divert_atexit(monkeypatch: pytest.MonkeyPatch) -> Generator[None]:
    import atexit

    finalizers = []

    def fake_register(
        func: Callable[..., object], *args: object, **kwargs: object
    ) -> None:
        finalizers.append((func, args, kwargs))

    monkeypatch.setattr(atexit, "register", fake_register)

    yield

    while finalizers:
        func, args, kwargs = finalizers.pop()
        func(*args, **kwargs)


def pytest_addoption(parser: pytest.Parser) -> None:
    parser.addoption(
        "--gx",
        action="append",
        dest="gspecs",
        help="add a global test environment, XSpec-syntax. ",
    )


@pytest.fixture
def specssh(request: pytest.FixtureRequest) -> str:
    return getspecssh(request.config)


# configuration information for tests
def getgspecs(config: pytest.Config) -> list[execnet.XSpec]:
    return [execnet.XSpec(spec) for spec in config.getvalueorskip("gspecs")]


def getspecssh(config: pytest.Config) -> str:
    xspecs = getgspecs(config)
    for spec in xspecs:
        if spec.ssh:
            if not shutil.which("ssh"):
                pytest.skip("command not found: ssh")
            return str(spec)
    pytest.skip("need '--gx ssh=...'")


def getsocketspec(config: pytest.Config) -> execnet.XSpec:
    xspecs = getgspecs(config)
    for spec in xspecs:
        if spec.socket:
            return spec
    pytest.skip("need '--gx socket=...'")