diff --git a/.github/workflows/compatibility-elixir.yml b/.github/workflows/compatibility-elixir.yml index c123368..d17a6c8 100644 --- a/.github/workflows/compatibility-elixir.yml +++ b/.github/workflows/compatibility-elixir.yml @@ -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 diff --git a/.github/workflows/elixir-ci.yml b/.github/workflows/elixir-ci.yml index 1ec6fe6..7b433d3 100644 --- a/.github/workflows/elixir-ci.yml +++ b/.github/workflows/elixir-ci.yml @@ -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 diff --git a/README.md b/README.md index df68f96..019764d 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/test/fixture_projects/component_types/.cargo/config.toml b/test/fixture_projects/component_types/.cargo/config.toml new file mode 100644 index 0000000..f4e8c00 --- /dev/null +++ b/test/fixture_projects/component_types/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-unknown-unknown" diff --git a/test/fixture_projects/component_types/.gitignore b/test/fixture_projects/component_types/.gitignore new file mode 100644 index 0000000..50bfc5c --- /dev/null +++ b/test/fixture_projects/component_types/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +src/bindings.rs diff --git a/test/fixture_projects/component_types/.vscode/settings.json b/test/fixture_projects/component_types/.vscode/settings.json new file mode 100644 index 0000000..b383e0d --- /dev/null +++ b/test/fixture_projects/component_types/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "rust-analyzer.check.overrideCommand": [ + "cargo", + "component", + "check", + "--workspace", + "--all-targets", + "--message-format=json" + ] +} diff --git a/test/fixture_projects/component_types/.zed/settings.json b/test/fixture_projects/component_types/.zed/settings.json new file mode 100644 index 0000000..3a896ae --- /dev/null +++ b/test/fixture_projects/component_types/.zed/settings.json @@ -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" + ] + } + } + } +} diff --git a/test/fixture_projects/component_types/Cargo.toml b/test/fixture_projects/component_types/Cargo.toml new file mode 100644 index 0000000..ae14a96 --- /dev/null +++ b/test/fixture_projects/component_types/Cargo.toml @@ -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] diff --git a/test/fixture_projects/component_types/README.md b/test/fixture_projects/component_types/README.md new file mode 100644 index 0000000..ca4938d --- /dev/null +++ b/test/fixture_projects/component_types/README.md @@ -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. diff --git a/test/fixture_projects/component_types/src/lib.rs b/test/fixture_projects/component_types/src/lib.rs new file mode 100644 index 0000000..f2d148f --- /dev/null +++ b/test/fixture_projects/component_types/src/lib.rs @@ -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 [](v: $rust_ty) -> $rust_ty { + v + } + } + }; +} + +struct Component; +impl Guest for Component { + id_function!(bool, bool); +} + +bindings::export!(Component with_types_in bindings); diff --git a/test/fixture_projects/component_types/wit/world.wit b/test/fixture_projects/component_types/wit/world.wit new file mode 100644 index 0000000..e8e940e --- /dev/null +++ b/test/fixture_projects/component_types/wit/world.wit @@ -0,0 +1,5 @@ +package fixtures:component-types; + +world fixtures { + export id-bool: func(v: bool) -> bool; +} diff --git a/test/test_helper.exs b/test/test_helper.exs index b5e84d3..e749ff1 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -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" @@ -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)