| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,157 @@ |
| +from types import SimpleNamespace |
| + |
| +from ape.managers.project import ProjectManager |
| +from ape.types import ContractType |
| + |
| + |
| +class RecordingCompiler: |
| + def __init__(self): |
| + self.calls = [] |
| + |
| + def compile(self, paths): |
| + paths = list(paths) |
| + self.calls.append(tuple(str(path) for path in paths)) |
| + return [ |
| + ContractType( |
| + contractName=f"{path.stem}:{path.read_text()}", |
| + sourceId=str(path), |
| + abi=[], |
| + ) |
| + for path in paths |
| + ] |
| + |
| + |
| +class CompilerManagerStub: |
| + def __init__(self, compiler): |
| + self.compiler = compiler |
| + self.registered_compilers = {".foo": compiler} |
| + |
| + def compile(self, paths): |
| + return { |
| + contract.contractName: contract |
| + for contract in self.compiler.compile(paths) |
| + } |
| + |
| + |
| +def make_project(tmp_path, compiler=None): |
| + compiler = compiler or RecordingCompiler() |
| + config = SimpleNamespace(PROJECT_FOLDER=tmp_path, name="", dependencies=[]) |
| + return ProjectManager(tmp_path, config, CompilerManagerStub(compiler)), compiler |
| + |
| + |
| +def test_missing_contract_directory_has_no_unknown_extensions(tmp_path): |
| + project, _ = make_project(tmp_path) |
| + |
| + assert project.sources_missing is True |
| + assert project.extensions_with_missing_compilers == [] |
| + |
| + |
| +def test_empty_contract_directory_is_reported_as_missing(tmp_path): |
| + project, _ = make_project(tmp_path) |
| + (tmp_path / "contracts").mkdir() |
| + assert project.sources_missing is True |
| + assert project.extensions_with_missing_compilers == [] |
| + |
| + |
| +def make_nested_sources(tmp_path): |
| + project, _ = make_project(tmp_path) |
| + contracts = tmp_path / "contracts" |
| + (contracts / "nested").mkdir(parents=True) |
| + for relative in ( |
| + "z.foo", |
| + "a.bar", |
| + "q.zeta", |
| + "nested/m.foo", |
| + "nested/b.bar", |
| + "nested/a.alpha", |
| + ): |
| + (contracts / relative).write_text(relative) |
| + return project, contracts |
| + |
| + |
| +def test_sources_are_recursive_and_sorted(tmp_path): |
| + project, contracts = make_nested_sources(tmp_path) |
| + assert [path.relative_to(contracts).as_posix() for path in project.sources] == [ |
| + "nested/m.foo", |
| + "z.foo", |
| + ] |
| + |
| + |
| +def test_unknown_extensions_are_recursive_and_sorted(tmp_path): |
| + project, _ = make_nested_sources(tmp_path) |
| + assert project.extensions_with_missing_compilers == [".alpha", ".bar", ".zeta"] |
| + |
| + |
| +def test_corrupt_manifest_cache_is_ignored_and_rebuilt(tmp_path): |
| + project, compiler = make_project(tmp_path) |
| + contracts = tmp_path / "contracts" |
| + contracts.mkdir() |
| + source = contracts / "one.foo" |
| + source.write_text("v1") |
| + cache = tmp_path / ".build" / "__local__.json" |
| + cache.parent.mkdir() |
| + cache.write_text("not-json") |
| + |
| + loaded = project.load_contracts() |
| + |
| + assert set(loaded) == {"one:v1"} |
| + assert compiler.calls == [(str(source),)] |
| + |
| + |
| +def setup_cached_sources(tmp_path): |
| + project, compiler = make_project(tmp_path) |
| + contracts = tmp_path / "contracts" |
| + contracts.mkdir() |
| + first = contracts / "first.foo" |
| + second = contracts / "second.foo" |
| + first.write_text("v1") |
| + second.write_text("v1") |
| + |
| + initial = project.load_contracts() |
| + assert set(initial) == {"first:v1", "second:v1"} |
| + assert compiler.calls[-1] == (str(first), str(second)) |
| + return project, compiler, contracts, first, second |
| + |
| + |
| +def test_incremental_cache_compiles_only_changed_and_new_sources(tmp_path): |
| + project, compiler, contracts, first, second = setup_cached_sources(tmp_path) |
| + first.write_text("v2") |
| + third = contracts / "third.foo" |
| + third.write_text("v1") |
| + updated = project.load_contracts() |
| + assert compiler.calls[-1] == (str(first), str(third)) |
| + assert "second:v1" in updated |
| + assert "third:v1" in updated |
| + |
| + |
| +def test_changed_source_replaces_its_stale_contract_types(tmp_path): |
| + project, _, _, first, _ = setup_cached_sources(tmp_path) |
| + first.write_text("v2") |
| + updated = project.load_contracts() |
| + assert "first:v1" not in updated |
| + assert updated["first:v2"].sourceId == str(first) |
| + |
| + |
| +def test_deleted_source_drops_its_cached_contract_types(tmp_path): |
| + project, _, _, _, second = setup_cached_sources(tmp_path) |
| + second.unlink() |
| + project.load_contracts() |
| + |
| + reloaded_project, _ = make_project(tmp_path) |
| + after_delete = reloaded_project.load_contracts() |
| + assert set(after_delete) == {"first:v1"} |
| + |
| + |
| +def test_force_rebuild_recompiles_all_current_sources(tmp_path): |
| + project, compiler = make_project(tmp_path) |
| + contracts = tmp_path / "contracts" |
| + contracts.mkdir() |
| + first = contracts / "first.foo" |
| + second = contracts / "second.foo" |
| + first.write_text("v1") |
| + second.write_text("v1") |
| + |
| + project.load_contracts() |
| + project.load_contracts(use_cache=False) |
| + |
| + assert compiler.calls[-1] == (str(first), str(second)) |
| |
| new file mode 100755 |
| |
| |
| @@ -0,0 +1,6 @@ |
| +#!/bin/bash |
| +set -euo pipefail |
| + |
| + |
| +pytest -q tests/functional/project/test_project_manager_cache_contract.py |
| + |
|
|