#![allow(
clippy::missing_docs_in_private_items,
reason = "test helpers and fixtures do not need doc comments"
)]
use super::*;
fn with_redirected_home(body: impl FnOnce(&std::path::Path)) {
let home = std::env::temp_dir().join(format!("moadim-defaults-lock-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&home).unwrap();
let previous_home = std::env::var_os("HOME");
let previous_xdg = std::env::var_os("XDG_CONFIG_HOME");
unsafe {
std::env::set_var("HOME", &home);
std::env::set_var("XDG_CONFIG_HOME", home.join(".config"));
}
body(&home);
unsafe {
match previous_home {
Some(value) => std::env::set_var("HOME", value),
None => std::env::remove_var("HOME"),
}
match previous_xdg {
Some(value) => std::env::set_var("XDG_CONFIG_HOME", value),
None => std::env::remove_var("XDG_CONFIG_HOME"),
}
}
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn concurrent_record_removed_default_calls_do_not_clobber_each_other() {
with_redirected_home(|_home| {
let barrier = std::sync::Arc::new(std::sync::Barrier::new(2));
let b1 = std::sync::Arc::clone(&barrier);
let t1 = std::thread::spawn(move || {
b1.wait();
record_removed_default("racer-one");
});
let b2 = std::sync::Arc::clone(&barrier);
let t2 = std::thread::spawn(move || {
b2.wait();
record_removed_default("racer-two");
});
t1.join().unwrap();
t2.join().unwrap();
let slugs = read_removed_defaults();
assert!(
slugs.contains("racer-one"),
"concurrent tombstone for racer-two must not drop racer-one"
);
assert!(
slugs.contains("racer-two"),
"concurrent tombstone for racer-one must not drop racer-two"
);
});
}