Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-cli/tests/common/home.rs

Raw
//! SMH-SPEC-SPEC0001: temporary fixture root cleanup.

use std::ops::Deref;
use std::path::{Path, PathBuf};

pub struct TestHome(PathBuf);

impl TestHome {
    pub fn new(name: &str) -> Self {
        let path = std::env::temp_dir().join(format!("{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&path);
        std::fs::create_dir_all(&path).unwrap();
        Self(path)
    }
}

impl AsRef<Path> for TestHome {
    fn as_ref(&self) -> &Path {
        &self.0
    }
}

impl Deref for TestHome {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Drop for TestHome {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}