Refactor mgmt of misc testsuite (#101)
Changes: * use [tempfile] crate for auto mgmt of temp dirs * use concrete types in place of generics in `utils` module [tempfile]: https://github.com/Stebalien/tempfile
This commit is contained in:
@@ -37,6 +37,7 @@ cranelift-wasm = "0.41.0"
|
|||||||
cranelift-native = "0.41.0"
|
cranelift-native = "0.41.0"
|
||||||
target-lexicon = "0.4.0"
|
target-lexicon = "0.4.0"
|
||||||
pretty_env_logger = "0.3.0"
|
pretty_env_logger = "0.3.0"
|
||||||
|
tempfile = "3.1.0"
|
||||||
|
|
||||||
[patch."https://github.com/CraneStation/wasi-common"]
|
[patch."https://github.com/CraneStation/wasi-common"]
|
||||||
wasi-common = { path = "." }
|
wasi-common = { path = "." }
|
||||||
|
|||||||
8
build.rs
8
build.rs
@@ -113,7 +113,7 @@ fn write_testsuite_tests(out: &mut File, dir_entry: DirEntry, testsuite: &str) -
|
|||||||
" fn {}() -> Result<(), String> {{",
|
" fn {}() -> Result<(), String> {{",
|
||||||
avoid_keywords(&stemstr.replace("-", "_"))
|
avoid_keywords(&stemstr.replace("-", "_"))
|
||||||
)?;
|
)?;
|
||||||
write!(out, " setup_log();")?;
|
writeln!(out, " setup_log();")?;
|
||||||
write!(out, " let path = std::path::Path::new(\"")?;
|
write!(out, " let path = std::path::Path::new(\"")?;
|
||||||
// Write out the string with escape_debug to prevent special characters such
|
// Write out the string with escape_debug to prevent special characters such
|
||||||
// as backslash from being reinterpreted.
|
// as backslash from being reinterpreted.
|
||||||
@@ -129,7 +129,11 @@ fn write_testsuite_tests(out: &mut File, dir_entry: DirEntry, testsuite: &str) -
|
|||||||
let workspace = if no_preopens(testsuite, stemstr) {
|
let workspace = if no_preopens(testsuite, stemstr) {
|
||||||
"None"
|
"None"
|
||||||
} else {
|
} else {
|
||||||
"Some(&utils::prepare_workspace(&bin_name)?)"
|
writeln!(
|
||||||
|
out,
|
||||||
|
" let workspace = utils::prepare_workspace(&bin_name)?;"
|
||||||
|
)?;
|
||||||
|
"Some(workspace.path())"
|
||||||
};
|
};
|
||||||
writeln!(
|
writeln!(
|
||||||
out,
|
out,
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
use cranelift_codegen::settings;
|
use cranelift_codegen::settings;
|
||||||
|
use std::path::Path;
|
||||||
use wasmtime_jit::Context;
|
use wasmtime_jit::Context;
|
||||||
|
|
||||||
pub fn instantiate<S: AsRef<str>>(
|
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> Result<(), String> {
|
||||||
data: &[u8],
|
|
||||||
bin_name: S,
|
|
||||||
workspace: Option<S>,
|
|
||||||
) -> Result<(), String> {
|
|
||||||
// Prepare runtime
|
// Prepare runtime
|
||||||
let isa_builder =
|
let isa_builder =
|
||||||
cranelift_native::builder().map_err(|_| "host machine is not a supported target")?;
|
cranelift_native::builder().map_err(|_| "host machine is not a supported target")?;
|
||||||
@@ -14,12 +11,12 @@ pub fn instantiate<S: AsRef<str>>(
|
|||||||
let mut context = Context::with_isa(isa);
|
let mut context = Context::with_isa(isa);
|
||||||
let global_exports = context.get_global_exports();
|
let global_exports = context.get_global_exports();
|
||||||
|
|
||||||
let get_preopens = |workspace: Option<S>| -> Result<Vec<_>, String> {
|
let get_preopens = |workspace: Option<&Path>| -> Result<Vec<_>, String> {
|
||||||
if let Some(workspace) = workspace {
|
if let Some(workspace) = workspace {
|
||||||
let preopen_dir = wasi_common::preopen_dir(workspace.as_ref()).map_err(|e| {
|
let preopen_dir = wasi_common::preopen_dir(workspace).map_err(|e| {
|
||||||
format!(
|
format!(
|
||||||
"error while preopening directory '{}': {}",
|
"error while preopening directory '{}': {}",
|
||||||
workspace.as_ref(),
|
workspace.display(),
|
||||||
e
|
e
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
@@ -36,7 +33,7 @@ pub fn instantiate<S: AsRef<str>>(
|
|||||||
"",
|
"",
|
||||||
global_exports,
|
global_exports,
|
||||||
&get_preopens(workspace)?,
|
&get_preopens(workspace)?,
|
||||||
&[bin_name.as_ref().to_owned(), ".".to_owned()],
|
&[bin_name.to_owned(), ".".to_owned()],
|
||||||
&[],
|
&[],
|
||||||
)
|
)
|
||||||
.map_err(|e| format!("error instantiating WASI: {}", e))?,
|
.map_err(|e| format!("error instantiating WASI: {}", e))?,
|
||||||
@@ -46,11 +43,5 @@ pub fn instantiate<S: AsRef<str>>(
|
|||||||
context
|
context
|
||||||
.instantiate_module(None, &data)
|
.instantiate_module(None, &data)
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|e| {
|
.map_err(|e| format!("error while processing main module '{}': {}", bin_name, e))
|
||||||
format!(
|
|
||||||
"error while processing main module '{}': {}",
|
|
||||||
bin_name.as_ref(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
use std::env;
|
|
||||||
use std::ffi::OsStr;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Component, Path};
|
use std::path::Path;
|
||||||
use std::time::SystemTime;
|
use tempfile::{Builder, TempDir};
|
||||||
|
|
||||||
pub fn read_wasm<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, String> {
|
pub fn read_wasm(path: &Path) -> Result<Vec<u8>, String> {
|
||||||
let data = fs::read(path).map_err(|err| err.to_string())?;
|
let data = fs::read(path).map_err(|err| err.to_string())?;
|
||||||
if data.starts_with(&[b'\0', b'a', b's', b'm']) {
|
if data.starts_with(&[b'\0', b'a', b's', b'm']) {
|
||||||
Ok(data)
|
Ok(data)
|
||||||
@@ -13,33 +11,20 @@ pub fn read_wasm<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prepare_workspace<S: AsRef<str>>(exe_name: S) -> Result<String, String> {
|
pub fn prepare_workspace(exe_name: &str) -> Result<TempDir, String> {
|
||||||
let mut workspace = env::temp_dir();
|
let prefix = format!("wasi_common_{}", exe_name);
|
||||||
let time_now = SystemTime::now()
|
Builder::new()
|
||||||
.duration_since(SystemTime::UNIX_EPOCH)
|
.prefix(&prefix)
|
||||||
.map_err(|err| err.to_string())?;
|
.tempdir()
|
||||||
let subdir = format!(
|
.map_err(|e| format!("couldn't create workspace in temp files: {}", e))
|
||||||
"wasi_common_tests_{}_{}",
|
|
||||||
exe_name.as_ref(),
|
|
||||||
time_now.as_secs()
|
|
||||||
);
|
|
||||||
workspace.push(subdir);
|
|
||||||
fs::create_dir(workspace.as_path()).map_err(|err| err.to_string())?;
|
|
||||||
|
|
||||||
Ok(workspace
|
|
||||||
.as_os_str()
|
|
||||||
.to_str()
|
|
||||||
.ok_or("couldn't convert to str".to_owned())?
|
|
||||||
.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_exec_name_from_path<P: AsRef<Path>>(path: P) -> Result<String, String> {
|
pub fn extract_exec_name_from_path(path: &Path) -> Result<String, String> {
|
||||||
Ok(path
|
path.file_stem()
|
||||||
.as_ref()
|
.and_then(|s| s.to_str())
|
||||||
.components()
|
.map(String::from)
|
||||||
.next_back()
|
.ok_or(format!(
|
||||||
.map(Component::as_os_str)
|
"couldn't extract the file stem from path {}",
|
||||||
.and_then(OsStr::to_str)
|
path.display()
|
||||||
.ok_or("couldn't convert to str".to_owned())?
|
))
|
||||||
.to_owned())
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user