|
| 1 | +"""Pytest plugin that defines options and fixtures for client exceptions.""" |
| 2 | + |
| 3 | +from typing import List |
| 4 | + |
| 5 | +import pytest |
| 6 | +from hive.client import ClientType |
| 7 | + |
| 8 | +from ethereum_test_exceptions import ExceptionMapper |
| 9 | +from ethereum_test_fixtures import ( |
| 10 | + BlockchainFixtureCommon, |
| 11 | +) |
| 12 | + |
| 13 | +from .helpers.exceptions import EXCEPTION_MAPPERS |
| 14 | + |
| 15 | + |
| 16 | +def pytest_addoption(parser): |
| 17 | + """Hive simulator specific consume command line options.""" |
| 18 | + consume_group = parser.getgroup( |
| 19 | + "consume", "Arguments related to consuming fixtures via a client" |
| 20 | + ) |
| 21 | + consume_group.addoption( |
| 22 | + "--disable-strict-exception-matching", |
| 23 | + action="store", |
| 24 | + dest="disable_strict_exception_matching", |
| 25 | + default="", |
| 26 | + help=( |
| 27 | + "Comma-separated list of client names and/or forks which should NOT use strict " |
| 28 | + "exception matching." |
| 29 | + ), |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="session") |
| 34 | +def client_exception_mapper_cache(): |
| 35 | + """Cache for exception mappers by client type.""" |
| 36 | + return {} |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture(scope="function") |
| 40 | +def client_exception_mapper( |
| 41 | + client_type: ClientType, client_exception_mapper_cache |
| 42 | +) -> ExceptionMapper | None: |
| 43 | + """Return the exception mapper for the client type, with caching.""" |
| 44 | + if client_type.name not in client_exception_mapper_cache: |
| 45 | + for client in EXCEPTION_MAPPERS: |
| 46 | + if client in client_type.name: |
| 47 | + client_exception_mapper_cache[client_type.name] = EXCEPTION_MAPPERS[client] |
| 48 | + break |
| 49 | + else: |
| 50 | + client_exception_mapper_cache[client_type.name] = None |
| 51 | + |
| 52 | + return client_exception_mapper_cache[client_type.name] |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(scope="session") |
| 56 | +def disable_strict_exception_matching(request: pytest.FixtureRequest) -> List[str]: |
| 57 | + """Return the list of clients or forks that should NOT use strict exception matching.""" |
| 58 | + config_string = request.config.getoption("disable_strict_exception_matching") |
| 59 | + return config_string.split(",") if config_string else [] |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(scope="function") |
| 63 | +def client_strict_exception_matching( |
| 64 | + client_type: ClientType, |
| 65 | + disable_strict_exception_matching: List[str], |
| 66 | +) -> bool: |
| 67 | + """Return True if the client type should use strict exception matching.""" |
| 68 | + return not any( |
| 69 | + client.lower() in client_type.name.lower() for client in disable_strict_exception_matching |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture(scope="function") |
| 74 | +def fork_strict_exception_matching( |
| 75 | + fixture: BlockchainFixtureCommon, |
| 76 | + disable_strict_exception_matching: List[str], |
| 77 | +) -> bool: |
| 78 | + """Return True if the fork should use strict exception matching.""" |
| 79 | + # NOTE: `in` makes it easier for transition forks ("Prague" in "CancunToPragueAtTime15k") |
| 80 | + return not any( |
| 81 | + s.lower() in str(fixture.fork).lower() for s in disable_strict_exception_matching |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture(scope="function") |
| 86 | +def strict_exception_matching( |
| 87 | + client_strict_exception_matching: bool, |
| 88 | + fork_strict_exception_matching: bool, |
| 89 | +) -> bool: |
| 90 | + """Return True if the test should use strict exception matching.""" |
| 91 | + return client_strict_exception_matching and fork_strict_exception_matching |
0 commit comments