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

✨ feat(CI): CI for Gentest #1015

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/cli/gentest/source_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import jinja2

from config import AppConfig

from .test_context_providers import Provider

template_loader = jinja2.PackageLoader("cli.gentest")
Expand Down Expand Up @@ -74,19 +76,22 @@ def format_code(code: str) -> str:
formatter_path = Path(sys.prefix) / "bin" / "ruff"

# Call ruff to format the file
config_path = Path(sys.prefix).parent / "pyproject.toml"

subprocess.run(
[
str(formatter_path),
"format",
str(input_file_path),
"--quiet",
"--config",
str(config_path),
],
check=True,
)
config_path = AppConfig().ROOT_DIR / "pyproject.toml"

try:
subprocess.run(
[
str(formatter_path),
"format",
str(input_file_path),
"--quiet",
"--config",
str(config_path),
],
check=True,
)
except subprocess.CalledProcessError as e:
raise Exception(f"Error formatting code using formatter '{formatter_path}'") from e

# Return the formatted source code
return input_file_path.read_text()
1 change: 1 addition & 0 deletions src/cli/gentest/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test cases for the `generate` CLI."""
101 changes: 101 additions & 0 deletions src/cli/gentest/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""Tests for the gentest CLI command."""

from click.testing import CliRunner

from cli.gentest.cli import generate
from cli.gentest.test_context_providers import StateTestProvider
from cli.pytest_commands.fill import fill
from ethereum_test_base_types import Account
from ethereum_test_tools import Environment, Storage, Transaction


def test_generate_success(tmp_path, monkeypatch):
"""Test the generate command with a successful scenario."""
## Arrange ##

# This test is run in a CI environment, where connection to a node could be
# unreliable. Therefore, we mock the RPC request to avoid any network issues.
# This is done by patching the `get_context` method of the `StateTestProvider`.
runner = CliRunner()
transaction_hash = "0xa41f343be7a150b740e5c939fa4d89f3a2850dbe21715df96b612fc20d1906be"
output_file = str(tmp_path / "gentest.py")

def get_mock_context(self: StateTestProvider) -> dict:
return {
"environment": Environment(
fee_recipient="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
gas_limit=9916577,
number=9974504,
timestamp=1588257377,
prev_randao=None,
difficulty=2315196811272822,
base_fee_per_gas=None,
excess_blob_gas=None,
target_blobs_per_block=None,
parent_difficulty=None,
parent_timestamp=None,
parent_base_fee_per_gas=None,
parent_gas_used=None,
parent_gas_limit=None,
blob_gas_used=None,
parent_ommers_hash="0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
parent_blob_gas_used=None,
parent_excess_blob_gas=None,
parent_beacon_block_root=None,
block_hashes={},
ommers=[],
withdrawals=None,
extra_data=b"\x00",
parent_hash=None,
),
"pre_state": {
"0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c": Account(
nonce=6038603, balance=23760714652307793035, code=b"", storage=Storage(root={})
),
"0x8a4a4d396a06cba2a7a4a73245991de40cdec289": Account(
nonce=2, balance=816540000000000000, code=b"", storage=Storage(root={})
),
"0xc6d96786477f82491bfead8f00b8294688f77abc": Account(
nonce=25, balance=29020266497911578313, code=b"", storage=Storage(root={})
),
},
"transaction": Transaction(
ty=0,
chain_id=1,
nonce=2,
gas_price=10000000000,
max_priority_fee_per_gas=None,
max_fee_per_gas=None,
gas_limit=21000,
to="0xc6d96786477f82491bfead8f00b8294688f77abc",
value=668250000000000000,
data=b"",
access_list=None,
max_fee_per_blob_gas=None,
blob_versioned_hashes=None,
v=38,
r=57233334052658009540326312124836763247359579695589124499839562829147086216092,
s=49687643984819828983661675232336138386174947240467726918882054280625462464348,
sender="0x8a4a4d396a06cba2a7a4a73245991de40cdec289",
authorization_list=None,
secret_key=None,
error=None,
protected=True,
rlp_override=None,
wrapped_blob_transaction=False,
blobs=None,
blob_kzg_commitments=None,
blob_kzg_proofs=None,
),
"tx_hash": transaction_hash,
}

monkeypatch.setattr(StateTestProvider, "get_context", get_mock_context)

## Genenrate ##
gentest_result = runner.invoke(generate, [transaction_hash, output_file])
assert gentest_result.exit_code == 0

## Fill ##
fill_result = runner.invoke(fill, ["-c", "pytest.ini", output_file])
assert fill_result.exit_code == 0
3 changes: 3 additions & 0 deletions src/config/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ class AppConfig(BaseModel):

DEFAULT_EVM_LOGS_DIR: Path = DEFAULT_LOGS_DIR / "evm"
"""The default directory where EVM log files are stored."""

ROOT_DIR: Path = Path(__file__).resolve().parents[2]
"""The root directory of the project."""
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ description = Run library and framework unit tests (pytest)
setenv =
# Use custom EELS_RESOLUTIONS_FILE if it is set via the environment (eg, in CI)
EELS_RESOLUTIONS_FILE = {env:EELS_RESOLUTIONS_FILE:}
extras = test
extras =
test
lint # Required `gentest` for formatting tests
commands_pre = solc-select use {[testenv]solc_version} --always-install
commands =
pytest -c ./pytest-framework.ini -n auto -m "not run_in_serial"
Expand Down
Loading