tests.rs
1 use std::path::Path; 2 use std::{fs, io}; 3 4 use tempdir::TempDir; 5 6 fn create_file(path: &Path) -> io::Result<()> { 7 std::fs::File::create(path)?; 8 Ok(()) 9 } 10 11 fn assert_is_link(path: &Path, links_to: &Path) { 12 let dst_path = fs::read_link(path).unwrap(); 13 14 assert_eq!(dst_path, links_to); 15 } 16 17 #[test] 18 fn simple_file() -> io::Result<()> { 19 let dotr = super::Dotr::new(); 20 21 let src = TempDir::new("src").unwrap(); 22 let dst = TempDir::new("dst").unwrap(); 23 let src = src.path(); 24 let dst = dst.path(); 25 26 let src_path = src.join("a"); 27 let dst_path = dst.join("a"); 28 create_file(&src_path)?; 29 30 dotr.link(src, dst)?; 31 assert_is_link(&dst_path, &src_path); 32 33 dotr.unlink(src, dst)?; 34 assert!(!dst_path.exists()); 35 36 Ok(()) 37 } 38 39 #[test] 40 fn simple_nested_file() -> io::Result<()> { 41 let dotr = super::Dotr::new(); 42 43 let src = TempDir::new("src").unwrap(); 44 let dst = TempDir::new("dst").unwrap(); 45 let src = src.path(); 46 let dst = dst.path(); 47 48 let src_path = src.join("foo").join("a"); 49 let dst_path = dst.join("foo").join("a"); 50 fs::create_dir_all(src.join("foo"))?; 51 create_file(&src_path)?; 52 53 dotr.link(src, dst)?; 54 assert_is_link(&dst_path, &src_path); 55 56 dotr.unlink(src, dst)?; 57 assert!(!dst_path.exists()); 58 59 Ok(()) 60 } 61 62 #[test] 63 fn simple_symlink() -> io::Result<()> { 64 let dotr = super::Dotr::new(); 65 66 let src = TempDir::new("src").unwrap(); 67 let dst = TempDir::new("dst").unwrap(); 68 let src = src.path(); 69 let dst = dst.path(); 70 71 let src_path = src.join("a"); 72 let src_link_path = src.join("a.lnk"); 73 let dst_path = dst.join("a"); 74 let dst_link_path = dst.join("a.lnk"); 75 create_file(&src_path)?; 76 77 std::os::unix::fs::symlink(&src_path, src_link_path)?; 78 79 dotr.link(src, dst)?; 80 assert_is_link(&dst_link_path, &src_path); 81 82 dotr.unlink(src, dst)?; 83 assert!(!dst_path.exists()); 84 assert!(!dst_link_path.exists()); 85 86 Ok(()) 87 }