use std::path::PathBuf;
pub const TEST_HOME_VAR: &str = "SCOPE_TEST_HOME";
#[allow(clippy::disallowed_methods)]
pub fn home_dir() -> Option<PathBuf> {
#[cfg(any(test, feature = "test-support"))]
return Some(sandbox());
#[cfg(not(any(test, feature = "test-support")))]
dirs::home_dir()
}
#[allow(clippy::disallowed_methods)]
pub fn config_dir() -> Option<PathBuf> {
#[cfg(any(test, feature = "test-support"))]
return Some(sandbox().join("config"));
#[cfg(not(any(test, feature = "test-support")))]
dirs::config_dir()
}
#[allow(clippy::disallowed_methods)]
pub fn data_dir() -> Option<PathBuf> {
#[cfg(any(test, feature = "test-support"))]
return Some(sandbox().join("data"));
#[cfg(not(any(test, feature = "test-support")))]
dirs::data_dir()
}
#[cfg(any(test, feature = "test-support"))]
fn sandbox() -> PathBuf {
std::env::var_os(TEST_HOME_VAR)
.map(PathBuf::from)
.unwrap_or_else(|| {
std::env::temp_dir().join(format!("scope-test-home-{}", std::process::id()))
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_test_builds_never_resolve_the_real_home() {
let temp = std::env::temp_dir();
for dir in [home_dir(), config_dir(), data_dir()] {
let dir = dir.unwrap();
assert!(
dir.starts_with(&temp),
"{} is outside the sandbox",
dir.display()
);
}
}
}