|
| 1 | +use std::io::Write; |
| 2 | + |
1 | 3 | #[compio_macros::test]
|
2 | 4 | async fn path_read_write() {
|
3 | 5 | let temp = tempfile::tempdir().unwrap();
|
@@ -81,3 +83,57 @@ async fn remove() {
|
81 | 83 | compio_fs::remove_dir(&new_dir).await.unwrap();
|
82 | 84 | assert!(compio_fs::metadata(&new_dir).await.is_err());
|
83 | 85 | }
|
| 86 | + |
| 87 | +#[compio_macros::test] |
| 88 | +async fn test_hard_link() { |
| 89 | + let dir = tempfile::tempdir().unwrap(); |
| 90 | + let src = dir.path().join("src.txt"); |
| 91 | + let dst = dir.path().join("dst.txt"); |
| 92 | + |
| 93 | + std::fs::File::create(&src) |
| 94 | + .unwrap() |
| 95 | + .write_all(b"hello") |
| 96 | + .unwrap(); |
| 97 | + |
| 98 | + compio_fs::hard_link(&src, &dst).await.unwrap(); |
| 99 | + |
| 100 | + std::fs::File::create(&src) |
| 101 | + .unwrap() |
| 102 | + .write_all(b"new-data") |
| 103 | + .unwrap(); |
| 104 | + |
| 105 | + let content = compio_fs::read(&dst).await.unwrap(); |
| 106 | + assert_eq!(content, b"new-data"); |
| 107 | + |
| 108 | + // test that this is not a symlink: |
| 109 | + assert!(std::fs::read_link(&dst).is_err()); |
| 110 | +} |
| 111 | + |
| 112 | +#[compio_macros::test] |
| 113 | +#[cfg(unix)] |
| 114 | +async fn test_symlink() { |
| 115 | + let dir = tempfile::tempdir().unwrap(); |
| 116 | + let src = dir.path().join("src.txt"); |
| 117 | + let dst = dir.path().join("dst.txt"); |
| 118 | + |
| 119 | + std::fs::File::create(&src) |
| 120 | + .unwrap() |
| 121 | + .write_all(b"hello") |
| 122 | + .unwrap(); |
| 123 | + |
| 124 | + compio_fs::symlink(&src, &dst).await.unwrap(); |
| 125 | + |
| 126 | + std::fs::File::create(&src) |
| 127 | + .unwrap() |
| 128 | + .write_all(b"new-data") |
| 129 | + .unwrap(); |
| 130 | + |
| 131 | + let content = compio_fs::read(&dst).await.unwrap(); |
| 132 | + assert_eq!(content, b"new-data"); |
| 133 | + |
| 134 | + let read = std::fs::read_link(dst.clone()).unwrap(); |
| 135 | + assert!(read == src); |
| 136 | + |
| 137 | + let symlink_meta = compio_fs::symlink_metadata(dst.clone()).await.unwrap(); |
| 138 | + assert!(symlink_meta.file_type().is_symlink()); |
| 139 | +} |
0 commit comments