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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::path::{Path, PathBuf};
use std::sync::{Mutex, Weak};

use rand;
use rand::Rng;

use TempDir;

use super::Registry;

const SUFFIX_LENGTH: usize = 10;

#[derive(Debug, Clone)]
pub struct FakeTempDir {
    registry: Weak<Mutex<Registry>>,
    path: PathBuf,
}

impl FakeTempDir {
    pub fn new(registry: Weak<Mutex<Registry>>, base: &Path, prefix: &str) -> Self {
        let mut rng = rand::thread_rng();
        let suffix: String = rng.gen_ascii_chars().take(SUFFIX_LENGTH).collect();
        let name = format!("{}_{}", prefix, suffix);
        let path = base.join(prefix).join(name);

        FakeTempDir {
            registry: registry,
            path: path,
        }
    }
}

impl TempDir for FakeTempDir {
    fn path(&self) -> &Path {
        self.path.as_ref()
    }
}

impl Drop for FakeTempDir {
    fn drop(&mut self) {
        if let Some(registry) = self.registry.upgrade() {
            let _ = registry.lock().unwrap().remove_dir_all(&self.path);
        }
    }
}