1#![warn(rust_2018_idioms)]
2#![cfg(all(feature = "full", not(target_os = "wasi")))] // WASI does not support all fs operations
3
4use tokio::fs;
5
6use std::io::Write;
7use tempfile::tempdir;
8
9#[tokio::test]
10async fn test_hard_link() {
11 let dir = tempdir().unwrap();
12 let src = dir.path().join("src.txt");
13 let dst = dir.path().join("dst.txt");
14
15 std::fs::File::create(&src)
16 .unwrap()
17 .write_all(b"hello")
18 .unwrap();
19
20 fs::hard_link(&src, &dst).await.unwrap();
21
22 std::fs::File::create(&src)
23 .unwrap()
24 .write_all(b"new-data")
25 .unwrap();
26
27 let content = fs::read(&dst).await.unwrap();
28 assert_eq!(content, b"new-data");
29
30 // test that this is not a symlink:
31 assert!(fs::read_link(&dst).await.is_err());
32}
33
34#[cfg(unix)]
35#[tokio::test]
36async fn test_symlink() {
37 let dir = tempdir().unwrap();
38 let src = dir.path().join("src.txt");
39 let dst = dir.path().join("dst.txt");
40
41 std::fs::File::create(&src)
42 .unwrap()
43 .write_all(b"hello")
44 .unwrap();
45
46 fs::symlink(&src, &dst).await.unwrap();
47
48 std::fs::File::create(&src)
49 .unwrap()
50 .write_all(b"new-data")
51 .unwrap();
52
53 let content = fs::read(&dst).await.unwrap();
54 assert_eq!(content, b"new-data");
55
56 let read = fs::read_link(dst.clone()).await.unwrap();
57 assert!(read == src);
58
59 let symlink_meta = fs::symlink_metadata(dst.clone()).await.unwrap();
60 assert!(symlink_meta.file_type().is_symlink());
61}
62