aboutsummaryrefslogtreecommitdiff
path: root/src/command/sync_up.rs
blob: 57c4e1ca4c6e6eae7b671156f5d42005937bbf53 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::configuration::Configuration;
use crate::remote::sync_up;
use std::io::Result;

pub struct SyncUp;

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

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

    fn execute(
        &self,
        _: Option<&String>,
        configuration: &Configuration,
        command: &str,
    ) -> Result<()> {
        match sync_up(&configuration.data_directory, &configuration.remote_config) {
            Ok(()) => {}
            Err(e) => {
                if command == self.command() {
                    return Err(e);
                }
            }
        }
        Ok(())
    }

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

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

    fn help(&self) -> &'static str {
        "\t\t\t\tPushes to the git remote if configured"
    }
}

#[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_sync_up_command() {
        let sync_up = SyncUp::new();

        let test_dir = setup_test_dir();
        let local_dir = test_dir.join("commandlocal");
        let remote_dir = test_dir.join("commandremote");
        create_dir_all(&local_dir).expect("Could not create local test directory");
        create_dir_all(&remote_dir).expect("Could not create remote test directory");

        create_remote(&remote_dir);

        let remote_dir_as_string = remote_dir.display().to_string();
        create_test_file(&test_dir.join("remoteconfig"), &remote_dir_as_string);

        let mut configuration = Configuration::new().unwrap();
        configuration.data_directory = local_dir.clone();
        configuration.remote_config = test_dir.join("remoteconfig");

        create_test_file(&local_dir.join("file1.txt"), "I exist");
        sync_up
            .execute(None, &configuration, "sync-up")
            .expect("Could not sync up to remote");
        assert_file_in_repo_with_contents(&remote_dir_as_string, "file1.txt", "I exist");
        assert_file_in_repo_does_not_exist(&remote_dir_as_string, "file2.txt");

        create_test_file(&local_dir.join("file2.txt"), "I also exist now, btw");
        sync_up
            .execute(None, &configuration, "sync-up")
            .expect("Could not sync up to remote");
        assert_file_in_repo_with_contents(&remote_dir_as_string, "file1.txt", "I exist");
        assert_file_in_repo_with_contents(
            &remote_dir_as_string,
            "file2.txt",
            "I also exist now, btw",
        );
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_sync_up_fails_with_incorrect_remote() {
        let sync_up = SyncUp::new();

        let test_dir = setup_test_dir();
        create_test_file(&test_dir.join("remoteconfig"), "DO NOT WAAANT");

        let mut configuration = Configuration::new().unwrap();
        configuration.data_directory = test_dir.clone();
        configuration.remote_config = test_dir.join("remoteconfig");

        let result = sync_up.execute(None, &configuration, "sync-up");
        assert!(result.is_err());
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_sync_up_does_not_fail_if_running_as_dependency() {
        let sync_up = SyncUp::new();

        let test_dir = setup_test_dir();
        create_test_file(&test_dir.join("remoteconfig"), "DO NOT WAAANT");

        let mut configuration = Configuration::new().unwrap();
        configuration.data_directory = test_dir.clone();
        configuration.remote_config = test_dir.join("remoteconfig");

        let result = sync_up.execute(None, &configuration, "NOPE");
        assert!(result.is_ok());
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn sync_up_before_dependencies() {
        let sync_up = SyncUp::new();
        let dependencies = sync_up.before_dependencies();

        assert_eq!(dependencies.len(), 0);
    }

    #[test]
    fn sync_up_after_dependencies() {
        let sync_up = SyncUp::new();
        let dependencies = sync_up.after_dependencies();

        assert_eq!(dependencies.len(), 0);
    }

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

    #[test]
    fn sync_up_command_output() {
        let sync_up = SyncUp::new();
        sync_up.command();
    }

    #[test]
    fn sync_up_help_output() {
        let sync_up = SyncUp::new();
        sync_up.help();
    }
}