1 #![warn(rust_2018_idioms)] 2 #![cfg(feature = "full")] 3 4 use tokio::fs; 5 6 use std::io::prelude::*; 7 use std::io::BufReader; 8 use tempfile::tempdir; 9 10 #[tokio::test] test_hard_link()11async fn test_hard_link() { 12 let dir = tempdir().unwrap(); 13 let src = dir.path().join("src.txt"); 14 let dst = dir.path().join("dst.txt"); 15 16 { 17 let mut file = std::fs::File::create(&src).unwrap(); 18 file.write_all(b"hello").unwrap(); 19 } 20 21 let dst_2 = dst.clone(); 22 23 assert!(fs::hard_link(src, dst_2.clone()).await.is_ok()); 24 25 let mut content = String::new(); 26 27 { 28 let file = std::fs::File::open(dst).unwrap(); 29 let mut reader = BufReader::new(file); 30 reader.read_to_string(&mut content).unwrap(); 31 } 32 33 assert!(content == "hello"); 34 } 35 36 #[cfg(unix)] 37 #[tokio::test] test_symlink()38async fn test_symlink() { 39 let dir = tempdir().unwrap(); 40 let src = dir.path().join("src.txt"); 41 let dst = dir.path().join("dst.txt"); 42 43 { 44 let mut file = std::fs::File::create(&src).unwrap(); 45 file.write_all(b"hello").unwrap(); 46 } 47 48 let src_2 = src.clone(); 49 let dst_2 = dst.clone(); 50 51 assert!(fs::symlink(src_2.clone(), dst_2.clone()).await.is_ok()); 52 53 let mut content = String::new(); 54 55 { 56 let file = std::fs::File::open(dst.clone()).unwrap(); 57 let mut reader = BufReader::new(file); 58 reader.read_to_string(&mut content).unwrap(); 59 } 60 61 assert!(content == "hello"); 62 63 let read = fs::read_link(dst.clone()).await.unwrap(); 64 assert!(read == src); 65 66 let symlink_meta = fs::symlink_metadata(dst.clone()).await.unwrap(); 67 assert!(symlink_meta.file_type().is_symlink()); 68 } 69