|  | 
|  | 1 | +"""Pytest configuration for state tests.""" | 
|  | 2 | + | 
|  | 3 | +from pathlib import Path | 
|  | 4 | + | 
|  | 5 | +import pytest | 
|  | 6 | + | 
|  | 7 | +DEFAULT_BENCHMARK_FORK = "Prague" | 
|  | 8 | + | 
|  | 9 | + | 
|  | 10 | +def pytest_generate_tests(metafunc): | 
|  | 11 | +    """Add default valid_from marker to state tests without explicit fork specification.""" | 
|  | 12 | +    state_dir = Path(__file__).parent | 
|  | 13 | +    test_file_path = Path(metafunc.definition.fspath) | 
|  | 14 | + | 
|  | 15 | +    if state_dir in test_file_path.parents: | 
|  | 16 | +        has_valid_from = any( | 
|  | 17 | +            marker.name == "valid_from" for marker in metafunc.definition.iter_markers() | 
|  | 18 | +        ) | 
|  | 19 | +        if not has_valid_from: | 
|  | 20 | +            metafunc.definition.add_marker(pytest.mark.valid_from(DEFAULT_BENCHMARK_FORK)) | 
|  | 21 | + | 
|  | 22 | + | 
|  | 23 | +def pytest_collection_modifyitems(config, items): | 
|  | 24 | +    """Manage state test markers and filtering.""" | 
|  | 25 | +    state_dir = Path(__file__).parent | 
|  | 26 | +    gen_docs = config.getoption("--gen-docs", default=False) | 
|  | 27 | + | 
|  | 28 | +    if gen_docs: | 
|  | 29 | +        _add_state_markers_for_docs(items, state_dir) | 
|  | 30 | +        return | 
|  | 31 | + | 
|  | 32 | +    marker_expr = config.getoption("-m", default="") | 
|  | 33 | + | 
|  | 34 | +    items_to_remove = [] | 
|  | 35 | + | 
|  | 36 | +    for i, item in enumerate(items): | 
|  | 37 | +        item_path = Path(item.fspath) | 
|  | 38 | +        is_in_state_dir = state_dir in item_path.parents | 
|  | 39 | + | 
|  | 40 | +        # Add state marker to tests in state directory that don't have it | 
|  | 41 | +        if is_in_state_dir and not item.get_closest_marker("state"): | 
|  | 42 | +            item.add_marker(pytest.mark.state) | 
|  | 43 | + | 
|  | 44 | +        has_state_marker = item.get_closest_marker("state") | 
|  | 45 | + | 
|  | 46 | +        run_state = marker_expr and ("state" in marker_expr) and ("not state" not in marker_expr) | 
|  | 47 | + | 
|  | 48 | +        # When not running state tests, remove all state tests | 
|  | 49 | +        if not run_state and has_state_marker: | 
|  | 50 | +            items_to_remove.append(i) | 
|  | 51 | + | 
|  | 52 | +    for i in reversed(items_to_remove): | 
|  | 53 | +        items.pop(i) | 
|  | 54 | + | 
|  | 55 | + | 
|  | 56 | +def _add_state_markers_for_docs(items, state_dir): | 
|  | 57 | +    """Add state markers for documentation generation.""" | 
|  | 58 | +    for item in items: | 
|  | 59 | +        item_path = Path(item.fspath) | 
|  | 60 | +        if state_dir in item_path.parents and not item.get_closest_marker("state"): | 
|  | 61 | +            item.add_marker(pytest.mark.state) | 
0 commit comments