use std::cell::RefCell;
use std::env;
use std::io::{Cursor, Read, Write};
use std::path::Path;
use std::time::SystemTime;
use findutils::find::matchers::{Follow, MatcherIO, WalkEntry};
use findutils::find::Dependencies;
pub struct FakeDependencies {
pub output: RefCell<Cursor<Vec<u8>>>,
now: SystemTime,
}
impl FakeDependencies {
pub fn new() -> Self {
Self {
output: RefCell::new(Cursor::new(Vec::<u8>::new())),
now: SystemTime::now(),
}
}
pub fn new_matcher_io(&self) -> MatcherIO<'_> {
MatcherIO::new(self)
}
pub fn get_output_as_string(&self) -> String {
let mut cursor = self.output.borrow_mut();
cursor.set_position(0);
let mut contents = String::new();
cursor.read_to_string(&mut contents).unwrap();
contents
}
}
impl Dependencies for FakeDependencies {
fn get_output(&self) -> &RefCell<dyn Write> {
&self.output
}
fn now(&self) -> SystemTime {
self.now
}
}
pub fn path_to_testing_commandline() -> String {
let mut path_to_use = env::current_exe()
.expect("can't find path of this executable")
.parent()
.expect("can't find parent directory of this executable")
.to_path_buf();
if path_to_use.ends_with("deps") {
path_to_use.pop();
}
path_to_use = path_to_use.join("testing-commandline");
path_to_use.to_string_lossy().to_string()
}
#[cfg(windows)]
pub fn fix_up_slashes(path: &str) -> String {
path.replace("/", "\\")
}
#[cfg(not(windows))]
pub fn fix_up_slashes(path: &str) -> String {
path.to_string()
}
pub fn get_dir_entry_for(root: &str, path: &str) -> WalkEntry {
let root = fix_up_slashes(root);
let root = Path::new(&root);
let path = fix_up_slashes(path);
let path = if path.is_empty() {
root.to_owned()
} else {
root.join(path)
};
let depth = path.components().count() - root.components().count();
WalkEntry::new(path, depth, Follow::Never)
}