Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest: add latest fork parameter and exit gracefully upon an invalid fork #6

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ console_output_style = "count"
minversion = "7.0"
testpaths = ["fillers/example", "fillers/eips/eip3651.py", "fillers/eips/eip3855.py", "fillers/vm/chain_id.py"]
python_files = "*.py"
addopts = "-p pytest_plugins.latest_fork"
markers = [
"state_test: test cases that implement a single state transition test",
"blockchain_test: test cases that implement block transition tests",
]
]
2 changes: 2 additions & 0 deletions src/ethereum_test_forks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from .forks.upcoming import Cancun
from .helpers import (
InvalidForkError,
fork_only,
forks_from,
forks_from_until,
Expand All @@ -44,6 +45,7 @@
"Frontier",
"GrayGlacier",
"Homestead",
"InvalidForkError",
"Istanbul",
"London",
"Merge",
Expand Down
7 changes: 6 additions & 1 deletion src/ethereum_test_forks/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def __init__(self):
latest_fork_resolver = LatestForkResolver()


class InvalidForkError(Exception):
def __init__(self, message):
super().__init__(message)


def set_latest_fork(fork: Fork) -> None:
"""
Sets the latest fork
Expand All @@ -37,7 +42,7 @@ def set_latest_fork_by_name(fork_name: str) -> None:
elif fork_name in upcoming.__dict__:
set_latest_fork(upcoming.__dict__[fork_name])
else:
raise Exception(f'fork "{fork_name}" not found')
raise InvalidForkError(f'fork "{fork_name}" not found')


def get_parent_fork(fork: Fork) -> Fork:
Expand Down
62 changes: 62 additions & 0 deletions src/pytest_plugins/latest_fork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest

from ethereum_test_forks import (
InvalidForkError,
set_latest_fork_by_name,
)


def pytest_addoption(parser):
"""
Adds command-line options to pytest.
"""
group = parser.getgroup("forks", "Arguments defining fork configuration")
group.addoption(
"--latest-fork",
action="store",
dest="latest_fork",
default=None,
help="Latest fork used to fill tests",
)


def pytest_configure(config):
"""
Check parameters and make session-wide configuration changes, such as
setting the latest fork.
"""
latest_fork = config.getoption("latest_fork")
if latest_fork is not None:
try:
set_latest_fork_by_name(latest_fork)
except InvalidForkError as e:
pytest.exit(f"Error applying --latest-fork={latest_fork}: {e}.")
except Exception as e:
raise e
return None


@pytest.hookimpl(trylast=True)
def pytest_report_header(config, start_path):
"""A pytest hook called to obtain the report header."""
bold = "\033[1m"
warning = "\033[93m"
reset = "\033[39;49m"
if config.getoption("latest_fork") is None:
header = [
(
bold
+ warning
+ "Only executing fillers with stable/deployed forks: "
"Specify an upcoming fork via --latest-fork=fork to "
"run experimental fillers." + reset
)
]
else:
header = [
(
bold + "Executing fillers up to and including "
f"{config.getoption('latest_fork')}." + reset
),
]
return header