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
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/HOW_TO_USE.md"))]
pub use assay_proc_macro::assay;
#[doc(hidden)]
pub use pretty_assertions::{assert_eq, assert_ne};
#[doc(hidden)]
pub use rusty_fork::{fork, rusty_fork_id, rusty_fork_test_name, ChildWrapper};
#[doc(hidden)]
pub use tokio::runtime::Runtime;
use std::{
env,
error::Error,
fs::{copy, create_dir_all},
path::{Component, Path, PathBuf},
};
use tempfile::{Builder, TempDir};
#[doc(hidden)]
pub struct PrivateFS {
ran_from: PathBuf,
directory: TempDir,
}
impl PrivateFS {
pub fn new() -> Result<Self, Box<dyn Error>> {
let ran_from = env::current_dir()?;
let directory = Builder::new().prefix("private").tempdir()?;
env::set_current_dir(directory.path())?;
Ok(Self {
ran_from,
directory,
})
}
pub fn include(&self, path: impl AsRef<Path>) -> Result<(), Box<dyn Error>> {
let mut inner_path = path.as_ref().to_owned();
let is_relative = inner_path.is_relative();
if is_relative {
inner_path = self.ran_from.join(&path);
}
let dir = self.directory.path().to_owned();
let relative = if !is_relative {
inner_path
.components()
.filter(|c| *c != Component::RootDir)
.collect::<PathBuf>()
} else {
path.as_ref().into()
};
if let Some(parent) = relative.parent() {
create_dir_all(dir.join(parent))?;
}
copy(inner_path, dir.join(relative))?;
Ok(())
}
}