#![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 tempdir::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 = TempDir::new("private")?;
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(())
}
}