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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/// Working directory for tests
#[derive(Debug)]
pub struct DirRoot(DirRootInner);

#[derive(Debug)]
enum DirRootInner {
    None,
    Immutable(std::path::PathBuf),
    #[cfg(feature = "dir")]
    MutablePath(std::path::PathBuf),
    #[cfg(feature = "dir")]
    MutableTemp {
        temp: tempfile::TempDir,
        path: std::path::PathBuf,
    },
}

impl DirRoot {
    pub fn none() -> Self {
        Self(DirRootInner::None)
    }

    pub fn immutable(target: &std::path::Path) -> Self {
        Self(DirRootInner::Immutable(target.to_owned()))
    }

    #[cfg(feature = "dir")]
    pub fn mutable_temp() -> Result<Self, crate::assert::Error> {
        let temp = tempfile::tempdir().map_err(|e| e.to_string())?;
        // We need to get the `/private` prefix on Mac so variable substitutions work
        // correctly
        let path = crate::dir::canonicalize(temp.path())
            .map_err(|e| format!("Failed to canonicalize {}: {}", temp.path().display(), e))?;
        Ok(Self(DirRootInner::MutableTemp { temp, path }))
    }

    #[cfg(feature = "dir")]
    pub fn mutable_at(target: &std::path::Path) -> Result<Self, crate::assert::Error> {
        let _ = std::fs::remove_dir_all(target);
        std::fs::create_dir_all(target)
            .map_err(|e| format!("Failed to create {}: {}", target.display(), e))?;
        Ok(Self(DirRootInner::MutablePath(target.to_owned())))
    }

    #[cfg(feature = "dir")]
    pub fn with_template(
        self,
        template_root: &std::path::Path,
    ) -> Result<Self, crate::assert::Error> {
        match &self.0 {
            DirRootInner::None | DirRootInner::Immutable(_) => {
                return Err("Sandboxing is disabled".into());
            }
            DirRootInner::MutablePath(path) | DirRootInner::MutableTemp { path, .. } => {
                crate::debug!(
                    "Initializing {} from {}",
                    path.display(),
                    template_root.display()
                );
                super::copy_template(template_root, path)?;
            }
        }

        Ok(self)
    }

    pub fn is_mutable(&self) -> bool {
        match &self.0 {
            DirRootInner::None | DirRootInner::Immutable(_) => false,
            #[cfg(feature = "dir")]
            DirRootInner::MutablePath(_) => true,
            #[cfg(feature = "dir")]
            DirRootInner::MutableTemp { .. } => true,
        }
    }

    pub fn path(&self) -> Option<&std::path::Path> {
        match &self.0 {
            DirRootInner::None => None,
            DirRootInner::Immutable(path) => Some(path.as_path()),
            #[cfg(feature = "dir")]
            DirRootInner::MutablePath(path) => Some(path.as_path()),
            #[cfg(feature = "dir")]
            DirRootInner::MutableTemp { path, .. } => Some(path.as_path()),
        }
    }

    /// Explicitly close to report errors
    pub fn close(self) -> Result<(), std::io::Error> {
        match self.0 {
            DirRootInner::None | DirRootInner::Immutable(_) => Ok(()),
            #[cfg(feature = "dir")]
            DirRootInner::MutablePath(_) => Ok(()),
            #[cfg(feature = "dir")]
            DirRootInner::MutableTemp { temp, .. } => temp.close(),
        }
    }
}

impl Default for DirRoot {
    fn default() -> Self {
        Self::none()
    }
}