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"
|
||||
target-lexicon = "0.4.0"
|
||||
pretty_env_logger = "0.3.0"
|
||||
tempfile = "3.1.0"
|
||||
|
||||
[patch."https://github.com/CraneStation/wasi-common"]
|
||||
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> {{",
|
||||
avoid_keywords(&stemstr.replace("-", "_"))
|
||||
)?;
|
||||
write!(out, " setup_log();")?;
|
||||
writeln!(out, " setup_log();")?;
|
||||
write!(out, " let path = std::path::Path::new(\"")?;
|
||||
// Write out the string with escape_debug to prevent special characters such
|
||||
// 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) {
|
||||
"None"
|
||||
} else {
|
||||
"Some(&utils::prepare_workspace(&bin_name)?)"
|
||||
writeln!(
|
||||
out,
|
||||
" let workspace = utils::prepare_workspace(&bin_name)?;"
|
||||
)?;
|
||||
"Some(workspace.path())"
|
||||
};
|
||||
writeln!(
|
||||
out,
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use cranelift_codegen::settings;
|
||||
use std::path::Path;
|
||||
use wasmtime_jit::Context;
|
||||
|
||||
pub fn instantiate<S: AsRef<str>>(
|
||||
data: &[u8],
|
||||
bin_name: S,
|
||||
workspace: Option<S>,
|
||||
) -> Result<(), String> {
|
||||
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> Result<(), String> {
|
||||
// Prepare runtime
|
||||
let isa_builder =
|
||||
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 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 {
|
||||
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!(
|
||||
"error while preopening directory '{}': {}",
|
||||
workspace.as_ref(),
|
||||
workspace.display(),
|
||||
e
|
||||
)
|
||||
})?;
|
||||
@@ -36,7 +33,7 @@ pub fn instantiate<S: AsRef<str>>(
|
||||
"",
|
||||
global_exports,
|
||||
&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))?,
|
||||
@@ -46,11 +43,5 @@ pub fn instantiate<S: AsRef<str>>(
|
||||
context
|
||||
.instantiate_module(None, &data)
|
||||
.map(|_| ())
|
||||
.map_err(|e| {
|
||||
format!(
|
||||
"error while processing main module '{}': {}",
|
||||
bin_name.as_ref(),
|
||||
e
|
||||
)
|
||||
})
|
||||
.map_err(|e| format!("error while processing main module '{}': {}", bin_name, e))
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs;
|
||||
use std::path::{Component, Path};
|
||||
use std::time::SystemTime;
|
||||
use std::path::Path;
|
||||
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())?;
|
||||
if data.starts_with(&[b'\0', b'a', b's', b'm']) {
|
||||
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> {
|
||||
let mut workspace = env::temp_dir();
|
||||
let time_now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|err| err.to_string())?;
|
||||
let subdir = format!(
|
||||
"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 prepare_workspace(exe_name: &str) -> Result<TempDir, String> {
|
||||
let prefix = format!("wasi_common_{}", exe_name);
|
||||
Builder::new()
|
||||
.prefix(&prefix)
|
||||
.tempdir()
|
||||
.map_err(|e| format!("couldn't create workspace in temp files: {}", e))
|
||||
}
|
||||
|
||||
pub fn extract_exec_name_from_path<P: AsRef<Path>>(path: P) -> Result<String, String> {
|
||||
Ok(path
|
||||
.as_ref()
|
||||
.components()
|
||||
.next_back()
|
||||
.map(Component::as_os_str)
|
||||
.and_then(OsStr::to_str)
|
||||
.ok_or("couldn't convert to str".to_owned())?
|
||||
.to_owned())
|
||||
pub fn extract_exec_name_from_path(path: &Path) -> Result<String, String> {
|
||||
path.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.map(String::from)
|
||||
.ok_or(format!(
|
||||
"couldn't extract the file stem from path {}",
|
||||
path.display()
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user