forked from kemitix/git-next
feat(filesystem): Add read_file()
This commit is contained in:
parent
8d983418c5
commit
90f9ab8e96
1 changed files with 23 additions and 0 deletions
|
@ -57,6 +57,18 @@ pub trait FileSystemEnv: Sync + Send + std::fmt::Debug {
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
File::open(name).is_ok()
|
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)]
|
#[derive(Clone, Debug, Default)]
|
||||||
|
@ -125,4 +137,15 @@ mod tests {
|
||||||
let env = RealFileSystemEnv::new(cwd.clone());
|
let env = RealFileSystemEnv::new(cwd.clone());
|
||||||
assert_eq!(env.cwd(), &cwd);
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue