summaryrefslogtreecommitdiff
path: root/test/buildtool/file_system/directory_entries.test.cpp
blob: 4390f80964483541301b691057c7f568a849a832 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <atomic>
#include <cstdlib>
#include <filesystem>
#include <functional>
#include <optional>
#include <string>
#include <unordered_set>

#include "catch2/catch_test_macros.hpp"
#include "fmt/core.h"
#include "src/buildtool/file_system/file_root.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "test/utils/shell_quoting.hpp"

namespace {

auto const kBundlePath =
    std::string{"test/buildtool/file_system/data/test_repo.bundle"};

auto const kBundleSymPath =
    std::string{"test/buildtool/file_system/data/test_repo_symlinks.bundle"};

[[nodiscard]] auto GetTestDir() -> std::filesystem::path {
    auto* tmp_dir = std::getenv("TEST_TMPDIR");
    if (tmp_dir != nullptr) {
        return tmp_dir;
    }
    return FileSystemManager::GetCurrentDirectory() /
           "test/buildtool/file_system";
}

[[nodiscard]] auto CreateTestRepo(bool do_checkout = false)
    -> std::optional<std::filesystem::path> {
    static std::atomic<int> counter{};
    auto repo_path =
        GetTestDir() / "test_repo" /
        std::filesystem::path{std::to_string(counter++)}.filename();
    auto cmd = fmt::format("git clone {}{} {}",
                           do_checkout ? "--branch master " : "",
                           QuoteForShell(kBundlePath),
                           QuoteForShell(repo_path.string()));
    if (std::system(cmd.c_str()) == 0) {
        return repo_path;
    }
    return std::nullopt;
}

[[nodiscard]] auto CreateTestRepoSymlinks(bool do_checkout = false)
    -> std::optional<std::filesystem::path> {
    static std::atomic<int> counter{};
    auto repo_path =
        GetTestDir() / "test_repo_symlinks" /
        std::filesystem::path{std::to_string(counter++)}.filename();
    auto cmd = fmt::format("git clone {}{} {}",
                           do_checkout ? "--branch master " : "",
                           QuoteForShell(kBundleSymPath),
                           QuoteForShell(repo_path.string()));
    if (std::system(cmd.c_str()) == 0) {
        return repo_path;
    }
    return std::nullopt;
}

}  // namespace

