snapbox/dir/
fixture.rs

1/// Collection of files
2pub trait DirFixture: std::fmt::Debug {
3    /// Initialize a test fixture directory `root`
4    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error>;
5}
6
7#[cfg(feature = "dir")] // for documentation purposes only
8impl DirFixture for std::path::Path {
9    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
10        super::copy_template(self, root)
11    }
12}
13
14#[cfg(feature = "dir")] // for documentation purposes only
15impl DirFixture for &'_ std::path::Path {
16    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
17        std::path::Path::new(self).write_to_path(root)
18    }
19}
20
21#[cfg(feature = "dir")] // for documentation purposes only
22impl DirFixture for &'_ std::path::PathBuf {
23    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
24        std::path::Path::new(self).write_to_path(root)
25    }
26}
27
28#[cfg(feature = "dir")] // for documentation purposes only
29impl DirFixture for std::path::PathBuf {
30    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
31        std::path::Path::new(self).write_to_path(root)
32    }
33}
34
35#[cfg(feature = "dir")] // for documentation purposes only
36impl DirFixture for str {
37    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
38        std::path::Path::new(self).write_to_path(root)
39    }
40}
41
42#[cfg(feature = "dir")] // for documentation purposes only
43impl DirFixture for &'_ str {
44    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
45        std::path::Path::new(self).write_to_path(root)
46    }
47}
48
49#[cfg(feature = "dir")] // for documentation purposes only
50impl DirFixture for &'_ String {
51    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
52        std::path::Path::new(self).write_to_path(root)
53    }
54}
55
56#[cfg(feature = "dir")] // for documentation purposes only
57impl DirFixture for String {
58    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
59        std::path::Path::new(self).write_to_path(root)
60    }
61}
62
63impl<P, S> DirFixture for &[(P, S)]
64where
65    P: AsRef<std::path::Path>,
66    P: std::fmt::Debug,
67    S: AsRef<[u8]>,
68    S: std::fmt::Debug,
69{
70    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
71        let root = super::ops::canonicalize(root)
72            .map_err(|e| format!("Failed to canonicalize {}: {}", root.display(), e))?;
73
74        for (path, content) in self.iter() {
75            let rel_path = path.as_ref();
76            let path = root.join(rel_path);
77            let path = super::ops::normalize_path(&path);
78            if !path.starts_with(&root) {
79                return Err(crate::assert::Error::new(format!(
80                    "Fixture {} is for outside of the target root",
81                    rel_path.display(),
82                )));
83            }
84
85            let content = content.as_ref();
86
87            if let Some(dir) = path.parent() {
88                std::fs::create_dir_all(dir).map_err(|e| {
89                    format!(
90                        "Failed to create fixture directory {}: {}",
91                        dir.display(),
92                        e
93                    )
94                })?;
95            }
96            std::fs::write(&path, content)
97                .map_err(|e| format!("Failed to write fixture {}: {}", path.display(), e))?;
98        }
99        Ok(())
100    }
101}
102
103impl<const N: usize, P, S> DirFixture for [(P, S); N]
104where
105    P: AsRef<std::path::Path>,
106    P: std::fmt::Debug,
107    S: AsRef<[u8]>,
108    S: std::fmt::Debug,
109{
110    fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
111        let s: &[(P, S)] = self;
112        s.write_to_path(root)
113    }
114}