1use std::cell::RefCell;
2use std::collections::HashMap;
3use std::path::PathBuf;
4
5#[derive(Default)]
6pub struct MockFileSystem {
7 files: RefCell<HashMap<String, std::io::Result<String>>>,
8}
9
10impl MockFileSystem {
11 pub fn clear(&self) {
12 self.files.borrow_mut().clear();
13 }
14
15 fn get_test_file_path() -> PathBuf {
16 PathBuf::from(std::env!("CARGO_MANIFEST_DIR")).join("resources")
17 }
18
19 fn get_file(&self, path: &str) -> std::io::Result<String> {
20 let mut tmp = self.files.borrow_mut();
21 let result = tmp.entry(path.to_string()).or_insert_with(|| {
22 let root_path = Self::get_test_file_path();
23 let full_path = root_path.join(path);
24 std::fs::read_to_string(full_path)
25 });
26 match result {
27 Ok(s) => Ok(s.to_string()),
28 Err(e) => Err(std::io::Error::new(e.kind(), "Error")),
29 }
30 }
31
32 #[cfg(feature = "sync")]
33 pub fn get_test_file_sync(&self, path: &str) -> std::io::Result<String> {
34 self.get_file(path)
35 }
36
37 #[cfg(feature = "async")]
38 pub async fn get_test_file_async(&self, path: &str) -> std::io::Result<String> {
39 self.get_file(path)
40 }
41}