Skip to content

Commit

Permalink
fix: issue where ape wouldn't recompile changed contracts [APE-1498] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 2, 2023
1 parent cab828b commit 88f9236
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ape/managers/project/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,9 @@ def _lookup_source(self, source_id: str) -> Optional[Source]:
return None

def _get_contract(self, name: str) -> Optional[ContractContainer]:
if name in self.contracts:
# NOTE: Use `load_contracts()` to re-compile changed contracts if needed.
# Else, if you make changes to a contract, it won't catch the need to re-compile.
if name in self.load_contracts():
return self.chain_manager.contracts.get_container(self.contracts[name])

return None
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import click

import ape

"""
Part of test that makes sure a contract is re-compiled
before running the script. The test changes a view method
and asserts the changed method's name appears in the script
output.
"""


def main():
contract = ape.project.TestContractVy
for method in contract.contract_type.view_methods:
click.echo(method.name)
1 change: 1 addition & 0 deletions tests/integration/cli/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"only-dependencies",
"test",
"bad-contracts",
"script",
"with-dependencies",
"with-contracts",
)
Expand Down
28 changes: 27 additions & 1 deletion tests/integration/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ def test_run(ape_cli, runner, project):
# By default, no commands are run
assert "Super secret script output" not in result.output

scripts = [s for s in project.scripts_folder.glob("*.py") if not s.name.startswith("error")]
not_part_of_test = ("output_contract_view_methods",)
scripts = [
s
for s in project.scripts_folder.glob("*.py")
if not s.name.startswith("error") and s.stem not in not_part_of_test
]
for script_file in scripts:
result = runner.invoke(ape_cli, ["run", script_file.stem], catch_exceptions=False)
assert (
Expand Down Expand Up @@ -161,3 +166,24 @@ def test_scripts_module_already_installed(ape_cli, runner, project, mocker):
assert result.exit_code == 0, result.output

del sys.modules["scripts"]


@skip_projects_except("script")
def test_run_recompiles_if_needed(ape_cli, runner, project):
"""
Ensure that when a change is made to a contract,
when we run a script, it re-compiles the script first.
"""
# Ensure we begin compiled.
runner.invoke(ape_cli, ["compile", "--force"])

# Make a change to the contract
contract = project.contracts_folder / "contract.json"
method_name = project.TestContractVy.contract_type.view_methods[0].name
new_method_name = f"f__{method_name}__"
new_contract_text = contract.read_text().replace(method_name, new_method_name)
contract.write_text(new_contract_text)

# Run the script. It better recompile first!
result = runner.invoke(ape_cli, ["run", "output_contract_view_methods"])
assert new_method_name in result.output

0 comments on commit 88f9236

Please sign in to comment.