1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #![cfg(feature = "fs")]
15 
16 use ylong_runtime::fs::{create_dir, create_dir_all, read_dir, remove_dir, remove_dir_all, File};
17 
18 /// SDV test cases for directory operations.
19 ///
20 /// # Brief
21 /// 1. Create a new directory.
22 /// 2. Create two files to read.
23 /// 3. Read the directory and check the name of files.
24 /// 4. Delete the directory and files in it.
25 #[test]
sdv_async_dirnull26 fn sdv_async_dir() {
27     let handle = ylong_runtime::spawn(async move {
28         let _ = create_dir("dir_test").await;
29         File::create("dir_test/test1.txt").await.unwrap();
30         File::create("dir_test/test2.txt").await.unwrap();
31         let mut dir = read_dir("dir_test").await.unwrap();
32         let entry = dir.next().await.unwrap().unwrap();
33         assert!(!entry.file_type().await.unwrap().is_dir());
34         assert!(entry.file_type().await.unwrap().is_file());
35         assert!(entry.file_name().into_string().unwrap().contains("test"));
36         let entry = dir.next().await.unwrap().unwrap();
37         assert!(!entry.metadata().await.unwrap().is_dir());
38         assert!(entry.metadata().await.unwrap().is_file());
39         assert!(!entry.metadata().await.unwrap().permissions().readonly());
40         assert!(entry.file_name().into_string().unwrap().contains("test"));
41         assert!(dir.next().await.unwrap().is_none());
42         assert!(remove_dir_all("dir_test").await.is_ok());
43     });
44     ylong_runtime::block_on(handle).unwrap();
45 }
46 
47 /// SDV test cases for creating and removing directories.
48 ///
49 /// # Brief
50 /// 1. Create a new directory at the given path.
51 /// 2. Create a new directory at the given path that is used.
52 /// 3. Create a new directory at the given path and whose parent directory is
53 ///    missing.
54 /// 4. Create a new directory and all of its parents.
55 /// 5. Remove a directory at the given path.
56 /// 6. Remove a directory that does not exist.
57 /// 7. Remove a directory that is not a directory.
58 /// 8. Remove a directory that is not empty.
59 /// 9. Remove a directory and all of its contents.
60 #[test]
sdv_async_dir_create_removenull61 fn sdv_async_dir_create_remove() {
62     let handle = ylong_runtime::spawn(async move {
63         assert!(create_dir("dir_test1").await.is_ok());
64         assert!(create_dir("dir_test1").await.is_err());
65         assert!(create_dir("dir_test2/dir_test_child").await.is_err());
66         assert!(create_dir_all("dir_test2/dir_test_child").await.is_ok());
67         assert!(remove_dir("dir_test1").await.is_ok());
68         assert!(remove_dir("dir_test1").await.is_err());
69         assert!(remove_dir("async_dir").await.is_err());
70         assert!(remove_dir("dir_test2").await.is_err());
71         assert!(remove_dir_all("dir_test2").await.is_ok());
72     });
73     ylong_runtime::block_on(handle).unwrap();
74 }
75