Wrap env::set_var/remove_var in unsafe blocks in WASI tests

These functions are deprecated as safe in recent Rust versions because
they are not thread-safe. Wrapping them in unsafe blocks with safety
comments documents that the tests run single-threaded.
This commit is contained in:
Sylvestre Ledru
2026-03-30 16:58:34 +02:00
parent e0b78e3963
commit fdcd14f72d
+15 -6
View File
@@ -115,19 +115,28 @@ fn test_wasi() {
fn test_wasi_nodename_env() {
let orig = std::env::var("HOSTNAME").ok();
std::env::set_var("HOSTNAME", "my-wasi-host");
// SAFETY: This test runs single-threaded (--test-threads 1) so env mutation is safe.
unsafe {
std::env::set_var("HOSTNAME", "my-wasi-host");
}
let info = PlatformInfo::new().unwrap();
assert_eq!(info.nodename().to_string_lossy(), "my-wasi-host");
std::env::remove_var("HOSTNAME");
std::env::remove_var("COMPUTERNAME");
// SAFETY: This test runs single-threaded (--test-threads 1) so env mutation is safe.
unsafe {
std::env::remove_var("HOSTNAME");
std::env::remove_var("COMPUTERNAME");
}
let info = PlatformInfo::new().unwrap();
assert_eq!(info.nodename().to_string_lossy(), "localhost");
// Restore
match orig {
Some(v) => std::env::set_var("HOSTNAME", v),
None => std::env::remove_var("HOSTNAME"),
// SAFETY: This test runs single-threaded (--test-threads 1) so env mutation is safe.
unsafe {
match orig {
Some(v) => std::env::set_var("HOSTNAME", v),
None => std::env::remove_var("HOSTNAME"),
}
}
}