internal: Simplify ratoml testdir usage

This commit is contained in:
Lukas Wirth 2024-12-16 10:01:35 +01:00
parent 6c6779e6f0
commit 1b27ba86eb

View file

@ -1,7 +1,7 @@
use std::{ use std::{
cell::{Cell, RefCell}, cell::{Cell, RefCell},
env, fs, env, fs,
sync::{Once, OnceLock}, sync::Once,
time::Duration, time::Duration,
}; };
@ -141,34 +141,15 @@ impl Project<'_> {
/// file in the config dir after server is run, something where our naive approach comes short. /// file in the config dir after server is run, something where our naive approach comes short.
/// Using a `prelock` allows us to force a lock when we know we need it. /// Using a `prelock` allows us to force a lock when we know we need it.
pub(crate) fn server_with_lock(self, config_lock: bool) -> Server { pub(crate) fn server_with_lock(self, config_lock: bool) -> Server {
static CONFIG_DIR_LOCK: OnceLock<(Utf8PathBuf, Mutex<()>)> = OnceLock::new(); static CONFIG_DIR_LOCK: Mutex<()> = Mutex::new(());
let config_dir_guard = if config_lock { let config_dir_guard = if config_lock {
Some({ Some({
let (path, mutex) = CONFIG_DIR_LOCK.get_or_init(|| { let guard = CONFIG_DIR_LOCK.lock();
let value = TestDir::new().keep().path().to_owned(); let test_dir = TestDir::new();
let value = test_dir.path().to_owned();
env::set_var("__TEST_RA_USER_CONFIG_DIR", &value); env::set_var("__TEST_RA_USER_CONFIG_DIR", &value);
(value, Mutex::new(())) (guard, test_dir)
});
#[allow(dyn_drop)]
(mutex.lock(), {
Box::new({
struct Dropper(Utf8PathBuf);
impl Drop for Dropper {
fn drop(&mut self) {
for entry in fs::read_dir(&self.0).unwrap() {
let path = entry.unwrap().path();
if path.is_file() {
fs::remove_file(path).unwrap();
} else if path.is_dir() {
fs::remove_dir_all(path).unwrap();
}
}
}
}
Dropper(path.clone())
}) as Box<dyn Drop>
})
}) })
} else { } else {
None None
@ -311,14 +292,12 @@ pub(crate) struct Server {
client: Connection, client: Connection,
/// XXX: remove the tempdir last /// XXX: remove the tempdir last
dir: TestDir, dir: TestDir,
#[allow(dyn_drop)] _config_dir_guard: Option<(MutexGuard<'static, ()>, TestDir)>,
_config_dir_guard: Option<(MutexGuard<'static, ()>, Box<dyn Drop>)>,
} }
impl Server { impl Server {
#[allow(dyn_drop)]
fn new( fn new(
config_dir_guard: Option<(MutexGuard<'static, ()>, Box<dyn Drop>)>, config_dir_guard: Option<(MutexGuard<'static, ()>, TestDir)>,
dir: TestDir, dir: TestDir,
config: Config, config: Config,
) -> Server { ) -> Server {