1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <filesystem>
17#include <unistd.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20#include "gtest/gtest.h"
21#include "NativeFileSystem.h"
22
23namespace {
24    class NativeFileSystemTest : public ::testing::Test {
25    public:
26        NativeFileSystemTest() {}
27        ~NativeFileSystemTest() {}
28    protected:
29        // 在整个测试夹具类执行前执行一次初始化操作
30        static void SetUpTestCase()
31        {
32            char buffer[FILENAME_MAX];
33            if (getcwd(buffer, FILENAME_MAX) != nullptr) {
34                currDir = std::string(buffer);
35                childDir = currDir + "/mytestdirname";
36                int status = mkdir(childDir.c_str(), 0777); // 0777 表示所有用户有读、写、执行权限
37                if (status != 0) {
38                    printf("Error creating folder!\n");
39                }
40            } else {
41                printf("error: getcwd failed");
42            }
43        }
44        // 在整个测试夹具类执行后执行一次清理操作
45        static void TearDownTestCase()
46        {
47            std::filesystem::remove(childDir.c_str());
48        }
49
50        static std::string currDir;
51        static std::string childDir;
52    };
53
54    std::string NativeFileSystemTest::currDir = "";
55    std::string NativeFileSystemTest::childDir = "";
56
57    TEST_F(NativeFileSystemTest, FindSubfolderByNameTest)
58    {
59        std::string filePath = OHOS::Ide::NativeFileSystem::FindSubfolderByName(currDir, "mytestdirname");
60        EXPECT_EQ(filePath, childDir);
61    }
62}