Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Add integration tests. #7

Merged
merged 1 commit into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,810 changes: 1,810 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -17,4 +17,6 @@ env_logger = "0.8.3"
wat = "1.0.37"

[workspace]
members = []
members = [
"crates/test-modules"
]
38 changes: 28 additions & 10 deletions README.md
Expand Up @@ -24,56 +24,74 @@ $ cargo run

## Testing

To run tests:
To run tests, first install the `wasm32-wasi` target:

```text
$ rustup target install wasm32-wasi
```

With the target installed, run the tests:

```text
$ cargo test --all
```

## Demo

### Prerequisites

The demo requires [cargo-wasi](https://github.com/bytecodealliance/cargo-wasi), so install it using `cargo`:

```text
$ cargo install cargo-wasi
```

First, build the `markdown` module:
### Building the `markdown` module

The `markdown` module exposes an interface consisting of a `render` function that takes a string (the [Markdown](https://en.wikipedia.org/wiki/Markdown)) as an argument and returns a string (the rendered HTML).

To build the `markdown` module:

```text
$ cd demo/markdown
$ cargo wasi build
$ cp markdown.witx target/wasm32-wasi/debug/markdown.witx
```

This module exposes an interface consisting of a `render` function that takes a string (the [Markdown](https://en.wikipedia.org/wiki/Markdown)) as an argument and returns a string (the rendered HTML).

_Note: the linker currently expects either an embedded witx file in a custom section of the module or a witx file of the same name next to the input wasm module, so we copy the witx file to the target directory above._

Next, build the `renderer` module:
### Building the `renderer` module

The `renderer` module will read input via `stdin`, pass the input as a string to the `render` function from the `markdown` module, and then print the returned HTML to `stdout`.

To build the `renderer` module:

```text
$ cd demo/renderer
$ cargo wasi build
```

This module will read input via `stdin`, pass the input as a string to the `render` function from the `markdown` module, and then print the returned HTML to `stdout`.
### Linking the two modules together

With the two modules now built, it is time to link them together so that they can be run directly with [Wasmtime](https://github.com/bytecodealliance/wasmtime):

```text
$ cargo run --release -- -i markdown=demo/markdown/target/wasm32-wasi/debug/markdown.wasm -p wasmtime -o linked.wasm demo/renderer/target/wasm32-wasi/debug/renderer.wasm
```

This command produces a linked module `linked.wasm` that we can now run directly with Wasmtime:
This command produces a linked module named `linked.wasm` in the current directory.

### Running the linked module

As the linked module uses features from both the [module linking](https://github.com/WebAssembly/module-linking) and [multi-memory](https://github.com/WebAssembly/multi-memory) WebAssembly proposals, support has to be explicitly enabled in Wasmtime to enable the module to run.

To run the linked module:

```text
$ echo '# Hello\nworld' | wasmtime --enable-module-linking --enable-multi-memory linked.wasm
```

As the linked module uses features from both the [module linking](https://github.com/WebAssembly/module-linking) and [multi-memory](https://github.com/WebAssembly/multi-memory) WebAssembly proposals, support has to be explicitly enabled in Wasmtime to enable the module to run.

If everything worked correctly, this should render the Markdown:
If everything worked correctly, this should render the Markdown echoed on the command line:

```markdown
# Hello
Expand Down
13 changes: 13 additions & 0 deletions crates/test-modules/Cargo.toml
@@ -0,0 +1,13 @@
[package]
name = "test-modules"
version = "0.1.0"
authors = ["Peter Huene <peter@huene.dev>"]
edition = "2018"

[dependencies]
anyhow = "1.0.40"
wasmlink = { path = "../wasmlink" }

[dev-dependencies]
wasmtime = "0.27.0"
wasmtime-wasi = "0.27.0"
38 changes: 38 additions & 0 deletions crates/test-modules/build.rs
@@ -0,0 +1,38 @@
use std::{
io,
process::{Command, Stdio},
};

fn main() {
println!("cargo:rerun-if-changed=modules/crates");
println!("cargo:rerun-if-changed=modules/Cargo.toml");
println!("cargo:rerun-if-changed=modules/Cargo.lock");

build_modules().unwrap();
}

fn build_modules() -> io::Result<()> {
let mut cmd = Command::new("cargo");

cmd.args(&["build", "--target=wasm32-wasi"]);

if !cfg!(debug_assertions) {
cmd.arg("--release");
}

cmd.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.current_dir("modules");

let output = cmd.output()?;

let status = output.status;
if !status.success() {
panic!(
"Building tests failed: exit code: {}",
status.code().unwrap()
);
}

Ok(())
}
178 changes: 178 additions & 0 deletions crates/test-modules/modules/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/test-modules/modules/Cargo.toml
@@ -0,0 +1,5 @@
[workspace]
members = [
"crates/types",
"crates/types-main"
]
8 changes: 8 additions & 0 deletions crates/test-modules/modules/crates/types-main/Cargo.toml
@@ -0,0 +1,8 @@
[package]
name = "types-main"
version = "0.1.0"
authors = ["Peter Huene <peter@huene.dev>"]
edition = "2018"

[dependencies]
witx-bindgen-rust = { git = "https://github.com/bytecodealliance/witx-bindgen", branch = "main" }
12 changes: 12 additions & 0 deletions crates/test-modules/modules/crates/types-main/src/main.rs
@@ -0,0 +1,12 @@
witx_bindgen_rust::import!("crates/types/types.witx");

fn main() {
types::a();
assert_eq!(types::b(1, 2, 3, 4, 5, 6, 7, 8), (1, 2, 3, 4, 5, 6, 7, 8));
assert_eq!(types::c(1.7, 2.6), (1.7, 2.6));
assert_eq!(types::d("this is a string"), "this is a string");
assert_eq!(types::e(), "hello world!");

let (a, b, c) = types::f();
assert_eq!((a, b.as_str(), c), (13, "hi", 37));
}
11 changes: 11 additions & 0 deletions crates/test-modules/modules/crates/types/Cargo.toml
@@ -0,0 +1,11 @@
[package]
name = "types"
version = "0.1.0"
authors = ["Peter Huene <peter@huene.dev>"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
witx-bindgen-rust = { git = "https://github.com/bytecodealliance/witx-bindgen", branch = "main" }