Skip to content

Commit febebb3

Browse files
committed
feat: add wrap test harness
1 parent 5df78c1 commit febebb3

File tree

10 files changed

+1188
-0
lines changed

10 files changed

+1188
-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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
33+
[tool.black]
34+
target-version = ["py310"]
35+
36+
[tool.pyright]
37+
typeCheckingMode = "strict"
38+
reportShadowedImports = false
39+
40+
[tool.pytest.ini_options]
41+
asyncio_mode = "auto"
42+
testpaths = [
43+
"tests"
44+
]
45+
46+
[tool.pylint]
47+
disable = [
48+
]
49+
ignore = [
50+
"tests/"
51+
]
52+
53+
[tool.isort]
54+
profile = "black"
55+
multi_line_output = 3
56+
57+
[tool.pydocstyle]
58+
# 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[tox]
2+
isolated_build = True
3+
envlist = py310
4+
5+
[testenv]
6+
commands =
7+
pytest tests/
8+
9+
[testenv:getwraps]
10+
commands =
11+
python -m polywrap_test_cases
12+
13+
[testenv:lint]
14+
commands =
15+
isort --check-only polywrap_test_cases
16+
black --check polywrap_test_cases
17+
pycln --check polywrap_test_cases --disable-all-dunder-policy
18+
pylint polywrap_test_cases
19+
pydocstyle polywrap_test_cases
20+
21+
[testenv:typecheck]
22+
commands =
23+
pyright polywrap_test_cases
24+
25+
[testenv:secure]
26+
commands =
27+
bandit -r polywrap_test_cases -c pyproject.toml
28+
29+
[testenv:dev]
30+
commands =
31+
isort polywrap_test_cases
32+
black polywrap_test_cases
33+
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)