Skip to content

Commit

Permalink
test: make helper script for updating test contracts (ApeWorX#2254)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 30, 2024
1 parent b1bbc23 commit 3ad3407
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/functional/data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Test Data

All contracts use `.JSON` contract types because core tests cannot assume `ape-vyper` or `ape-solidity` is installed.
These artifacts are stored in `tests/functional/data/contracts/ethereum/local`.
Their counter-part actual source files are located in `tests/functional/data/sources` and have corresponding names (e.g. `SolidityContract.json` matches up with `SolidityContract.sol`).

## Making Changes

To make changes to the source files, first ensure you have `ape-solidity` and/or `ape-vyper` installed and then edit the files in `sources/`.
After you are ready to test your changes, from this directory, run the following script:

```shell
ape run update <MyContract>
```

Now, it's corresponding JSON file should have been updated.
32 changes: 32 additions & 0 deletions tests/functional/data/scripts/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pathlib import Path

import click

from ape.cli import ape_cli_context

BASE_PATH = Path(__file__).parent.parent
SOURCES_PATH = BASE_PATH / "sources"
ARTIFACTS_PATH = BASE_PATH / "contracts" / "ethereum" / "local"


def _contract_callback(ctx, param, val):
for ext in ("sol", "vy"):
path = SOURCES_PATH / f"{val}.{ext}"
if path.is_file():
return path

raise click.BadArgumentUsage(f"Contract not found: {val}")


@click.command()
@ape_cli_context()
@click.argument("contract", callback=_contract_callback)
def cli(cli_ctx, contract):
cm = cli_ctx.compiler_manager
compiler = "vyper" if contract.suffix == ".vy" else "solidity"
code = contract.read_text(encoding="utf-8")
destination = ARTIFACTS_PATH / f"{contract.stem}.json"
contract_type = cm.compile_source(compiler, code, contractName=contract.stem)
destination.unlink()
destination.write_text(contract_type.model_dump_json())
click.echo("Done!")

0 comments on commit 3ad3407

Please sign in to comment.