import re
from pathlib import Path

import pytest

from .utils import cluster_fixture


def pytest_configure(config):
    config.addinivalue_line("markers", "slow: marks tests as slow")
    config.addinivalue_line("markers", "ledger: marks tests as ledger hardware test")
    config.addinivalue_line("markers", "grpc: marks grpc tests")
    config.addinivalue_line("markers", "upgrade: marks upgrade tests")


def pytest_addoption(parser):
    parser.addoption(
        "--supervisord-quiet",
        dest="supervisord-quiet",
        action="store_true",
        default=False,
        help="redirect supervisord's stdout to file",
    )


@pytest.fixture(scope="session")
def worker_index(worker_id):
    match = re.search(r"\d+", worker_id)
    return int(match[0]) if match is not None else 0


@pytest.fixture(scope="session")
def cluster(worker_index, pytestconfig, tmp_path_factory):
    "default cluster fixture"
    yield from cluster_fixture(
        Path(__file__).parent / "configs/default.yaml",
        worker_index,
        tmp_path_factory,
        quiet=pytestconfig.getoption("supervisord-quiet"),
    )


@pytest.fixture(scope="session")
def suspend_capture(pytestconfig):
    "used for pause in testing"

    class SuspendGuard:
        def __init__(self):
            self.capmanager = pytestconfig.pluginmanager.getplugin("capturemanager")

        def __enter__(self):
            self.capmanager.suspend_global_capture(in_=True)

        def __exit__(self, _1, _2, _3):
            self.capmanager.resume_global_capture()

    yield SuspendGuard()

import re
from pathlib import Path

import pytest

from .utils import cluster_fixture


def pytest_configure(config):
    config.addinivalue_line("markers", "slow: marks tests as slow")
    config.addinivalue_line("markers", "ledger: marks tests as ledger hardware test")
    config.addinivalue_line("markers", "grpc: marks grpc tests")
    config.addinivalue_line("markers", "upgrade: marks upgrade tests")


def pytest_addoption(parser):
    parser.addoption(
        "--supervisord-quiet",
        dest="supervisord-quiet",
        action="store_true",
        default=False,
        help="redirect supervisord's stdout to file",
    )


@pytest.fixture(scope="session")
def worker_index(worker_id):
    match = re.search(r"\d+", worker_id)
    return int(match[0]) if match is not None else 0


@pytest.fixture(scope="session")
def cluster(worker_index, pytestconfig, tmp_path_factory):
    "default cluster fixture"
    yield from cluster_fixture(
        Path(__file__).parent / "configs/default.yaml",
        worker_index,
        tmp_path_factory,
        quiet=pytestconfig.getoption("supervisord-quiet"),
    )


@pytest.fixture(scope="session")
def suspend_capture(pytestconfig):
    "used for pause in testing"

    class SuspendGuard:
        def __init__(self):
            self.capmanager = pytestconfig.pluginmanager.getplugin("capturemanager")

        def __enter__(self):
            self.capmanager.suspend_global_capture(in_=True)

        def __exit__(self, _1, _2, _3):
            self.capmanager.resume_global_capture()

    yield SuspendGuard()
