Skip to content

Commit

Permalink
Merge pull request #167 from njgheorghita/builder-build-dependencies
Browse files Browse the repository at this point in the history
Write build dependency mechanism for manifest building
  • Loading branch information
njgheorghita committed Jul 8, 2019
2 parents caa0b70 + d0a725c commit 147f5d7
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
34 changes: 34 additions & 0 deletions docs/tools.rst
Expand Up @@ -572,6 +572,40 @@ This builder function simplifies adding the same contract type deployment across
),
)
To add a build dependency
~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python
build(
...,
build_dependency(
package_name,
uri,
),
...,
)
.. py:function:: build_dependency(package_name, uri)
To add a build dependency to your manifest, just provide the package's name and a supported, content-addressed URI.

.. doctest::

>>> expected_manifest = {
... 'package_name': 'owned',
... 'manifest_version': '2',
... 'version': '1.0.0',
... 'build_dependencies': {
... 'owned': 'ipfs://QmbeVyFLSuEUxiXKwSsEjef6icpdTdA4kGG9BcrJXKNKUW',
... }
... }
>>> built_manifest = build(
... BASE_MANIFEST,
... build_dependency('owned', 'ipfs://QmbeVyFLSuEUxiXKwSsEjef6icpdTdA4kGG9BcrJXKNKUW'),
... )
>>> assert expected_manifest == built_manifest


Checker
-------
Expand Down
26 changes: 25 additions & 1 deletion ethpm/tools/builder.py
Expand Up @@ -26,7 +26,8 @@
validate_manifest_against_schema,
)
from ethpm.utils.mappings import deep_merge_dicts
from ethpm.validation import validate_address
from ethpm.utils.uri import is_supported_content_addressed_uri
from ethpm.validation import validate_address, validate_package_name


def build(obj: Dict[str, Any], *fns: Callable[..., Any]) -> Dict[str, Any]:
Expand Down Expand Up @@ -676,6 +677,29 @@ def _build_deployments_object(
yield "runtime_bytecode", runtime_bytecode


#
# Build Dependencies
#


def build_dependency(package_name: str, uri: URI) -> Manifest:
"""
Returns the manifest with injected build dependency.
"""
return _build_dependency(package_name, uri)


@curry
def _build_dependency(package_name: str, uri: URI, manifest: Manifest) -> Manifest:
validate_package_name(package_name)
if not is_supported_content_addressed_uri(uri):
raise ValidationError(
f"{uri} is not a supported content-addressed URI. "
"Currently only IPFS and Github blob uris are supported."
)
return assoc_in(manifest, ("build_dependencies", package_name), uri)


#
# Helpers
#
Expand Down
48 changes: 47 additions & 1 deletion tests/ethpm/tools/test_builder.py
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path

from eth_utils import to_canonical_address
from eth_utils.toolz import assoc
from eth_utils.toolz import assoc, assoc_in
import pytest
from pytest_ethereum.linker import deploy, link, linker

Expand All @@ -13,6 +13,7 @@
as_package,
authors,
build,
build_dependency,
contract_type,
deployment,
deployment_type,
Expand Down Expand Up @@ -678,3 +679,48 @@ def test_builder_deployment_type_complex(escrow_package):
assert len(manifest["deployments"].keys()) == 2
assert len(list(manifest["deployments"].values())[0]) == 2
assert len(list(manifest["deployments"].values())[1]) == 2


def test_builder_with_single_build_dependency():
expected_build_dep = {
"package": "ipfs://QmUYcVzTfSwJoigggMxeo2g5STWAgJdisQsqcXHws7b1FW"
}
expected = assoc_in(BASE_MANIFEST, ["build_dependencies"], expected_build_dep)
actual = build(
BASE_MANIFEST,
build_dependency(
"package", "ipfs://QmUYcVzTfSwJoigggMxeo2g5STWAgJdisQsqcXHws7b1FW"
),
)
assert actual == expected


def test_builder_with_multiple_build_dependencies():
expected_build_deps = {
"escrow": "ipfs://QmPDwMHk8e1aMEZg3iKsUiPSkhHkywpGB3KHKM52RtGrkv",
"package": "ipfs://QmUYcVzTfSwJoigggMxeo2g5STWAgJdisQsqcXHws7b1FW",
}
expected = assoc_in(BASE_MANIFEST, ["build_dependencies"], expected_build_deps)
actual = build(
BASE_MANIFEST,
build_dependency(
"package", "ipfs://QmUYcVzTfSwJoigggMxeo2g5STWAgJdisQsqcXHws7b1FW"
),
build_dependency(
"escrow", "ipfs://QmPDwMHk8e1aMEZg3iKsUiPSkhHkywpGB3KHKM52RtGrkv"
),
)
assert actual == expected


def test_builder_with_invalid_uri():
with pytest.raises(
ValidationError, match="is not a supported content-addressed URI"
):
build(
{},
package_name("package"),
version("1.0.0"),
manifest_version("2"),
build_dependency("package", "www.google.com"),
)

0 comments on commit 147f5d7

Please sign in to comment.