-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wit-component): expose wit parsing and wasm detection functions (#…
…1232) * feat(wit-component): expose wit parsing and wasm detection functions WIT parsing and wasm detection functions were used inside wasm-tools but not available to downstream consumers. This commit refactors the code slightly and puts it in wit-component, and makes two functions available (wit_component::parse_wit_from_path, wit_component::is_wasm_binary_or_wat) for downstream consumers of wit-component to use Signed-off-by: Victor Adossi <vadossi@cosmonic.com> * fix(tests): remove canonicalize usage Signed-off-by: Victor Adossi <vadossi@cosmonic.com> --------- Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
- Loading branch information
1 parent
25a6916
commit 67fb7b0
Showing
7 changed files
with
186 additions
and
71 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use anyhow::Result; | ||
use wit_component::{is_wasm_binary_or_wat, parse_wit_from_path}; | ||
|
||
const EXAMPLE_MODULE_WAT: &str = r#" | ||
(module | ||
(type (;0;) (func)) | ||
(func (;0;) (type 0) | ||
nop | ||
) | ||
) | ||
"#; | ||
|
||
/// Ensure that parse_wit_from_path works with directories | ||
#[test] | ||
fn parse_wit_dir() -> Result<()> { | ||
drop(env_logger::try_init()); | ||
|
||
let (resolver, package_id) = parse_wit_from_path("tests/wit/parse-dir/wit")?; | ||
assert!(resolver | ||
.select_world(package_id, "foo-world".into()) | ||
.is_ok()); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Ensure that parse_wit_from_path works for a single file | ||
#[test] | ||
fn parse_wit_file() -> Result<()> { | ||
drop(env_logger::try_init()); | ||
|
||
let (resolver, package_id) = parse_wit_from_path("tests/wit/parse-dir/wit/deps/bar/bar.wit")?; | ||
resolver.select_world(package_id, "bar-world".into())?; | ||
assert!(resolver | ||
.interfaces | ||
.iter() | ||
.any(|(_, iface)| iface.name == Some("bar".into()))); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Ensure that parse_with_from_path fails for missing paths | ||
#[test] | ||
fn parse_wit_missing_path() -> Result<()> { | ||
drop(env_logger::try_init()); | ||
|
||
assert!(parse_wit_from_path("tests/nonexistent/path").is_err()); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Ensure that is_wasm_binary_or_wat works for binaries | ||
#[test] | ||
fn check_wasm_from_bytes() -> Result<()> { | ||
drop(env_logger::try_init()); | ||
|
||
assert!(is_wasm_binary_or_wat(wat::parse_str(EXAMPLE_MODULE_WAT)?)); | ||
|
||
Ok(()) | ||
} |
9 changes: 9 additions & 0 deletions
9
crates/wit-component/tests/wit/parse-dir/wit/deps/bar/bar.wit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package foo:bar; | ||
|
||
interface bar { | ||
f: func() -> bool; | ||
} | ||
|
||
world bar-world { | ||
export bar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package foo:foo; | ||
|
||
world foo-world { | ||
import foo:bar/bar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters