aboutsummaryrefslogtreecommitdiff
path: root/src/command/add.rs
blob: 9622f8120758ffa468531c143c6e41e1afb80fb7 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use super::{generate::Generate, sync_down::SyncDown, sync_up::SyncUp, update::Update};
use crate::configuration::Configuration;
use std::fs::{create_dir_all, remove_dir_all, rename};
use std::io::Result;

pub struct Add;

impl Add {
    pub fn new() -> Self {
        Add
    }
}

impl super::Command for Add {
    fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![Box::new(SyncDown::new())]
    }

    fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &str) -> Result<()> {
        create_dir_all(&configuration.posts_directory)?;
        for i in (0..configuration.max_posts - 1).rev() {
            let source = configuration.posts_directory.join(i.to_string());
            let target = configuration.posts_directory.join((i + 1).to_string());

            if target.exists() {
                remove_dir_all(&target)?;
            }
            if source.exists() {
                rename(&source, &target)?;
            }
        }
        Ok(())
    }

    fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![
            Box::new(Update::new()),
            Box::new(Generate::new()),
            Box::new(SyncUp::new()),
        ]
    }

    fn command(&self) -> &'static str {
        "add"
    }

    fn help(&self) -> &'static str {
        "<path_to_post>\t\t\tCreates new blog post"
    }
}

#[cfg(test)]
mod tests {
    use std::fs::create_dir_all;

    use super::*;
    use crate::command::Command;
    use crate::configuration::Configuration;

    use test_utilities::*;

    #[test]
    fn test_add_command() {
        let add = Add::new();

        // Create all directories
        let test_dir = setup_test_dir();

        let posts_dir = test_dir.join("posts");
        create_dir_all(&posts_dir).expect("Could not create posts test directory");
        create_dir_all(&posts_dir.join("0")).expect("Could not create post 0 test directory");

        // Create Test Files

        // Create configuration
        let mut configuration = Configuration::new();
        configuration.posts_directory = posts_dir.clone();

        // Let's ensure the initial state is OK
        assert!(&posts_dir.join("0").exists());
        assert!(!&posts_dir.join("1").exists());
        assert!(!&posts_dir.join("2").exists());
        assert!(!&posts_dir.join("3").exists());

        // Add 1 of 3
        add.execute(None, &configuration, "add")
            .expect("Could not add a.gmi");
        assert!(!&posts_dir.join("0").exists());
        assert!(&posts_dir.join("1").exists());
        assert!(!&posts_dir.join("2").exists());
        assert!(!&posts_dir.join("3").exists());

        // Add 2 of 3
        add.execute(None, &configuration, "add")
            .expect("Could not add a.gmi");
        assert!(!&posts_dir.join("0").exists());
        assert!(!&posts_dir.join("1").exists());
        assert!(&posts_dir.join("2").exists());
        assert!(!&posts_dir.join("3").exists());

        // Add 3 of 3
        add.execute(None, &configuration, "add")
            .expect("Could not add a.gmi");
        assert!(!&posts_dir.join("0").exists());
        assert!(!&posts_dir.join("1").exists());
        assert!(!&posts_dir.join("2").exists());
        assert!(!&posts_dir.join("3").exists());

        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn add_before_dependencies() {
        let add = Add::new();
        let dependencies = add.before_dependencies();

        assert_eq!(dependencies.len(), 1);
        assert_eq!(dependencies[0].command(), "sync-down");
    }

    #[test]
    fn add_after_dependencies() {
        let add = Add::new();
        let dependencies = add.after_dependencies();

        assert_eq!(dependencies.len(), 3);
        assert_eq!(dependencies[0].command(), "update");
        assert_eq!(dependencies[1].command(), "generate");
        assert_eq!(dependencies[2].command(), "sync-up");
    }

    // These two tests feel pointless but I'm doing it for the coverage :p

    #[test]
    fn add_command_output() {
        let add = Add::new();
        add.command();
    }

    #[test]
    fn add_help_output() {
        let add = Add::new();
        add.help();
    }
}