diff --git a/src/filesystem.rs b/src/filesystem.rs index 50aa490..2170b45 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -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 { + 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(()) + } }