File: FileSystemUtilsTest.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (202 lines) | stat: -rw-r--r-- 8,394 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "BATesting.h"

#include "Base/Util/PathUtil.h"
#include "Tests/GTestWrapper/google_test.h"
#include <algorithm>
#include <filesystem>
#include <fstream>

class FileSystemUtilsTest : public ::testing::Test {
protected:
    void SetUp() override
    {
        Base::Path::createDirectories(BATesting::TestOutDir_Base);
        ASSERT_TRUE(std::filesystem::exists(BATesting::TestOutDir_Base));
        ASSERT_TRUE(assureNonExistingTestCasePath());
    }

    void TearDown() override
    {
        ASSERT_TRUE(assureNonExistingTestCasePath());
        // by design no removing of BATesting::TestOutDir_Base which may have been
        // created in SetUp
    }

    std::string testCaseFolderName() const
    {
        const auto* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
        return test_info->test_suite_name() + std::string("_") + test_info->name();
    }

    std::string testCasePath() const
    {
        return Base::Path::jointPath(BATesting::TestOutDir_Base, testCaseFolderName());
    }

    //! Assures the test case specific path is not existent. Removes it if necessary.
    bool assureNonExistingTestCasePath() const
    {
        // current dir must not be the dir to be removed
        std::filesystem::current_path(BATesting::TestOutDir_Base);
        std::filesystem::remove_all(testCasePath());
        return !std::filesystem::exists(testCasePath());
    }
};


TEST_F(FileSystemUtilsTest, extension)
{
    EXPECT_EQ(Base::Path::extension(""), "");
    EXPECT_EQ(Base::Path::extension("/home/james/"), "");
    EXPECT_EQ(Base::Path::extension("/home/james/."), "");
    EXPECT_EQ(Base::Path::extension("/home/james/.."), "");
    EXPECT_EQ(Base::Path::extension("/home/james/.hidden"), "");
    EXPECT_EQ(Base::Path::extension("/home/james/.hidden.txt"), ".txt");
    EXPECT_EQ(Base::Path::extension("/home/james/file.txt"), ".txt");
    EXPECT_EQ(Base::Path::extension("/home/james/file.txt.gz"), ".gz");
    EXPECT_EQ(Base::Path::extension("/home/james/file.txt.GZ"), ".GZ");
}

TEST_F(FileSystemUtilsTest, hasExtension)
{
    EXPECT_TRUE(Base::Path::hasExtension("", ""));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/", ""));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/.", ""));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/..", ""));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/.hidden", ""));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/.hidden.txt", ".txt"));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/file.txt", ".txt"));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/file.TXT", ".txt"));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/file.txt.gz", ".gz"));
    EXPECT_TRUE(Base::Path::hasExtension("/home/james/file.TXT.GZ", ".gz"));
}

TEST_F(FileSystemUtilsTest, extensions)
{
    EXPECT_EQ(Base::Path::extensions(""), "");
    EXPECT_EQ(Base::Path::extensions("/home/james/"), "");
    EXPECT_EQ(Base::Path::extensions("/home/james/."), "");
    EXPECT_EQ(Base::Path::extensions("/home/james/.."), "");
    EXPECT_EQ(Base::Path::extensions("/home/james/.hidden"), "");
    EXPECT_EQ(Base::Path::extensions("/home/james/.hidden.txt"), ".txt");
    EXPECT_EQ(Base::Path::extensions("/home/james/.hidden.txt.gz"), ".txt.gz");
    EXPECT_EQ(Base::Path::extensions("/home/james/f"), "");
    EXPECT_EQ(Base::Path::extensions("/home/james/f.txt"), ".txt");
    EXPECT_EQ(Base::Path::extensions("/home/james/file.txt"), ".txt");
    EXPECT_EQ(Base::Path::extensions("/home/james/file.txt.gz"), ".txt.gz");
}

TEST_F(FileSystemUtilsTest, filename)
{
    EXPECT_EQ(Base::Path::filename(""), "");
    EXPECT_EQ(Base::Path::filename("/home/james/"), "");
    EXPECT_EQ(Base::Path::filename("/home/james/."), ".");   // sic! according to C++17
    EXPECT_EQ(Base::Path::filename("/home/james/.."), ".."); // sic! according to C++17
    EXPECT_EQ(Base::Path::filename("/home/james/.hidden"), ".hidden");
    EXPECT_EQ(Base::Path::filename("/home/james/file.txt"), "file.txt");
    EXPECT_EQ(Base::Path::filename("/home/james/file"), "file");
}

