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;
5use tokio_test::{assert_err, assert_ok};
6
7use std::sync::{Arc, Mutex};
8use tempfile::tempdir;
9
10#[tokio::test]
11async fn create_dir() {
12 let base_dir = tempdir().unwrap();
13 let new_dir = base_dir.path().join("foo");
14 let new_dir_2 = new_dir.clone();
15
16 assert_ok!(fs::create_dir(new_dir).await);
17
18 assert!(new_dir_2.is_dir());
19}
20
21#[tokio::test]
22async fn create_all() {
23 let base_dir = tempdir().unwrap();
24 let new_dir = base_dir.path().join("foo").join("bar");
25 let new_dir_2 = new_dir.clone();
26
27 assert_ok!(fs::create_dir_all(new_dir).await);
28 assert!(new_dir_2.is_dir());
29}
30
31#[tokio::test]
32async fn build_dir() {
33 let base_dir = tempdir().unwrap();
34 let new_dir = base_dir.path().join("foo").join("bar");
35 let new_dir_2 = new_dir.clone();
36
37 assert_ok!(fs::DirBuilder::new().recursive(true).create(new_dir).await);
38
39 assert!(new_dir_2.is_dir());
40 assert_err!(
41 fs::DirBuilder::new()
42 .recursive(false)
43 .create(new_dir_2)
44 .await
45 );
46}
47
48#[tokio::test]
49#[cfg(unix)]
50async fn build_dir_mode_read_only() {
51 let base_dir = tempdir().unwrap();
52 let new_dir = base_dir.path().join("abc");
53
54 assert_ok!(
55 fs::DirBuilder::new()
56 .recursive(true)
57 .mode(0o444)
58 .create(&new_dir)
59 .await
60 );
61
62 assert!(fs::metadata(new_dir)
63 .await
64 .expect("metadata result")
65 .permissions()
66 .readonly());
67}
68
69#[tokio::test]
70async fn remove() {
71 let base_dir = tempdir().unwrap();
72 let new_dir = base_dir.path().join("foo");
73 let new_dir_2 = new_dir.clone();
74
75 std::fs::create_dir(new_dir.clone()).unwrap();
76
77 assert_ok!(fs::remove_dir(new_dir).await);
78 assert!(!new_dir_2.exists());
79}
80
81#[tokio::test]
82async fn read_inherent() {
83 let base_dir = tempdir().unwrap();
84
85 let p = base_dir.path();
86 std::fs::create_dir(p.join("aa")).unwrap();
87 std::fs::create_dir(p.join("bb")).unwrap();
88 std::fs::create_dir(p.join("cc")).unwrap();
89
90 let files = Arc::new(Mutex::new(Vec::new()));
91
92 let f = files.clone();
93 let p = p.to_path_buf();
94
95 let mut entries = fs::read_dir(p).await.unwrap();
96
97 while let Some(e) = assert_ok!(entries.next_entry().await) {
98 let s = e.file_name().to_str().unwrap().to_string();
99 f.lock().unwrap().push(s);
100 }
101
102 let mut files = files.lock().unwrap();
103 files.sort(); // because the order is not guaranteed
104 assert_eq!(
105 *files,
106 vec!["aa".to_string(), "bb".to_string(), "cc".to_string()]
107 );
108}
109
110#[tokio::test]
111async fn read_dir_entry_info() {
112 let temp_dir = tempdir().unwrap();
113
114 let file_path = temp_dir.path().join("a.txt");
115
116 fs::write(&file_path, b"Hello File!").await.unwrap();
117
118 let mut dir = fs::read_dir(temp_dir.path()).await.unwrap();
119
120 let first_entry = dir.next_entry().await.unwrap().unwrap();
121
122 assert_eq!(first_entry.path(), file_path);
123 assert_eq!(first_entry.file_name(), "a.txt");
124 assert!(first_entry.metadata().await.unwrap().is_file());
125 assert!(first_entry.file_type().await.unwrap().is_file());
126}
127