Path: blob/main/crates/test-programs/artifacts/src/lib.rs
3064 views
include!(concat!(env!("OUT_DIR"), "/gen.rs"));12use std::borrow::Cow;3use std::collections::HashMap;4use std::io::IsTerminal;5use std::sync::{Arc, Mutex};6use wasmtime::{CacheStore, Config, Engine};78/// The wasi-tests binaries use these environment variables to determine their9/// expected behavior.10/// Used by all of the tests/ which execute the wasi-tests binaries.11pub fn wasi_tests_environment() -> &'static [(&'static str, &'static str)] {12#[cfg(windows)]13{14&[15("ERRNO_MODE_WINDOWS", "1"),16// Windows does not support dangling links or symlinks in the filesystem.17("NO_DANGLING_FILESYSTEM", "1"),18// Windows does not support renaming a directory to an empty directory -19// empty directory must be deleted.20("NO_RENAME_DIR_TO_EMPTY_DIR", "1"),21("RENAME_DIR_ONTO_FILE", "1"),22]23}24#[cfg(all(unix, not(target_os = "macos")))]25{26&[("ERRNO_MODE_UNIX", "1")]27}28#[cfg(target_os = "macos")]29{30&[("ERRNO_MODE_MACOS", "1")]31}32}3334pub fn stdio_is_terminal() -> bool {35std::io::stdin().is_terminal()36&& std::io::stdout().is_terminal()37&& std::io::stderr().is_terminal()38}3940// Simple incremental cache used during tests to help improve test runtime.41//42// Many tests take a similar module (e.g. a component, a preview1 thing, sync,43// async, etc) and run it in different contexts and this improve cache hit rates44// across usages by sharing one incremental cache across tests.45fn cache_store() -> Arc<dyn CacheStore> {46#[derive(Debug)]47struct MyCache;4849static CACHE: Mutex<Option<HashMap<Vec<u8>, Vec<u8>>>> = Mutex::new(None);5051impl CacheStore for MyCache {52fn get(&self, key: &[u8]) -> Option<Cow<'_, [u8]>> {53let mut cache = CACHE.lock().unwrap();54let cache = cache.get_or_insert_with(HashMap::new);55cache.get(key).map(|s| s.to_vec().into())56}5758fn insert(&self, key: &[u8], value: Vec<u8>) -> bool {59let mut cache = CACHE.lock().unwrap();60let cache = cache.get_or_insert_with(HashMap::new);61cache.insert(key.to_vec(), value);62true63}64}6566Arc::new(MyCache)67}6869/// Helper to create an `Engine` with a pre-configured `Config` that uses a70/// cache for faster building of modules.71pub fn engine(configure: impl FnOnce(&mut Config)) -> Engine {72let mut config = Config::new();73config.wasm_component_model(true);74config75.enable_incremental_compilation(cache_store())76.unwrap();77configure(&mut config);78Engine::new(&config).unwrap()79}808182