From 3e229b369988026a5ba0d152dcff6b97a0b77b53 Mon Sep 17 00:00:00 2001 From: rahul Date: Wed, 11 Dec 2024 11:26:19 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=A8=20feat:=20Tox=20environment=20for?= =?UTF-8?q?=20gentets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli/gentest/source_code_generator.py | 32 ++++++++++++++---------- src/config/app.py | 3 +++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/cli/gentest/source_code_generator.py b/src/cli/gentest/source_code_generator.py index 8da2bef155..d9a5871c25 100644 --- a/src/cli/gentest/source_code_generator.py +++ b/src/cli/gentest/source_code_generator.py @@ -11,6 +11,8 @@ import jinja2 +from config import AppConfig + from .test_context_providers import Provider template_loader = jinja2.PackageLoader("cli.gentest") @@ -74,19 +76,23 @@ 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: + print(f"Error formatting code: {e}", file=sys.stderr) + raise # Return the formatted source code return input_file_path.read_text() diff --git a/src/config/app.py b/src/config/app.py index 7fd700e35b..593ee93e9e 100644 --- a/src/config/app.py +++ b/src/config/app.py @@ -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.""" From 931a929c5654173d7d783aa7c5d472a273236e2c Mon Sep 17 00:00:00 2001 From: rahul Date: Tue, 14 Jan 2025 10:33:28 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=A8=20feat:=20Gentest=20tox=20using?= =?UTF-8?q?=20pytest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli/gentest/tests/__init__.py | 1 + src/cli/gentest/tests/test_cli.py | 102 ++++++++++++++++++++++++++++++ tox.ini | 12 +++- 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/cli/gentest/tests/__init__.py create mode 100644 src/cli/gentest/tests/test_cli.py diff --git a/src/cli/gentest/tests/__init__.py b/src/cli/gentest/tests/__init__.py new file mode 100644 index 0000000000..93c0e2ef9d --- /dev/null +++ b/src/cli/gentest/tests/__init__.py @@ -0,0 +1 @@ +"""Test cases for the `generate` CLI.""" diff --git a/src/cli/gentest/tests/test_cli.py b/src/cli/gentest/tests/test_cli.py new file mode 100644 index 0000000000..c66efc406c --- /dev/null +++ b/src/cli/gentest/tests/test_cli.py @@ -0,0 +1,102 @@ +"""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(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 = "tests/paris/test_0xa41f.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) + + ## Act ## + gentest_result = runner.invoke(generate, [transaction_hash, output_file]) + print(output_file) + fill_result = runner.invoke(fill, [output_file]) + + ## Assert ## + assert gentest_result.exit_code == 0 + assert fill_result.exit_code == 0 diff --git a/tox.ini b/tox.ini index 2bdb5f6b47..b77186290e 100644 --- a/tox.ini +++ b/tox.ini @@ -148,4 +148,14 @@ setenv = commands = {[testenv:spellcheck]commands} {[testenv:markdownlint]commands} - {[testenv:mkdocs]commands} \ No newline at end of file + {[testenv:mkdocs]commands} + +[testenv:gentest] +description = Test the `gentest` cli. + +extras = + lint + test + +commands = + pytest -c pytest-framework.ini src/cli/gentest \ No newline at end of file From fbf0c989033841c17081a36ab6a50c53418c8d0b Mon Sep 17 00:00:00 2001 From: rahul Date: Fri, 17 Jan 2025 06:53:04 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20feat:=20Gentest=20fill?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli/gentest/tests/test_cli.py | 7 +++---- tox.ini | 16 ++++------------ 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/cli/gentest/tests/test_cli.py b/src/cli/gentest/tests/test_cli.py index c66efc406c..18e138404a 100644 --- a/src/cli/gentest/tests/test_cli.py +++ b/src/cli/gentest/tests/test_cli.py @@ -9,7 +9,7 @@ from ethereum_test_tools import Environment, Storage, Transaction -def test_generate_success(monkeypatch): +def test_generate_success(tmp_path, monkeypatch): """Test the generate command with a successful scenario.""" ## Arrange ## @@ -18,7 +18,7 @@ def test_generate_success(monkeypatch): # This is done by patching the `get_context` method of the `StateTestProvider`. runner = CliRunner() transaction_hash = "0xa41f343be7a150b740e5c939fa4d89f3a2850dbe21715df96b612fc20d1906be" - output_file = "tests/paris/test_0xa41f.py" + output_file = str(tmp_path / "gentest.py") def get_mock_context(self: StateTestProvider) -> dict: return { @@ -94,8 +94,7 @@ def get_mock_context(self: StateTestProvider) -> dict: ## Act ## gentest_result = runner.invoke(generate, [transaction_hash, output_file]) - print(output_file) - fill_result = runner.invoke(fill, [output_file]) + fill_result = runner.invoke(fill, ["-c", "pytest.ini", output_file]) ## Assert ## assert gentest_result.exit_code == 0 diff --git a/tox.ini b/tox.ini index b77186290e..f97df646b1 100644 --- a/tox.ini +++ b/tox.ini @@ -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" @@ -148,14 +150,4 @@ setenv = commands = {[testenv:spellcheck]commands} {[testenv:markdownlint]commands} - {[testenv:mkdocs]commands} - -[testenv:gentest] -description = Test the `gentest` cli. - -extras = - lint - test - -commands = - pytest -c pytest-framework.ini src/cli/gentest \ No newline at end of file + {[testenv:mkdocs]commands} \ No newline at end of file From 84444ef7531ec1d43fdd16674481337b6bcaae54 Mon Sep 17 00:00:00 2001 From: Mario Vega Date: Mon, 20 Jan 2025 14:29:38 -0600 Subject: [PATCH 4/4] Apply suggestions from code review --- src/cli/gentest/source_code_generator.py | 3 +-- src/cli/gentest/tests/test_cli.py | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/cli/gentest/source_code_generator.py b/src/cli/gentest/source_code_generator.py index d9a5871c25..e437631aad 100644 --- a/src/cli/gentest/source_code_generator.py +++ b/src/cli/gentest/source_code_generator.py @@ -91,8 +91,7 @@ def format_code(code: str) -> str: check=True, ) except subprocess.CalledProcessError as e: - print(f"Error formatting code: {e}", file=sys.stderr) - raise + raise Exception(f"Error formatting code using formatter '{formatter_path}'") from e # Return the formatted source code return input_file_path.read_text() diff --git a/src/cli/gentest/tests/test_cli.py b/src/cli/gentest/tests/test_cli.py index 18e138404a..158201dbae 100644 --- a/src/cli/gentest/tests/test_cli.py +++ b/src/cli/gentest/tests/test_cli.py @@ -92,10 +92,10 @@ def get_mock_context(self: StateTestProvider) -> dict: monkeypatch.setattr(StateTestProvider, "get_context", get_mock_context) - ## Act ## + ## Genenrate ## gentest_result = runner.invoke(generate, [transaction_hash, output_file]) - fill_result = runner.invoke(fill, ["-c", "pytest.ini", output_file]) - - ## Assert ## assert gentest_result.exit_code == 0 + + ## Fill ## + fill_result = runner.invoke(fill, ["-c", "pytest.ini", output_file]) assert fill_result.exit_code == 0