comtrya_lib/steps/initializers/
file_exists.rs1use std::path::PathBuf;
2
3use super::Initializer;
4
5#[derive(Clone, Debug)]
6pub struct FileExists(pub PathBuf);
7
8impl Initializer for FileExists {
9 fn initialize(&self) -> anyhow::Result<bool> {
10 Ok(self.0.exists())
11 }
12}
13
14#[cfg(test)]
15mod tests {
16 use super::*;
17 use pretty_assertions::assert_eq;
18
19 #[test]
20 fn it_returns_false_when_not_found() {
21 let initializer = FileExists(PathBuf::from("not-a-existing-file"));
22 let result = initializer.initialize();
23
24 assert_eq!(true, result.is_ok());
25 assert_eq!(false, result.unwrap());
26 }
27
28 #[test]
29 fn it_returns_true_when_found() {
30 let to_file = match tempfile::NamedTempFile::new() {
31 std::result::Result::Ok(file) => file,
32 std::result::Result::Err(_) => {
33 assert_eq!(false, true);
34 return;
35 }
36 };
37
38 let path_buf = to_file.path().to_path_buf();
39
40 let initializer = FileExists(path_buf);
41 let result = initializer.initialize();
42
43 assert_eq!(true, result.is_ok());
44 assert_eq!(true, result.unwrap());
45 }
46}