summaryrefslogtreecommitdiffstats
path: root/src/backup.rs
blob: ecdb87f9201697a68e1c1d00f4abb6930c671163 (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
use std::io;
use std::fs;
use chrono::Local;
use std::path::{Path,PathBuf};

use crate::defaults;
use crate::khline::KhLine;

pub fn backup(khline: &KhLine) -> io::Result<PathBuf> {
  let backupdir = defaults::get_backupdir();
  let backup_path = backupdir
    .join(format!("{}", Local::now().format("%FT%T")))
    .join(khline.get_normalized_path());

  if backup_path == khline.path {
    Err(io::Error::new(io::ErrorKind::Other, "backup dir same as source dir"))
  } else {
    let backup_path_parent = backup_path.parent().unwrap();
    prepare_backup_dir(&backup_path_parent)?;
    fs::copy(&khline.path, backup_path.clone())?;
    Ok(backup_path.clone())
  }
}

fn prepare_backup_dir(backupdir: &Path) -> io::Result<()> {
  if !backupdir.exists() {
    info!("Creating backup directory: {}", backupdir.to_string_lossy());
    fs::create_dir_all(&backupdir)?;
  }

  Ok(())
}

#[cfg(test)]
mod tests {
  use super::*;

  use crate::testutils::prepare_testdir;
  use assert_fs::prelude::*;
  use predicates::prelude::*;

  #[test]
  fn backup_test() {
    let testdir = prepare_testdir("testdir");

    let khline = "twodaysacrossbuckets.ics".parse::<KhLine>().unwrap();

    let new_path = backup(&khline).unwrap();

    testdir.child(".khaleesi/cal/twodaysacrossbuckets.ics").assert(predicate::path::exists());
    testdir.child(new_path.clone()).assert(predicate::path::exists());

    let predicate_file = predicate::path::eq_file(testdir.child(new_path.clone()).path());
    testdir.child(".khaleesi/cal/twodaysacrossbuckets.ics").assert(predicate_file);
  }
}