Skip to content

Commit 8a1cfdb

Browse files
authored
✨ feat(CI): CI for Gentest (#1015)
1 parent bdce2dc commit 8a1cfdb

File tree

5 files changed

+126
-14
lines changed

5 files changed

+126
-14
lines changed

src/cli/gentest/source_code_generator.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import jinja2
1313

14+
from config import AppConfig
15+
1416
from .test_context_providers import Provider
1517

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

7678
# Call ruff to format the file
77-
config_path = Path(sys.prefix).parent / "pyproject.toml"
78-
79-
subprocess.run(
80-
[
81-
str(formatter_path),
82-
"format",
83-
str(input_file_path),
84-
"--quiet",
85-
"--config",
86-
str(config_path),
87-
],
88-
check=True,
89-
)
79+
config_path = AppConfig().ROOT_DIR / "pyproject.toml"
80+
81+
try:
82+
subprocess.run(
83+
[
84+
str(formatter_path),
85+
"format",
86+
str(input_file_path),
87+
"--quiet",
88+
"--config",
89+
str(config_path),
90+
],
91+
check=True,
92+
)
93+
except subprocess.CalledProcessError as e:
94+
raise Exception(f"Error formatting code using formatter '{formatter_path}'") from e
9095

9196
# Return the formatted source code
9297
return input_file_path.read_text()

src/cli/gentest/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test cases for the `generate` CLI."""

src/cli/gentest/tests/test_cli.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""Tests for the gentest CLI command."""
2+
3+
from click.testing import CliRunner
4+
5+
from cli.gentest.cli import generate
6+
from cli.gentest.test_context_providers import StateTestProvider
7+
from cli.pytest_commands.fill import fill
8+
from ethereum_test_base_types import Account
9+
from ethereum_test_tools import Environment, Storage, Transaction
10+
11+
12+
def test_generate_success(tmp_path, monkeypatch):
13+
"""Test the generate command with a successful scenario."""
14+
## Arrange ##
15+
16+
# This test is run in a CI environment, where connection to a node could be
17+
# unreliable. Therefore, we mock the RPC request to avoid any network issues.
18+
# This is done by patching the `get_context` method of the `StateTestProvider`.
19+
runner = CliRunner()
20+
transaction_hash = "0xa41f343be7a150b740e5c939fa4d89f3a2850dbe21715df96b612fc20d1906be"
21+
output_file = str(tmp_path / "gentest.py")
22+
23+
def get_mock_context(self: StateTestProvider) -> dict:
24+
return {
25+
"environment": Environment(
26+
fee_recipient="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
27+
gas_limit=9916577,
28+
number=9974504,
29+
timestamp=1588257377,
30+
prev_randao=None,
31+
difficulty=2315196811272822,
32+
base_fee_per_gas=None,
33+
excess_blob_gas=None,
34+
target_blobs_per_block=None,
35+
parent_difficulty=None,
36+
parent_timestamp=None,
37+
parent_base_fee_per_gas=None,
38+
parent_gas_used=None,
39+
parent_gas_limit=None,
40+
blob_gas_used=None,
41+
parent_ommers_hash="0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
42+
parent_blob_gas_used=None,
43+
parent_excess_blob_gas=None,
44+
parent_beacon_block_root=None,
45+
block_hashes={},
46+
ommers=[],
47+
withdrawals=None,
48+
extra_data=b"\x00",
49+
parent_hash=None,
50+
),
51+
"pre_state": {
52+
"0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c": Account(
53+
nonce=6038603, balance=23760714652307793035, code=b"", storage=Storage(root={})
54+
),
55+
"0x8a4a4d396a06cba2a7a4a73245991de40cdec289": Account(
56+
nonce=2, balance=816540000000000000, code=b"", storage=Storage(root={})
57+
),
58+
"0xc6d96786477f82491bfead8f00b8294688f77abc": Account(
59+
nonce=25, balance=29020266497911578313, code=b"", storage=Storage(root={})
60+
),
61+
},
62+
"transaction": Transaction(
63+
ty=0,
64+
chain_id=1,
65+
nonce=2,
66+
gas_price=10000000000,
67+
max_priority_fee_per_gas=None,
68+
max_fee_per_gas=None,
69+
gas_limit=21000,
70+
to="0xc6d96786477f82491bfead8f00b8294688f77abc",
71+
value=668250000000000000,
72+
data=b"",
73+
access_list=None,
74+
max_fee_per_blob_gas=None,
75+
blob_versioned_hashes=None,
76+
v=38,
77+
r=57233334052658009540326312124836763247359579695589124499839562829147086216092,
78+
s=49687643984819828983661675232336138386174947240467726918882054280625462464348,
79+
sender="0x8a4a4d396a06cba2a7a4a73245991de40cdec289",
80+
authorization_list=None,
81+
secret_key=None,
82+
error=None,
83+
protected=True,
84+
rlp_override=None,
85+
wrapped_blob_transaction=False,
86+
blobs=None,
87+
blob_kzg_commitments=None,
88+
blob_kzg_proofs=None,
89+
),
90+
"tx_hash": transaction_hash,
91+
}
92+
93+
monkeypatch.setattr(StateTestProvider, "get_context", get_mock_context)
94+
95+
## Genenrate ##
96+
gentest_result = runner.invoke(generate, [transaction_hash, output_file])
97+
assert gentest_result.exit_code == 0
98+
99+
## Fill ##
100+
fill_result = runner.invoke(fill, ["-c", "pytest.ini", output_file])
101+
assert fill_result.exit_code == 0

src/config/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ class AppConfig(BaseModel):
2121

2222
DEFAULT_EVM_LOGS_DIR: Path = DEFAULT_LOGS_DIR / "evm"
2323
"""The default directory where EVM log files are stored."""
24+
25+
ROOT_DIR: Path = Path(__file__).resolve().parents[2]
26+
"""The root directory of the project."""

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ description = Run library and framework unit tests (pytest)
5454
setenv =
5555
# Use custom EELS_RESOLUTIONS_FILE if it is set via the environment (eg, in CI)
5656
EELS_RESOLUTIONS_FILE = {env:EELS_RESOLUTIONS_FILE:}
57-
extras = test
57+
extras =
58+
test
59+
lint # Required `gentest` for formatting tests
5860
commands_pre = solc-select use {[testenv]solc_version} --always-install
5961
commands =
6062
pytest -c ./pytest-framework.ini -n auto -m "not run_in_serial"

0 commit comments

Comments
 (0)