Files
wasmtime/crates/test-programs/tests/wasm_tests/utils.rs
Dan Gohman d4fd229e5e Reorganize wasi-misc-tests. (#575)
* Reorganize wasi-misc-tests.

Move wasi-misc-tests out of wasi-common, to break a dependency cycle;
previously, wasmtime-* depended on wasi-common, but wasi-common
dev-dependended on wasmtime-*.

Now, wasi-common no longer dev-depends on wasmtime-*; instead, the
tests are in their own crate which depends on wasi-common and on
wasmtime-*.

Also, rename wasi-misc-tests to wasi-tests for simplicity.

This also removes the "wasm_tests" feature; it's replaced by the
"test-programs" feature.

* Update the CI script to use the new feature name.

* Update the CI script to use the new feature name in one more place.

* Change a `write!` to a `writeln!`.
2019-11-15 08:03:43 -08:00

31 lines
845 B
Rust

use std::fs;
use std::path::Path;
use tempfile::{Builder, TempDir};
pub fn read_wasm(path: &Path) -> anyhow::Result<Vec<u8>> {
let data = fs::read(path)?;
if data.starts_with(&[b'\0', b'a', b's', b'm']) {
Ok(data)
} else {
anyhow::bail!("Invalid Wasm file encountered")
}
}
pub fn prepare_workspace(exe_name: &str) -> anyhow::Result<TempDir> {
let prefix = format!("wasi_common_{}", exe_name);
let tempdir = Builder::new().prefix(&prefix).tempdir()?;
Ok(tempdir)
}
pub fn extract_exec_name_from_path(path: &Path) -> anyhow::Result<String> {
path.file_stem()
.and_then(|s| s.to_str())
.map(String::from)
.ok_or_else(|| {
anyhow::anyhow!(
"couldn't extract the file stem from path {}",
path.display()
)
})
}