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 using namespace std; 23 24 namespace { 25 class NativeFileSystemTest : public ::testing::Test { 26 public: NativeFileSystemTest()27 NativeFileSystemTest() {} ~NativeFileSystemTest()28 ~NativeFileSystemTest() {} 29 protected: 30 // 在整个测试夹具类执行前执行一次初始化操作 SetUpTestCase()31 static void SetUpTestCase() 32 { 33 char buffer[FILENAME_MAX]; 34 if (getcwd(buffer, FILENAME_MAX) != nullptr) { 35 currDir = std::string(buffer); 36 childDir = currDir + "/mytestdirname"; 37 int status = mkdir(childDir.c_str(), 0777); // 0777 表示所有用户有读、写、执行权限 38 if (status != 0) { 39 printf("Error creating folder!\n"); 40 } 41 } else { 42 printf("error: getcwd failed"); 43 } 44 } 45 // 在整个测试夹具类执行后执行一次清理操作 TearDownTestCase()46 static void TearDownTestCase() 47 { 48 std::filesystem::remove(childDir.c_str()); 49 } 50 51 static std::string currDir; 52 static std::string childDir; 53 }; 54 55 std::string NativeFileSystemTest::currDir = ""; 56 std::string NativeFileSystemTest::childDir = ""; 57 TEST_F(NativeFileSystemTest,FindSubfolderByNameTest)58 TEST_F(NativeFileSystemTest, FindSubfolderByNameTest) 59 { 60 std::string filePath = OHOS::Ide::NativeFileSystem::FindSubfolderByName(currDir, "mytestdirname"); 61 EXPECT_EQ(filePath, childDir); 62 } 63 }