summaryrefslogtreecommitdiff
path: root/tests/tests.rs
blob: bfdab2a2eca8e8dfa4991407ae4bc8fd9b3110b1 (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
extern crate rsycle;

#[cfg(test)]
mod tests {
    use rsycle::main::{build_path, empty, list, restore, rsycle};
    use std::ffi::OsStr;
    use std::fs;
    use std::path::{Component, PathBuf};

    fn gen_rsyclebin(dir_name: &str) -> PathBuf {
        let mut rsyclebin = dirs::home_dir().unwrap();
        rsyclebin.push(dir_name);
        if !rsyclebin.exists() {
            fs::create_dir(&rsyclebin).unwrap();
        }
        rsyclebin
    }

    #[test]
    fn test_basics() {
        let filename = "test_file".as_ref();
        let test_path: PathBuf = [Component::CurDir, Component::Normal(filename)]
            .iter()
            .collect();
        assert!(!test_path.exists());
        fs::File::create(test_path.clone()).unwrap();

        let test_rsyclebin = gen_rsyclebin(".test_rsyclebin");
        test_rsycle(filename, test_rsyclebin.clone());
        test_restore(filename, test_rsyclebin.clone());
        test_cleanup(test_path, test_rsyclebin);
    }

    #[test]
    fn test_relative() {
        let filename = "../test_relative_file".as_ref();
        let test_path: PathBuf = [Component::CurDir, Component::Normal(filename)]
            .iter()
            .collect();
        assert!(!test_path.exists());
        fs::File::create(test_path.clone()).unwrap();

        let test_rsyclebin = gen_rsyclebin(".test_relative_rsyclebin");
        test_rsycle(filename, test_rsyclebin.clone());
        test_restore(filename, test_rsyclebin.clone());
        test_cleanup(test_path, test_rsyclebin);
    }

    fn test_rsycle(filename: &OsStr, rsyclebin: PathBuf) {
        let test_path = build_path(filename.to_str().unwrap()).unwrap();

        assert!(rsycle(rsyclebin.clone(), test_path.clone()).is_ok());

        assert!(!test_path.exists());

        assert!(list(rsyclebin.clone()).is_ok());
    }

    fn test_restore(filename: &OsStr, rsyclebin: PathBuf) {
        let test_path = build_path(filename.to_str().unwrap()).unwrap();

        assert!(restore(rsyclebin.clone(), test_path.clone()).is_ok());

        assert!(test_path.exists());
    }

    fn test_cleanup(path: PathBuf, rsyclebin: PathBuf) {
        assert!(empty(rsyclebin.clone()).is_ok());

        fs::remove_file(path).unwrap();
        fs::remove_dir_all(rsyclebin).unwrap();
    }
}