1/*
2 * Copyright (c) 2023 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 "ecmascript/compiler/aot_compiler_preprocessor.h"
17#include "ecmascript/platform/filesystem.h"
18#include "ecmascript/tests/test_helper.h"
19
20namespace panda::test {
21using namespace panda;
22using namespace panda::ecmascript;
23using namespace panda::ecmascript::kungfu;
24class CreateEmptyFileTest : public testing::Test {
25public:
26    static void SetUpTestCase()
27    {
28        GTEST_LOG_(INFO) << "SetUpTestCase";
29    }
30
31    static void TearDownTestCase()
32    {
33        GTEST_LOG_(INFO) << "TearDownCase";
34    }
35
36    void SetUp() override
37    {
38        temp_dir = filesystem::TempDirectoryPath() + "/create_empty_file_test";
39        filesystem::CreateDirectory(temp_dir);
40    }
41
42    void TearDown() override
43    {
44        filesystem::RemoveAll(temp_dir);
45    }
46
47protected:
48    std::string temp_dir;
49};
50
51HWTEST_F_L0(CreateEmptyFileTest, FileDoesNotExist)
52{
53    std::string file_path = temp_dir + "/dir1/dir2/entry.an";
54    filesystem::CreateEmptyFile(file_path);
55
56    EXPECT_TRUE(filesystem::Exists(file_path));
57}
58
59HWTEST_F_L0(CreateEmptyFileTest, FileExists)
60{
61    std::string file_path = temp_dir + "/entry.an";
62
63    std::ofstream ofs(file_path);
64    ofs << "test data";
65    ofs.close();
66
67    auto old_size = filesystem::FileSize(file_path);
68    EXPECT_GT(old_size, 0);
69
70    filesystem::CreateEmptyFile(file_path);
71
72    auto new_size = filesystem::FileSize(file_path);
73
74    EXPECT_TRUE(filesystem::Exists(file_path));
75    EXPECT_GT(old_size, 0);
76    EXPECT_GT(new_size, 0);
77}
78
79HWTEST_F_L0(CreateEmptyFileTest, DirectoryDoesNotExist)
80{
81    std::string dir_path = temp_dir + "/dir4/com.hm.app";
82    std::string file_path = dir_path + "/entry.an";
83    filesystem::CreateEmptyFile(file_path);
84
85    EXPECT_TRUE(filesystem::Exists(dir_path));
86    EXPECT_TRUE(filesystem::Exists(file_path));
87}
88} // namespace panda::test