1 | #![warn (rust_2018_idioms)] |
2 | #![cfg (all(feature = "full" , not(target_os = "wasi" )))] // WASI does not support all fs operations |
3 | |
4 | use tempfile::tempdir; |
5 | use tokio::fs; |
6 | |
7 | #[tokio::test ] |
8 | async fn copy() { |
9 | let dir = tempdir().unwrap(); |
10 | |
11 | let source_path = dir.path().join("foo.txt" ); |
12 | let dest_path = dir.path().join("bar.txt" ); |
13 | |
14 | fs::write(&source_path, b"Hello File!" ).await.unwrap(); |
15 | fs::copy(&source_path, &dest_path).await.unwrap(); |
16 | |
17 | let from = fs::read(&source_path).await.unwrap(); |
18 | let to = fs::read(&dest_path).await.unwrap(); |
19 | |
20 | assert_eq!(from, to); |
21 | } |
22 | |
23 | #[tokio::test ] |
24 | async fn copy_permissions() { |
25 | let dir = tempdir().unwrap(); |
26 | let from_path = dir.path().join("foo.txt" ); |
27 | let to_path = dir.path().join("bar.txt" ); |
28 | |
29 | let from = tokio::fs::File::create(&from_path).await.unwrap(); |
30 | let mut from_perms = from.metadata().await.unwrap().permissions(); |
31 | from_perms.set_readonly(true); |
32 | from.set_permissions(from_perms.clone()).await.unwrap(); |
33 | |
34 | tokio::fs::copy(from_path, &to_path).await.unwrap(); |
35 | |
36 | let to_perms = tokio::fs::metadata(to_path).await.unwrap().permissions(); |
37 | |
38 | assert_eq!(from_perms, to_perms); |
39 | } |
40 | |