From 3ad3407703af2be16538d95a283f2a34bbcaa06a Mon Sep 17 00:00:00 2001 From: antazoey Date: Fri, 30 Aug 2024 11:41:05 -0500 Subject: [PATCH] test: make helper script for updating test contracts (#2254) --- tests/functional/data/README.md | 16 +++++++++++++ tests/functional/data/scripts/update.py | 32 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/functional/data/README.md create mode 100644 tests/functional/data/scripts/update.py diff --git a/tests/functional/data/README.md b/tests/functional/data/README.md new file mode 100644 index 0000000000..1691c2988e --- /dev/null +++ b/tests/functional/data/README.md @@ -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 +``` + +Now, it's corresponding JSON file should have been updated. diff --git a/tests/functional/data/scripts/update.py b/tests/functional/data/scripts/update.py new file mode 100644 index 0000000000..41ae1bcdf1 --- /dev/null +++ b/tests/functional/data/scripts/update.py @@ -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!")