aboutsummaryrefslogtreecommitdiffstats
path: root/crates/config/src/testing.rs
blob: 8101ee31cac5edc4b29716cb1147661d2c65e93c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//! What the crate's tests share.

use std::path::PathBuf;

/// A directory of a test's files, removed with it.
pub(crate) struct Scratch(pub(crate) PathBuf);

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

    /// A file holding `content`: its name.
    pub(crate) fn file(&self, name: &str, content: &str) -> String {
        std::fs::write(self.0.join(name), content).unwrap();
        name.to_owned()
    }
}

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