Skip to content

✨ feat(cli): et clean #980

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

Merged
merged 13 commits into from
Dec 4, 2024
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Test fixtures for use by clients are available for each release on the [Github r
- 🐞 Fix `Bytecode` class `__eq__` method ([#939](https://github.com/ethereum/execution-spec-tests/pull/939)).
- πŸ”€ Update `pydantic` from 2.8.2 to 2.9.2 ([#960](https://github.com/ethereum/execution-spec-tests/pull/960)).
- ✨ Add the `et make test` command, an interactive CLI that helps users create a new test module and function ([#950](https://github.com/ethereum/execution-spec-tests/pull/950)).
- ✨ Add the `et clean` command that helps delete generated files and directories from the repository ([#980](https://github.com/ethereum/execution-spec-tests/pull/980)).

### πŸ”§ EVM Tools

Expand Down
7 changes: 7 additions & 0 deletions docs/library/cli/et.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The `et` πŸ‘½ CLI

::: mkdocs-click
:module: cli.et.cli
:command: et
:depth: 1
:list_subcommands: true
1 change: 1 addition & 0 deletions docs/library/cli/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# EEST CLI Tools

- [`et`](et.md) - A CLI tool that helps with routine tasks in ethereum/execution-spec-tests.
- [`evm_bytes`](evm_bytes.md) - Convert the given EVM bytes from a binary file or a hex string to EEST's python opcodes.
- [`env_init`](env_init.md) - Generate an example `env.yaml` [environment](../../dev/configurations.md) config file if it doesn't exist.
8 changes: 5 additions & 3 deletions src/cli/et/cli.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"""
`et` is the dev CLI for EEST. It provides commands to help developers write tests.
`et` πŸ‘½ is a CLI tool that helps with routine tasks.
Invoke using `uv run et`.
"""

import click

from cli.et.commands import clean
from cli.et.make.cli import make


@click.group()
@click.group(context_settings=dict(help_option_names=["-h", "--help"], max_content_width=120))
def et():
"""
`et` πŸ‘½ is the dev CLI for EEST. It provides commands to help developers write tests.
`et` πŸ‘½ is a CLI tool that helps with routine tasks.
"""
pass

Expand All @@ -27,3 +28,4 @@ def et():
https://click.palletsprojects.com/en/8.0.x/commands/#nested-handling-and-contexts
"""
et.add_command(make)
et.add_command(clean)
9 changes: 9 additions & 0 deletions src/cli/et/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
A collection of commands supported by `et` CLI.

Run `uv run et` for complete list.
"""

from .clean import clean

__all__ = ["clean"]
92 changes: 92 additions & 0 deletions src/cli/et/commands/clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Clean CLI command removes generated files and directories.
"""

import glob
import os
import shutil

import click


@click.command(short_help="Remove all generated files and directories.")
@click.option(
"--all", is_flag=True, help="Remove the virtual environment and .tox directory as well."
)
@click.option("--dry-run", is_flag=True, help="Simulate the cleanup without removing files.")
@click.option("-v", "--verbose", is_flag=True, help="Show verbose output.")
def clean(all: bool, dry_run: bool, verbose: bool):
"""
Remove all generated files and directories from the repository.
If `--all` is specified, the virtual environment and .tox directory will also be removed.

Args:

all (flag): Remove the virtual environment and .tox directory as well.

dry_run (bool): Simulate the cleanup without removing files.

verbose (bool): Show verbose output.

Note: The virtual environment and .tox directory are not removed by default.

Example: Cleaning all generated files and directories and show the deleted items.

uv run et clean --all -v

Output:

\b
πŸ—‘οΈ Deleted: .tox
πŸ—‘οΈ Deleted: .venv
πŸ—‘οΈ Deleted: src/cli/et/__pycache__
πŸ—‘οΈ Deleted: src/cli/et/commands/__pycache__
πŸ—‘οΈ Deleted: src/cli/et/make/__pycache__
πŸ—‘οΈ Deleted: src/cli/et/make/commands/__pycache__
...
🧹 Cleanup complete!
""" # noqa: D417, D301
# List of items to remove can contain files and directories.
items_to_remove = [
".pytest_cache",
".mypy_cache",
"fixtures",
"build",
"site",
"cached_downloads",
".pyspelling_en.dict",
]

# glob patterns to remove.
patterns_to_remove = ["src/**/__pycache__", "tests/**/__pycache__"]

for pattern in patterns_to_remove:
matching_files = glob.glob(pattern, recursive=True)
items_to_remove.extend(matching_files)

if all:
items_to_remove.extend([".tox", ".venv"])

# Perform dry run or actual deletion
for item in items_to_remove:
if os.path.exists(item):
if dry_run:
click.echo(f"[🧐 Dry run] File would be deleted: {item}")
else:
try:
if os.path.isdir(item):
shutil.rmtree(item, ignore_errors=False)
else:
os.remove(item)

# Verbose flag: Output the name of the deleted item
if verbose:
click.echo(f"πŸ—‘οΈ Deleted: {item}")

except PermissionError:
click.echo(f"❌ Permission denied to remove {item}.")
except Exception as e:
click.echo(f"❌ Failed to remove {item}: {e}")

if not dry_run:
click.echo("🧹 Cleanup complete!")
4 changes: 2 additions & 2 deletions src/cli/et/make/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from .commands import test


@click.group()
@click.group(short_help="Generate project files.")
def make():
"""
Generate project files from the CLI.
Generate project files.
"""
pass

Expand Down
42 changes: 24 additions & 18 deletions src/cli/et/make/commands/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,43 @@
)


@click.command()
@click.command(
short_help="Generate a new test file for an EIP.",
epilog=f"Further help: {DocsConfig().DOCS_URL__WRITING_TESTS}",
)
def test():
"""
Create a new specification test file for an EIP.
Generate a new test file for an EIP.

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{eip_number}_{eip_name}/test_{eip_name}.py`
* Choose the type of test to generate (State or Blockchain)

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/eip1234_sample_eip/test_sample_eip.py`
* Select the fork where this functionality was introduced

The function uses Jinja2 templates to render the content of the test file.
* Enter the EIP number

Raises:
- FileNotFoundError: If the template file does not exist.
- IOError: If there is an error writing the file.
"""
* Enter the EIP name

Example:

uv run et make test

\f
<figure class="video_container">
<video controls="true" allowfullscreen="true">
<source
src="/execution-spec-tests/writing_tests/img/et_make_test.mp4"
type="video/mp4"
/>
</video>
</figure>
""" # noqa: D301
test_type = input_select(
"Choose the type of test to generate", choices=["State", "Blockchain"]
)
Expand Down
3 changes: 2 additions & 1 deletion whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ docstrings
doseq
dunder
dup
EEST
eip
eip123
eip3540
Expand All @@ -143,9 +142,11 @@ eofwrap
EOF1
EOFException
eofparse
epilog
esbenp
EEST
EEST's
et
eth
ethash
ethereum
Expand Down
Loading