TEST_CASE("Get entries of a directory", "[directory_entries]") {
    const std::unordered_set<std::string> reference_entries{
        "test_repo.bundle",
        "test_repo_symlinks.bundle",
        "empty_executable",
        "subdir",
        "example_file"};

    const auto dir = std::filesystem::path("test/buildtool/file_system/data");

    auto fs_root = FileRoot();
    auto dir_entries = fs_root.ReadDirectory(dir);
    CHECK(dir_entries.ContainsBlob("test_repo.bundle"));
    CHECK(dir_entries.ContainsBlob("test_repo_symlinks.bundle"));
    CHECK(dir_entries.ContainsBlob("empty_executable"));
    CHECK(dir_entries.ContainsBlob("example_file"));
    {
        // all the entries returned by FilesIterator are files,
        // are contained in reference_entries,
        // and are 4
        auto counter = 0;
        for (const auto& x : dir_entries.FilesIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 4);
    }
    {
        // all the entries returned by DirectoriesIterator are not files (e.g.,
        // trees),
        // are contained in reference_entries,
        // and are 1

        auto counter = 0;
        for (const auto& x : dir_entries.DirectoriesIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK_FALSE(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 1);
    }
}

TEST_CASE("Get entries of a git tree", "[directory_entries]") {
    auto reference_entries =
        std::unordered_set<std::string>{"foo", "bar", "baz", ".git"};
    auto repo = *CreateTestRepo(true);
    auto fs_root = FileRoot();
    auto dir_entries = fs_root.ReadDirectory(repo);
    CHECK(dir_entries.ContainsBlob("bar"));
    CHECK(dir_entries.ContainsBlob("foo"));
    CHECK_FALSE(dir_entries.ContainsBlob("baz"));
    {
        // all the entries returned by FilesIterator are files,
        // are contained in reference_entries,
        // and are 2 (foo, and bar)
        auto counter = 0;
        for (const auto& x : dir_entries.FilesIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 2);
    }
    {
        // all the entries returned by DirectoriesIterator are not files (e.g.,
        // trees),
        // are contained in reference_entries,
        // and are 2 (baz, and .git)
        auto counter = 0;
        for (const auto& x : dir_entries.DirectoriesIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK_FALSE(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 2);
    }
}

TEST_CASE("Get entries of an empty directory", "[directory_entries]") {
    // CreateDirectoryEntriesMap returns
    // FileRoot::DirectoryEntries{FileRoot::DirectoryEntries::pairs_t{}} in case
    // of a missing directory, which represents an empty directory

    auto dir_entries =
        FileRoot::DirectoryEntries{FileRoot::DirectoryEntries::pairs_t{}};
    // of course, no files should be there
    CHECK_FALSE(dir_entries.ContainsBlob("test_repo.bundle"));
    {
        // FilesIterator should be an empty range
        auto counter = 0;
        for (const auto& x : dir_entries.FilesIterator()) {
            CHECK_FALSE(dir_entries.ContainsBlob(x));  // should never be called
            ++counter;
        }

        CHECK(counter == 0);
    }
    {
        // DirectoriesIterator should be an empty range
        auto counter = 0;
        for (const auto& x : dir_entries.DirectoriesIterator()) {
            CHECK(dir_entries.ContainsBlob(x));  // should never be called
            ++counter;
        }

        CHECK(counter == 0);
    }
}

TEST_CASE("Get ignore-special entries of a git tree with symlinks",
          "[directory_entries]") {
    auto reference_entries =
        std::unordered_set<std::string>{"foo", "bar", "baz", ".git"};
    auto repo = *CreateTestRepoSymlinks(true);
    auto fs_root = FileRoot(/*ignore_special=*/true);
    auto dir_entries = fs_root.ReadDirectory(repo);
    CHECK(dir_entries.ContainsBlob("bar"));
    CHECK(dir_entries.ContainsBlob("foo"));
    CHECK_FALSE(dir_entries.ContainsBlob("baz"));
    {
        // all the entries returned by FilesIterator are files,
        // are contained in reference_entries,
        // and are 2 (foo, and bar)
        auto counter = 0;
        for (const auto& x : dir_entries.FilesIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 2);
    }
    {
        // all the entries returned by DirectoriesIterator are not files (e.g.,
        // trees),
        // are contained in reference_entries,
        // and are 2 (baz, and .git)
        auto counter = 0;
        for (const auto& x : dir_entries.DirectoriesIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK_FALSE(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 2);
    }
}

TEST_CASE("Get entries of a git tree with symlinks", "[directory_entries]") {
    auto reference_entries = std::unordered_set<std::string>{
        "foo", "bar", "baz", "foo_l", "baz_l", ".git"};
    auto repo = *CreateTestRepoSymlinks(true);
    auto fs_root = FileRoot(/*ignore_special=*/false);
    auto dir_entries = fs_root.ReadDirectory(repo);
    CHECK(dir_entries.ContainsBlob("bar"));
    CHECK(dir_entries.ContainsBlob("foo"));
    CHECK_FALSE(dir_entries.ContainsBlob("baz"));
    CHECK(dir_entries.ContainsBlob("foo_l"));
    CHECK(dir_entries.ContainsBlob("baz_l"));
    {
        // all the entries returned by FilesIterator are files,
        // are contained in reference_entries,
        // and are 2 (foo, and bar)
        auto counter = 0;
        for (const auto& x : dir_entries.FilesIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 2);
    }
    {
        // all the entries returned by SymlinksIterator are symlinks,
        // are contained in reference_entries,
        // and are 2 (foo_l, and baz_l)
        auto counter = 0;
        for (const auto& x : dir_entries.SymlinksIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 2);
    }
    {
        // all the entries returned by DirectoriesIterator are not files (e.g.,
        // trees),
        // are contained in reference_entries,
        // and are 2 (baz, and .git)
        auto counter = 0;
        for (const auto& x : dir_entries.DirectoriesIterator()) {
            REQUIRE(reference_entries.contains(x));
            CHECK_FALSE(dir_entries.ContainsBlob(x));
            ++counter;
        }

        CHECK(counter == 2);
    }
}