TEST_F(FileSystemUtilsTest, stem)
{
    EXPECT_EQ(Base::Path::stem(""), "");
    EXPECT_EQ(Base::Path::stem("/home/james/"), "");
    EXPECT_EQ(Base::Path::stem("/home/james/."), ".");
    EXPECT_EQ(Base::Path::stem("/home/james/.."), "..");
    EXPECT_EQ(Base::Path::stem("/home/james/.hidden"), ".hidden");
    EXPECT_EQ(Base::Path::stem("/home/james/.hidden.txt"), ".hidden");
    EXPECT_EQ(Base::Path::stem("/home/james/.hidden.txt.gz"), ".hidden.txt");
    EXPECT_EQ(Base::Path::stem("/home/james/filename.txt"), "filename");
    EXPECT_EQ(Base::Path::stem("/home/james/filename.txt.gz"), "filename.txt");
}

TEST_F(FileSystemUtilsTest, StemAndExtension)
{
    EXPECT_EQ(Base::Path::stem_ext(""), "");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/"), "");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/."), ".");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/.."), "..");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/.hidden"), ".hidden");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/.hidden.txt"), ".hidden");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/.hidden.txt.gz"), ".hidden");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/filename"), "filename");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/filename.txt"), "filename");
    EXPECT_EQ(Base::Path::stem_ext("/home/james/filename.txt.gz"), "filename");
}

TEST_F(FileSystemUtilsTest, createDirectory)
{
    // with absolute path
    EXPECT_TRUE(Base::Path::createDirectories(testCasePath()));
    EXPECT_FALSE(Base::Path::createDirectories(testCasePath()));

    // with relative path
    std::filesystem::current_path(testCasePath());
    EXPECT_TRUE(Base::Path::createDirectories("sub"));
    EXPECT_FALSE(Base::Path::createDirectories("sub"));
    EXPECT_TRUE(std::filesystem::exists("sub"));
    EXPECT_TRUE(std::filesystem::exists(Base::Path::jointPath(testCasePath(), "sub")));
}

TEST_F(FileSystemUtilsTest, createDirectories)
{
    // with absolute path
    const auto sub1 = Base::Path::jointPath(testCasePath(), "sub1");
    const auto sub2 = Base::Path::jointPath(sub1, "sub2");

    EXPECT_TRUE(Base::Path::createDirectories(sub2));
    EXPECT_FALSE(Base::Path::createDirectories(sub2));
    EXPECT_TRUE(std::filesystem::exists(testCasePath()));
    EXPECT_TRUE(std::filesystem::exists(sub1));
    EXPECT_TRUE(std::filesystem::exists(sub2));

    // with relative path
    const auto* const sub4 = "sub3/sub4";
    std::filesystem::current_path(sub2);
    EXPECT_TRUE(Base::Path::createDirectories(sub4));
    EXPECT_FALSE(Base::Path::createDirectories(sub4));
    EXPECT_TRUE(std::filesystem::exists("sub3"));
    EXPECT_TRUE(std::filesystem::exists("sub3/sub4"));
    EXPECT_TRUE(std::filesystem::exists(Base::Path::jointPath(sub2, sub4)));
}

TEST_F(FileSystemUtilsTest, jointPath)
{
#ifdef _WIN32
    EXPECT_EQ(Base::Path::jointPath("a", "b"), "a\\b");
#else
    EXPECT_EQ(Base::Path::jointPath("a", "b"), "a/b");
#endif

    EXPECT_FAILED_ASSERT(Base::Path::jointPath("", ""));
    EXPECT_FAILED_ASSERT(Base::Path::jointPath("a", ""));
    EXPECT_EQ(Base::Path::jointPath("", "b"), "b");
}

TEST_F(FileSystemUtilsTest, filesInDirectoryIsFileExists)
{
    EXPECT_ANY_THROW(Base::Path::filesInDirectory("non-existent/directory"));

    // assure clean preconditions
    ASSERT_TRUE(Base::Path::createDirectories(testCasePath()));
    ASSERT_TRUE(Base::Path::filesInDirectory(testCasePath()).empty());

    // create a few files
    std::filesystem::current_path(testCasePath());
    std::ofstream("file1.txt");
    std::ofstream("file2.txt");
    std::ofstream("file3.txt");

    EXPECT_EQ(Base::Path::filesInDirectory(testCasePath()).size(), 3uL); // abs
    EXPECT_EQ(Base::Path::filesInDirectory(".").size(), 3uL);            // rel
    const auto files = Base::Path::filesInDirectory(".");
    EXPECT_TRUE(std::find(files.begin(), files.end(), "file1.txt") != files.end());
    EXPECT_TRUE(std::find(files.begin(), files.end(), "file2.txt") != files.end());
    EXPECT_TRUE(std::find(files.begin(), files.end(), "file3.txt") != files.end());

    // tests for IsFileExists
    EXPECT_TRUE(Base::Path::IsFileExists("file1.txt"));
    EXPECT_TRUE(Base::Path::IsFileExists("file2.txt"));
    EXPECT_TRUE(Base::Path::IsFileExists("file3.txt"));
    EXPECT_FALSE(Base::Path::IsFileExists("nonexisting.txt"));
}