diff --git a/Cargo.toml b/Cargo.toml index e506f9bde4..04a961ec01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = "." } diff --git a/build.rs b/build.rs index c7113d5a29..ff492bdf03 100644 --- a/build.rs +++ b/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, diff --git a/tests/runtime.rs b/tests/runtime.rs index 6f0db720d9..9c0976166f 100644 --- a/tests/runtime.rs +++ b/tests/runtime.rs @@ -1,11 +1,8 @@ use cranelift_codegen::settings; +use std::path::Path; use wasmtime_jit::Context; -pub fn instantiate>( - data: &[u8], - bin_name: S, - workspace: Option, -) -> 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>( let mut context = Context::with_isa(isa); let global_exports = context.get_global_exports(); - let get_preopens = |workspace: Option| -> Result, String> { + let get_preopens = |workspace: Option<&Path>| -> Result, 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>( "", 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>( 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)) } diff --git a/tests/utils.rs b/tests/utils.rs index 1b702e08f5..160412d93a 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -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>(path: P) -> Result, String> { +pub fn read_wasm(path: &Path) -> Result, 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>(path: P) -> Result, String> { } } -pub fn prepare_workspace>(exe_name: S) -> Result { - 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 { + 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>(path: P) -> Result { - 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 { + 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() + )) }