From ca09b8ed909096f0445cb138848a50577ca0db54 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 08:38:15 +0000 Subject: [PATCH] test(runtime): serialise tests that use WEFT_APP_STORE One test set WEFT_APP_STORE while another, running on a parallel thread, expected the default store roots, so the default-roots test failed intermittently (5 of 60 local runs, and in CI). The four tests that read or change the variable now share a lock, and the default-roots test clears the variable for its duration (0 of 30 runs fail). --- crates/weft-runtime/src/main.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs index e50d4de..6d30105 100644 --- a/crates/weft-runtime/src/main.rs +++ b/crates/weft-runtime/src/main.rs @@ -507,6 +507,14 @@ fn package_store_roots() -> Vec { mod tests { use super::*; + /// Serialises the tests that read or change WEFT_APP_STORE; the test + /// harness runs tests on parallel threads that share the environment. + static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + fn env_lock() -> std::sync::MutexGuard<'static, ()> { + ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner()) + } + #[cfg(not(feature = "wasmtime-runtime"))] #[test] fn engine_disabled_build_refuses_to_run_components() { @@ -517,7 +525,14 @@ mod tests { #[test] fn package_store_roots_includes_system_path() { + let _env = env_lock(); + let prior = std::env::var_os("WEFT_APP_STORE"); + // SAFETY: env_lock serialises every test that touches the environment. + unsafe { std::env::remove_var("WEFT_APP_STORE") }; let roots = package_store_roots(); + if let Some(v) = prior { + unsafe { std::env::set_var("WEFT_APP_STORE", v) }; + } assert!( roots .iter() @@ -527,6 +542,7 @@ mod tests { #[test] fn package_store_roots_uses_weft_app_store_when_set() { + let _env = env_lock(); // SAFETY: test binary is single-threaded at this point. unsafe { std::env::set_var("WEFT_APP_STORE", "/custom/store") }; let roots = package_store_roots(); @@ -536,6 +552,7 @@ mod tests { #[test] fn resolve_package_finds_installed_package() { + let _env = env_lock(); use std::fs; let store = std::env::temp_dir().join(format!("weft_runtime_resolve_{}", std::process::id())); @@ -566,6 +583,7 @@ mod tests { #[test] fn resolve_package_errors_on_unknown_id() { + let _env = env_lock(); let store = std::env::temp_dir().join(format!("weft_runtime_resolve_empty_{}", std::process::id())); let _ = std::fs::create_dir_all(&store);