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