feat(filesystem): Add read_file()

This commit is contained in:
Paul Campbell 2024-04-07 13:46:59 +01:00
parent 8d983418c5
commit 90f9ab8e96

View file

@ -57,6 +57,18 @@ pub trait FileSystemEnv: Sync + Send + std::fmt::Debug {
use std::fs::File;
File::open(name).is_ok()
}
fn read_file(&self, file_name: &str) -> std::io::Result<String> {
use std::fs::File;
use std::io::Read;
let path = self.in_cwd(file_name);
info!("reading from {:?}", path);
let mut file = File::open(path)?;
let mut content = String::new();
file.read_to_string(&mut content)?;
Ok(content)
}
}
#[derive(Clone, Debug, Default)]
@ -125,4 +137,15 @@ mod tests {
let env = RealFileSystemEnv::new(cwd.clone());
assert_eq!(env.cwd(), &cwd);
}
#[test_log::test]
fn test_write_and_read_file() -> std::io::Result<()> {
let env = TempFileSystemEnv::new()?;
let file_name = "test.txt";
let content = "Hello, World!";
let path = env.write_file(file_name, content)?;
assert_eq!(env.read_file(file_name)?, content);
assert!(path.exists());
Ok(())
}
}