1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::path::PathBuf;

use tracing::error;

use crate::atoms::{Atom, Outcome};

pub struct Remove {
    pub target: PathBuf,
}

impl std::fmt::Display for Remove {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "The directory {} needs to be removed",
            self.target.display(),
        )
    }
}

impl Atom for Remove {
    fn plan(&self) -> anyhow::Result<Outcome> {
        let path_to_dir = PathBuf::from(&self.target);

        if !path_to_dir.is_dir() {
            error!(
                "Cannot plan: target isn`t a directory: {}",
                self.target.display()
            );
            return Ok(Outcome {
                side_effects: vec![],
                should_run: false,
            });
        }

        let is_empty = path_to_dir.read_dir()?.next().is_none();

        if !is_empty {
            error!(
                "Cannot plan: directory {} is not empty",
                self.target.display()
            );

            return Ok(Outcome {
                side_effects: vec![],
                should_run: false,
            });
        }

        Ok(Outcome {
            side_effects: vec![],
            should_run: self.target.exists(),
        })
    }

    fn execute(&mut self) -> anyhow::Result<()> {
        std::fs::remove_dir(&self.target)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {

    use std::{fs::File, io::Write};

    use super::*;
    use pretty_assertions::assert_eq;
    use tempfile::tempdir;

    #[test]
    fn it_can_plan() {
        let temp = tempdir().unwrap().into_path();
        let atom = Remove {
            target: temp.clone(),
        };
        assert_eq!(true, atom.plan().unwrap().should_run);

        let file_path = temp.join("my-temporary-note.txt");
        let mut file = File::create(&file_path).unwrap();
        writeln!(file, "Brian was here. Briefly.").unwrap();

        // Should not run when dir is not empty
        assert_eq!(false, atom.plan().unwrap().should_run);

        // Should not run if path is not a dir
        let atom = Remove {
            target: file_path.clone(),
        };

        assert_eq!(false, atom.plan().unwrap().should_run)
    }

    #[test]
    fn it_can_execute() {
        let temp_dir = match tempfile::tempdir() {
            std::result::Result::Ok(dir) => dir,
            std::result::Result::Err(_) => {
                assert_eq!(false, true);
                return;
            }
        };

        let path = temp_dir.path();

        assert_eq!(true, path.exists());

        let mut atom = Remove {
            target: temp_dir.path().to_path_buf(),
        };

        // Deletes dir
        assert_eq!(true, atom.execute().is_ok());
        // Dir is deleted and dont exists
        assert_eq!(false, temp_dir.path().exists())
    }
}