Skip to content

Commit

Permalink
🚧 wip: Make test
Browse files Browse the repository at this point in the history
  • Loading branch information
raxhvl committed Nov 13, 2024
1 parent f8e4ee9 commit bdb4bcd
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ order_fixtures = "cli.order_fixtures:order_fixtures"
evm_bytes = "cli.evm_bytes:cli"
hasher = "cli.hasher:main"
env_init = "config.env:create_default_config"
make_test = "cli.make.cli:test"

[tool.setuptools.packages.find]
where = ["src"]
Expand Down
10 changes: 10 additions & 0 deletions src/cli/make/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
Make CLI
This module provides the `make` CLI command that helps you quickly scaffold
files.
"""

from .cli import test

__all__ = ["test"]
78 changes: 78 additions & 0 deletions src/cli/make/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
CLI interface for generating blockchain test scripts.
It extracts a specified transaction and its required state from a blockchain network
using the transaction hash and generates a Python test script based on that information.
"""

import os

import jinja2

from cli.input import input_select, input_text

template_loader = jinja2.PackageLoader("cli.make")
template_env = jinja2.Environment(loader=template_loader, keep_trailing_newline=True)


def test():
"""
Scaffold a test file from the command line interface (CLI).
This function guides the user through a series of prompts to generate a test file
for Ethereum execution specifications. The user is prompted to select the type of test,
the fork to use, and to provide the EIP number and name. Based on the inputs, a test file
is created in the appropriate directory with a rendered template.
Prompts:
- Choose the type of test to generate (State or Blockchain)
- Select the fork to use (Prague or Osaka)
- Enter the EIP number
- Enter the EIP name
The generated test file is saved in the following format:
`tests/{fork}/{eip_number}_{eip_name}/test_{eip_name}.py`
Example:
If the user selects "State" as the test type, "Prague" as the fork,
enters "1234" as the EIP number,
and "Sample EIP" as the EIP name, the generated file will be:
`tests/prague/1234_sample_eip/test_sample_eip.py`
The function uses Jinja2 templates to render the content of the test file.
Raises:
- FileNotFoundError: If the template file does not exist.
- IOError: If there is an error writing the file.
"""
test_type = input_select(
"Choose the type of test to generate", choices=["State", "Blockchain"]
)
# TODO: Get forks from a config called `UPCOMING_FORKS`
fork = input_select("Select the fork to use", choices=["Prague", "Osaka"])

eip_number = input_text("Enter the EIP number")

# TODO: Perhaps get the EIP name from the number using an API?
eip_name = input_text("Enter the EIP name")

file_name = f"test_{eip_name.lower().replace(' ', '_')}.py"

directory_path = f"tests/{fork.lower()}/{eip_number}-{eip_name.lower().replace(' ', '_')}"
file_path = f"{directory_path}/{file_name}"

# Create directories if they don't exist
os.makedirs(directory_path, exist_ok=True)

template = template_env.get_template(f"{test_type.lower()}_test.py.j2")
rendered_template = template.render(
fork=fork,
eip_number=eip_number,
eip_name=eip_name,
test_name=eip_name.lower().replace(" ", "_"),
)

with open(file_path, "w") as file:
file.write(rendered_template)

print(f"Test file created at: {file_path}")
21 changes: 21 additions & 0 deletions src/cli/make/templates/blockchain_test.py.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
A blockchain test for EIP-{{eip_number}} [fork: `{{fork}}`].
"""

import pytest

from ethereum_test_tools import Alloc, Environment, BlockchainTestFiller, Transaction


@pytest.mark.valid_from("{{fork}}")
def test_{{test_name}}(blockchain_test: BlockchainTestFiller, pre: Alloc):
"""
Test type 1 transaction.
"""
env = Environment()

tx = Transaction()

post: dict[str, dict] = {}

blockchain_test(env=env, pre=pre, post=post, tx=[tx])
21 changes: 21 additions & 0 deletions src/cli/make/templates/state_test.py.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
A state test for EIP-{{eip_number}} [fork: `{{fork}}`].
"""

import pytest

from ethereum_test_tools import Alloc, Environment, StateTestFiller, Transaction


@pytest.mark.valid_from("{{fork}}")
def test_{{test_name}}(state_test: StateTestFiller, pre: Alloc):
"""
Test type 1 transaction.
"""
env = Environment()

tx = Transaction()

post: dict[str, dict] = {}

state_test(env=env, pre=pre, post=post, tx=tx)

0 comments on commit bdb4bcd

Please sign in to comment.