use std::{
env::temp_dir,
fs,
path::{Path, PathBuf},
};
use tempfile::Builder;
use crate::random;
pub const RELATIVE_TARI_PATH: &str = "tari-tests/";
pub fn create_temporary_data_path() -> PathBuf {
let path = temp_tari_path().join(random::prefixed_string("data-", 20));
fs::create_dir_all(&path).unwrap();
path
}
pub fn with_temp_dir<F, R>(f: F) -> R
where F: FnOnce(&Path) -> R {
let tmp = Builder::new().prefix("tari-test").tempdir().unwrap();
let r = f(tmp.path());
drop(tmp);
r
}
pub fn temp_tari_path() -> PathBuf {
temp_dir().join(RELATIVE_TARI_PATH)
}
pub fn cargo_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
pub use tempfile::tempdir;
#[cfg(test)]
mod test {
use std::{
path::{Path, PathBuf},
str::FromStr,
};
#[test]
fn with_temp_dir() {
let path = super::with_temp_dir(|path| {
assert!(Path::exists(path));
path.to_str().unwrap().to_string()
});
assert!(!PathBuf::from_str(&path).unwrap().exists());
}
}