#[cfg(test)]
use std::cell::RefCell;
use std::path::PathBuf;
use crate::constants::APP_NAME;
const THEMES_DIRNAME: &str = "themes";
#[must_use]
pub(crate) fn themes_dir() -> Option<PathBuf> {
#[cfg(test)]
if let Some(path) = THEMES_DIR_OVERRIDE.with(|slot| slot.borrow().clone()) {
return Some(path);
}
dirs::config_dir().map(|d| d.join(APP_NAME).join(THEMES_DIRNAME))
}
#[cfg(test)]
thread_local! {
static THEMES_DIR_OVERRIDE: RefCell<Option<PathBuf>> = const {
RefCell::new(None)
};
}
#[cfg(test)]
pub(crate) struct ThemesDirOverrideGuard {
previous: Option<PathBuf>,
}
#[cfg(test)]
impl Drop for ThemesDirOverrideGuard {
fn drop(&mut self) {
let previous = self.previous.take();
THEMES_DIR_OVERRIDE.with(|slot| {
*slot.borrow_mut() = previous;
});
}
}
#[cfg(test)]
pub(crate) fn set_themes_dir_override_for_test(path: PathBuf) -> ThemesDirOverrideGuard {
let previous = THEMES_DIR_OVERRIDE.with(|slot| slot.replace(Some(path)));
ThemesDirOverrideGuard { previous }
}
#[cfg(test)]
#[allow(
clippy::expect_used,
clippy::unwrap_used,
reason = "tests should panic on unexpected values"
)]
mod tests {
use super::*;
#[test]
fn themes_dir_override_routes_through_themes_dir() {
let dir = std::env::temp_dir().join(format!(
"cargo_port_themes_override_route_{}",
std::process::id()
));
let _guard = set_themes_dir_override_for_test(dir.clone());
let resolved = themes_dir().expect("override returns Some");
assert_eq!(resolved, dir);
}
}