use dotenv::dotenv;
use once_cell::sync::OnceCell;
pub struct TestDir {
pub path: std::path::PathBuf,
}
impl TestDir {
pub fn as_string(&self) -> String {
self.path.to_str().unwrap().to_string()
}
pub fn new(createdir: bool) -> Self {
let path = std::path::PathBuf::new()
.join(env!("CARGO_MANIFEST_DIR"))
.join("testdata")
.join(format!("_testdir{}", hex::encode(randbytes(4))));
if createdir {
std::fs::create_dir(&path).unwrap();
}
Self { path }
}
}
impl Drop for TestDir {
fn drop(&mut self) {
match self.path.exists() {
true => {
std::fs::remove_dir_all(self.path.as_path()).unwrap();
}
false => {}
}
}
}
pub fn randbytes(n: usize) -> Vec<u8> {
let mut v = Vec::with_capacity(n);
for _ in 0..n {
let b: u8 = rand::random();
v.push(b);
}
v
}
pub fn integration_test_flag() -> bool {
match std::env::var("TEST_INTEGRATION") {
Ok(v) => v != "0",
Err(_) => false,
}
}
static CELL: OnceCell<()> = OnceCell::new();
pub fn setup_integration() {
CELL.get_or_init(|| {
match dotenv() {
Ok(_) => {}
Err(_) => {
eprint!("no .env file detected")
}
}
env_logger::init();
});
}