aboutsummaryrefslogtreecommitdiff
path: root/src/remote/mod.rs
blob: 74f4c4815370a382ddadb7e52bc5acf65589e664 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
mod git;

use std::fs::{create_dir_all, remove_file, write, File};
use std::io::{Error, ErrorKind::Other, Read, Result};
use std::path::Path;

use git::Git;

pub trait Remote {
    fn can_handle(&self, remote: &str) -> bool;
    fn sync_up(&self, remote: &str, directory: &Path) -> Result<()>;
    fn sync_down(&self, remote: &str, directory: &Path) -> Result<()>;
}

pub fn add(config_directory: &Path, remote_config: &Path, remote: &str) -> Result<()> {
    create_dir_all(config_directory)?;
    write(remote_config, remote)?;
    Ok(())
}

pub fn remove(remote_config: &Path) -> Result<()> {
    if remote_config.exists() {
        remove_file(remote_config)?;
    }
    Ok(())
}

pub fn sync_up(data_directory: &Path, remote_config: &Path) -> Result<()> {
    let remote_address =
        read_remote(remote_config).ok_or_else(|| Error::new(Other, "No remote is configured"))?;
    create_dir_all(data_directory)?;
    let remotes = available_remotes();
    for remote in remotes {
        if remote.can_handle(&remote_address) {
            return remote.sync_up(&remote_address, data_directory);
        }
    }
    Err(Error::new(
        Other,
        "No valid strategies found for your configured remote.",
    ))
}

pub fn sync_down(data_directory: &Path, remote_config: &Path) -> Result<()> {
    let remote_address =
        read_remote(remote_config).ok_or_else(|| Error::new(Other, "No remote is configured"))?;
    create_dir_all(data_directory)?;
    let remotes = available_remotes();
    for remote in remotes {
        if remote.can_handle(&remote_address) {
            return remote.sync_down(&remote_address, data_directory);
        }
    }
    Err(Error::new(
        Other,
        "No valid strategies found for your configured remote.",
    ))
}

fn available_remotes() -> Vec<Box<dyn Remote>> {
    vec![Box::new(Git::new())]
}

fn read_remote(file_path: &Path) -> Option<String> {
    let mut file = File::open(file_path).ok()?;
    let mut contents = String::new();
    file.read_to_string(&mut contents).ok()?;
    Some(contents)
}

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

    use super::*;

    use test_utilities::*;

    #[test]
    fn test_adds_a_remote() {
        let test_dir = setup_test_dir();
        let config_dir = test_dir.join("config");
        let remote_config = config_dir.join("remoteconfig");

        assert!(!&config_dir.exists());
        add(&config_dir, &remote_config, "whaaat").expect("Could not add a remote");
        assert!(&config_dir.exists());
        assert_file_contents(&remote_config, "whaaat");

        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_removes_a_remote() {
        let test_dir = setup_test_dir();
        let remote_config = test_dir.join("remoteconfig");

        create_test_file(&remote_config, "remote control");

        assert_file_contents(&remote_config, "remote control");
        remove(&remote_config).expect("Could not remove a remote");
        assert!(!&remote_config.exists());

        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_syncs_remote_up() {
        let test_dir = setup_test_dir();
        let local_dir = test_dir.join("modlocal");
        let remote_dir = test_dir.join("modremote");
        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);

        create_test_file(&local_dir.join("file1.txt"), "I exist");
        sync_up(&local_dir, &test_dir.join("remoteconfig")).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(&local_dir, &test_dir.join("remoteconfig")).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_syncs_remote_up_even_if_directory_does_not_exist() {
        let test_dir = setup_test_dir();
        let local_dir = test_dir.join("modlocal");
        let remote_dir = test_dir.join("modremote");
        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);

        sync_up(&local_dir, &test_dir.join("remoteconfig")).expect("Could not sync up to remote");
        assert!(local_dir.exists());
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_sync_up_down_fails_if_local_not_creatable() {
        let test_dir = setup_test_dir();
        let local_dir = &PathBuf::from("/lasjkdlaksjdklasjdlaks/asldkj/asjdlaksdj");
        let remote_dir = test_dir.join("modremote");
        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 result = sync_up(&local_dir, &test_dir.join("remoteconfig"));
        assert!(result.is_err());
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_sync_up_fails_on_invalid_strategies() {
        let test_dir = setup_test_dir();
        let local_dir = test_dir.join("modlocal");
        create_test_file(&test_dir.join("remoteconfig"), "AAAAAH");

        let result = sync_up(&local_dir, &test_dir.join("remoteconfig"));
        assert!(result.is_err());
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_syncs_remote_down() {
        let test_dir = setup_test_dir();
        let local_dir = test_dir.join("modownlocal");
        let remote_dir = test_dir.join("modownremote");
        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);

        commit_file_to_remote(&remote_dir_as_string, "file 1.txt", "I exist, but remotely");
        assert!(!&local_dir.join("file 1.txt").exists());
        assert!(!&local_dir.join("file2.txt").exists());
        sync_down(&local_dir, &test_dir.join("remoteconfig"))
            .expect("Could not sync down from remote");
        assert_file_contents(&local_dir.join("file 1.txt"), "I exist, but remotely");
        assert!(!&local_dir.join("file2.txt").exists());

        commit_file_to_remote(
            &remote_dir_as_string,
            "file2.txt",
            "I also exist, but remotely",
        );
        assert!(!&local_dir.join("file2.txt").exists());
        sync_down(&local_dir, &test_dir.join("remoteconfig"))
            .expect("Could not sync down from remote");
        assert_file_contents(&local_dir.join("file 1.txt"), "I exist, but remotely");
        assert_file_contents(&local_dir.join("file2.txt"), "I also exist, but remotely");
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_syncs_remote_down_even_if_directory_does_not_exist() {
        let test_dir = setup_test_dir();
        let local_dir = test_dir.join("modlocal");
        let remote_dir = test_dir.join("modremote");
        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);

        commit_file_to_remote(&remote_dir_as_string, "onions", "Make me cry");

        sync_down(&local_dir, &test_dir.join("remoteconfig")).expect("Could not sync up to remote");
        assert_file_contents(&local_dir.join("onions"), "Make me cry");
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_sync_down_fails_if_local_not_creatable() {
        let test_dir = setup_test_dir();
        let local_dir = &PathBuf::from("/lasjkdlaksjdklasjdlaks/asldkj/asjdlaksdj");
        let remote_dir = test_dir.join("modremote");
        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 result = sync_down(&local_dir, &test_dir.join("remoteconfig"));
        assert!(result.is_err());
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_sync_down_fails_on_invalid_strategies() {
        let test_dir = setup_test_dir();
        let local_dir = test_dir.join("modlocal");
        create_test_file(&test_dir.join("remoteconfig"), "AAAAAH");

        let result = sync_down(&local_dir, &test_dir.join("remoteconfig"));
        assert!(result.is_err());
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_it_should_fail_if_remote_is_invalid() {
        let test_dir = setup_test_dir();
        let bad_file_path = &test_dir.join("BADFILE OH NO");
        let invalid_bytes = vec![0xFF, 0xFF];

        let mut bad_file = File::create(&bad_file_path).expect("Could not create bad file.");
        bad_file
            .write_all(&invalid_bytes)
            .expect("Could not write bad bytes to bad file.");

        assert_eq!(read_remote(&bad_file_path), None);
        cleanup_test_dir(&test_dir);
    }
}