Skip to content

Commit

Permalink
chore: Merge branch 'main' into fix/raise-single-error-if-only-1
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Aug 27, 2024
2 parents cd91d2d + 9e932b8 commit 1a1682f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
6 changes: 3 additions & 3 deletions docs/userguides/console.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Ape Console
# Console

Ape provides an [IPython](https://ipython.readthedocs.io/) interactive console with useful pre-defined locals to interact with your project.

Expand Down Expand Up @@ -56,14 +56,14 @@ You can also create scripts to be included in the console namespace by adding a
An example file might look something like this:

```python
from ape import networks
from eth_utils import encode_hex, decode_hex


def latest(key):
return getattr(networks.active_provider.get_block("latest"), key)
```

Then both imported util functions and `WETH_ADDRESS` will be available when you launch the console.
Then both imported util functions and `latest()` will be available when you launch the console.

```python
In [1]: latest('number')
Expand Down
16 changes: 12 additions & 4 deletions src/ape/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from ape.api.projects import ApeProject, DependencyAPI, ProjectAPI
from ape.contracts import ContractContainer, ContractInstance
from ape.exceptions import APINotImplementedError, ChainError, ProjectError
from ape.exceptions import APINotImplementedError, ChainError, CompilerError, ProjectError
from ape.logging import logger
from ape.managers.base import BaseManager
from ape.managers.config import ApeConfig
Expand Down Expand Up @@ -1891,6 +1891,14 @@ def reconfigure(self, **overrides):
self.account_manager.test_accounts.reset()

def extract_manifest(self) -> PackageManifest:
# Attempt to compile, if needed.
try:
self.load_contracts()
except CompilerError as err:
# Some manifest-based projects may not require compiling,
# such as OpenZeppelin or snekmate.
logger.warning(err)

return self.manifest

def clean(self):
Expand Down Expand Up @@ -2460,9 +2468,9 @@ def extract_manifest(self) -> PackageManifest:

sources = dict(self.sources)
contract_types = {
n: ct
for n, ct in (self.manifest.contract_types or {}).items()
if ct.source_id in sources
n: c.contract_type
for n, c in self.load_contracts().items()
if c.contract_type.source_id in sources
}

# Add any remaining data to the manifest here.
Expand Down
41 changes: 41 additions & 0 deletions tests/functional/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ def test_extract_manifest_excludes_cache(tmp_project):
assert ".cache/subdir/CacheFile.json" not in (manifest.sources or {})


def test_extract_manifest_compiles(tmp_project):
tmp_project.manifest.contract_types = {} # Not compiled.
actual = tmp_project.extract_manifest()
assert actual.contract_types # Fails if empty


def test_extract_manifest_from_manifest_project(project_from_manifest):
project_from_manifest.manifest.contract_types = {} # Not compiled.
manifest = project_from_manifest.extract_manifest()
assert "FooContractFromManifest" in manifest.contract_types


def test_exclusions(tmp_project):
exclusions = ["Other.json", "*Excl*"]
exclude_config = {"compile": {"exclude": exclusions}}
Expand Down Expand Up @@ -417,6 +429,35 @@ def test_load_contracts_output_abi(tmp_project):
assert isinstance(data[0], dict)


def test_load_contracts_use_cache(mocker, tmp_project):
"""
Showing the 'use_cache=bool' kwarg works.
"""
compile_spy = mocker.spy(tmp_project.contracts, "_compile")

tmp_project.manifest.contract_types = {} # Force initial compile.
contracts = tmp_project.load_contracts(use_cache=True)
assert "Other" in contracts # Other.json contract.
assert "Project" in contracts # Project.json contract.
assert compile_spy.call_args_list[-1][-1]["use_cache"] is True

# Show they get added to the manifest.
assert "Other" in tmp_project.manifest.contract_types
assert "Project" in tmp_project.manifest.contract_types

# Showe we can use the cache again (no compiling!)
contracts = tmp_project.load_contracts(use_cache=True)
assert "Other" in contracts # Other.json contract.
assert "Project" in contracts # Project.json contract.
assert compile_spy.call_args_list[-1][-1]["use_cache"] is True

# Show force-recompiles.
contracts = tmp_project.load_contracts(use_cache=False)
assert "Other" in contracts # Other.json contract.
assert "Project" in contracts # Project.json contract.
assert compile_spy.call_args_list[-1][-1]["use_cache"] is False


def test_manifest_path(tmp_project):
assert tmp_project.manifest_path == tmp_project.path / ".build" / "__local__.json"

Expand Down

0 comments on commit 1a1682f

Please sign in to comment.