Skip to content

Commit 2e8b890

Browse files
authored
feat: add wrap test harness (#185)
1 parent 3161bf7 commit 2e8b890

File tree

10 files changed

+1190
-0
lines changed

10 files changed

+1190
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wrappers/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# polywrap-wasm
2+
3+
Python implementation of the plugin wrapper runtime.
4+
5+
## Usage
6+
7+
### Invoke Plugin Wrapper
8+
9+
```python
10+
from typing import Any, Dict, List, Union, Optional
11+
from polywrap_manifest import AnyWrapManifest
12+
from polywrap_plugin import PluginModule
13+
from polywrap_core import Invoker, Uri, InvokerOptions, UriPackageOrWrapper, Env
14+
15+
class GreetingModule(PluginModule[None]):
16+
def __init__(self, config: None):
17+
super().__init__(config)
18+
19+
def greeting(self, args: Dict[str, Any], client: Invoker[UriPackageOrWrapper], env: Optional[Env] = None):
20+
return f"Greetings from: {args['name']}"
21+
22+
manifest = cast(AnyWrapManifest, {})
23+
wrapper = PluginWrapper(greeting_module, manifest)
24+
args = {
25+
"name": "Joe"
26+
}
27+
options: InvokeOptions[UriPackageOrWrapper] = InvokeOptions(
28+
uri=Uri.from_str("ens/greeting.eth"),
29+
method="greeting",
30+
args=args
31+
)
32+
invoker: Invoker = ...
33+
34+
result = await wrapper.invoke(options, invoker)
35+
assert result, "Greetings from: Joe"
36+
```

packages/polywrap-test-cases/poetry.lock

Lines changed: 996 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""This package contains the utility functions to fetch the wrappers\
2+
from the wasm-test-harness repo."""
3+
import io
4+
import os
5+
import zipfile
6+
from urllib.error import HTTPError
7+
from urllib.request import urlopen
8+
9+
10+
def get_path_to_test_wrappers() -> str:
11+
"""Get the path to the test wrappers."""
12+
return os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "wrappers"))
13+
14+
15+
def fetch_file(url: str) -> bytes:
16+
"""Fetch a file using HTTP."""
17+
with urlopen(url) as response:
18+
if response.status != 200:
19+
raise HTTPError(
20+
url, response.status, "Failed to fetch file", response.headers, None
21+
)
22+
return response.read()
23+
24+
25+
def unzip_file(file_buffer: bytes, destination: str) -> None:
26+
"""Unzip a file to a destination."""
27+
with zipfile.ZipFile(io.BytesIO(file_buffer), "r") as zip_ref:
28+
zip_ref.extractall(destination)
29+
30+
31+
def fetch_wrappers() -> None:
32+
"""Fetch the wrappers from the wasm-test-harness repo."""
33+
tag = "0.1.0"
34+
repo_name = "wasm-test-harness"
35+
url = f"https://github.com/polywrap/{repo_name}/releases/download/{tag}/wrappers"
36+
37+
buffer = fetch_file(url)
38+
zip_built_folder = get_path_to_test_wrappers()
39+
unzip_file(buffer, zip_built_folder)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""This module contains the main entry point of the package."""
2+
from . import fetch_wrappers
3+
4+
if __name__ == "__main__":
5+
fetch_wrappers()

packages/polywrap-test-cases/polywrap_test_cases/py.typed

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[build-system]
2+
requires = ["poetry-core"]
3+
build-backend = "poetry.core.masonry.api"
4+
5+
[tool.poetry]
6+
name = "polywrap-test-cases"
7+
version = "0.1.0a29"
8+
description = "Plugin package"
9+
authors = ["Cesar <cesar@polywrap.io>"]
10+
readme = "README.md"
11+
12+
[tool.poetry.dependencies]
13+
python = "^3.10"
14+
15+
[tool.poetry.dev-dependencies]
16+
pytest = "^7.1.2"
17+
pytest-asyncio = "^0.19.0"
18+
pylint = "^2.15.4"
19+
black = "^22.10.0"
20+
bandit = { version = "^1.7.4", extras = ["toml"]}
21+
tox = "^3.26.0"
22+
tox-poetry = "^0.4.1"
23+
isort = "^5.10.1"
24+
pyright = "^1.1.275"
25+
pydocstyle = "^6.1.1"
26+
27+
[tool.poetry.group.dev.dependencies]
28+
pycln = "^2.1.3"
29+
30+
[tool.bandit]
31+
exclude_dirs = ["tests"]
32+
skips = ["B310"]
33+
34+
[tool.black]
35+
target-version = ["py310"]
36+
37+
[tool.pyright]
38+
typeCheckingMode = "strict"
39+
reportShadowedImports = false
40+
41+
[tool.pytest.ini_options]
42+
asyncio_mode = "auto"
43+
testpaths = [
44+
"tests"
45+
]
46+
47+
[tool.pylint]
48+
disable = [
49+
]
50+
ignore = [
51+
"tests/"
52+
]
53+
54+
[tool.isort]
55+
profile = "black"
56+
multi_line_output = 3
57+
58+
[tool.pydocstyle]
59+
# default
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
from polywrap_test_cases import get_path_to_test_wrappers
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize(
7+
"wrapper", ["asyncify", "bigint-type", "enum-type", "map-type"]
8+
)
9+
@pytest.mark.parametrize("language", ["as", "rs"])
10+
def test_wrappers_exist(wrapper: str, language: str):
11+
assert os.path.exists(get_path_to_test_wrappers())
12+
wrapper_path = os.path.join(get_path_to_test_wrappers(), wrapper, "implementations", language)
13+
assert os.path.exists(wrapper_path)
14+
assert os.path.isdir(wrapper_path)
15+
assert os.path.exists(os.path.join(wrapper_path, "wrap.info"))
16+
assert os.path.exists(os.path.join(wrapper_path, "wrap.wasm"))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[tox]
2+
isolated_build = True
3+
envlist = py310
4+
5+
[testenv]
6+
commands =
7+
python -m polywrap_test_cases
8+
pytest tests/
9+
10+
[testenv:getwraps]
11+
commands =
12+
python -m polywrap_test_cases
13+
14+
[testenv:lint]
15+
commands =
16+
isort --check-only polywrap_test_cases
17+
black --check polywrap_test_cases
18+
pycln --check polywrap_test_cases --disable-all-dunder-policy
19+
pylint polywrap_test_cases
20+
pydocstyle polywrap_test_cases
21+
22+
[testenv:typecheck]
23+
commands =
24+
pyright polywrap_test_cases
25+
26+
[testenv:secure]
27+
commands =
28+
bandit -r polywrap_test_cases -c pyproject.toml
29+
30+
[testenv:dev]
31+
commands =
32+
isort polywrap_test_cases
33+
black polywrap_test_cases
34+
pycln polywrap_test_cases --disable-all-dunder-policy

python-monorepo.code-workspace

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
{
4040
"name": "polywrap-plugin",
4141
"path": "packages/polywrap-plugin"
42+
},
43+
{
44+
"name": "polywrap-test-cases",
45+
"path": "packages/polywrap-test-cases"
4246
}
4347
],
4448
"settings": {

0 commit comments

Comments
 (0)