Skip to content

Commit

Permalink
setup component tests infra
Browse files Browse the repository at this point in the history
also add `cargo component` to README and CI workflows
  • Loading branch information
tessi committed Nov 10, 2024
1 parent 1d551aa commit 6f83c56
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/compatibility-elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ jobs:
with:
toolchain: stable

- name: Add targets
- name: Add targets and install cargo-component
run: |
rustup target add wasm32-unknown-unknown
rustup target add wasm32-wasip1
cargo install cargo-component
- run: mix do deps.get, deps.compile

- run: mix test
2 changes: 2 additions & 0 deletions .github/workflows/elixir-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
with:
toolchain: stable

- run: cargo install cargo-component

- id: get-cache-key
run: echo "key=mix-${{ env.ELIXIR_VERSION }}-${{ env.OTP_VERSION }}-${{ hashFiles('**/mix.lock') }}" >> $GITHUB_OUTPUT

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ rustup component add rustfmt
rustup component add clippy
rustup target add wasm32-unknown-unknown # to compile our example Wasm files for testing
rustup target add wasm32-wasip1 # to compile our example Wasm/WASI files for testing
cargo install cargo-component # to run component tests
```

Then install the erlang/elixir dependencies:
Expand Down
2 changes: 2 additions & 0 deletions test/fixture_projects/component_types/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-unknown-unknown"
2 changes: 2 additions & 0 deletions test/fixture_projects/component_types/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cargo.lock
src/bindings.rs
10 changes: 10 additions & 0 deletions test/fixture_projects/component_types/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"rust-analyzer.check.overrideCommand": [
"cargo",
"component",
"check",
"--workspace",
"--all-targets",
"--message-format=json"
]
}
20 changes: 20 additions & 0 deletions test/fixture_projects/component_types/.zed/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Folder-specific settings
//
// For a full list of overridable settings, and general information on folder-specific settings,
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
{
"lsp": {
"rust-analyzer": {
"check": {
"overrideCommand": [
"cargo",
"component",
"check",
"--workspace",
"--all-targets",
"--message-format=json"
]
}
}
}
}
23 changes: 23 additions & 0 deletions test/fixture_projects/component_types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "component-types"
version = "0.1.0"
edition = "2021"

[dependencies]
paste = "1.0.15"
wit-bindgen-rt = { version = "0.33.0", features = ["bitflags"] }

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

[profile.release]
codegen-units = 1
opt-level = "s"
debug = false
strip = true
lto = true

[package.metadata.component]
package = "fixtures:component-types"

[package.metadata.component.dependencies]
10 changes: 10 additions & 0 deletions test/fixture_projects/component_types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Wasm component fixture to test converting types back and forth.

Prerequisite: `cargo install cargo-component`

To rebuild, run the following from this directory:
```
cargo component build --release
```

`mix test` will automatically compile the wasm file.
22 changes: 22 additions & 0 deletions test/fixture_projects/component_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[allow(warnings)]
mod bindings;

use bindings::Guest;
use paste::paste;

macro_rules! id_function {
($wasm_ty:ident, $rust_ty:ty) => {
paste! {
fn [<id_ $wasm_ty>](v: $rust_ty) -> $rust_ty {
v
}
}
};
}

struct Component;
impl Guest for Component {
id_function!(bool, bool);
}

bindings::export!(Component with_types_in bindings);
5 changes: 5 additions & 0 deletions test/fixture_projects/component_types/wit/world.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fixtures:component-types;

world fixtures {
export id-bool: func(v: bool) -> bool;
}
7 changes: 7 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ defmodule TestHelper do
@wasm_link_import_test_source_dir "#{@fixture_project_dir}/wasm_link_import_test"
@wasm_import_test_source_dir "#{@fixture_project_dir}/wasm_import_test"
@wasi_test_source_dir "#{@fixture_project_dir}/wasi_test"
@component_test_source_dir "#{@fixture_project_dir}/component_types"

def component_test_file_path,
do: "#{@component_test_source_dir}/target/wasm32-unknown-unknown/release/component_types.wasm"

def wasm_test_file_path,
do: "#{@wasm_test_source_dir}/target/wasm32-unknown-unknown/debug/wasmex_test.wasm"
Expand All @@ -28,6 +32,9 @@ defmodule TestHelper do
do: "#{@wasi_test_source_dir}/target/wasm32-wasip1/debug/main.wasm"

def precompile_wasm_files do
{"", 0} =
System.cmd("cargo", ["component", "build", "--release"], cd: @component_test_source_dir)

{"", 0} = System.cmd("cargo", ["build"], cd: @wasm_test_source_dir)
{"", 0} = System.cmd("cargo", ["build"], cd: @wasm_import_test_source_dir)
{"", 0} = System.cmd("cargo", ["build"], cd: @wasm_link_import_test_source_dir)
Expand Down

0 comments on commit 6f83c56

Please sign in to comment.