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
|
#include <atomic>
#include <thread>
#include "catch2/catch.hpp"
#include "src/buildtool/common/artifact_description.hpp"
#include "src/buildtool/file_system/file_root.hpp"
#include "test/utils/container_matchers.hpp"
namespace {
auto const kBundlePath =
std::string{"test/buildtool/file_system/data/test_repo.bundle"};
auto const kTreeId = std::string{"e51a219a27b672ccf17abec7d61eb4d6e0424140"};
auto const kFooId = std::string{"19102815663d23f8b75a47e7a01965dcdc96468c"};
auto const kBarId = std::string{"ba0e162e1c47469e3fe4b393a8bf8c569f302116"};
[[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 " : "",
kBundlePath,
repo_path.string());
if (std::system(cmd.c_str()) == 0) {
return repo_path;
}
return std::nullopt;
}
void TestFileRootReadFile(FileRoot const& root) {
REQUIRE(root.Exists("foo"));
REQUIRE(root.IsFile("foo"));
auto foo = root.ReadFile("foo");
REQUIRE(foo);
CHECK(*foo == "foo");
REQUIRE(root.Exists("bar"));
REQUIRE(root.IsFile("bar"));
auto bar = root.ReadFile("bar");
REQUIRE(bar);
CHECK(*bar == "bar");
REQUIRE(root.Exists("baz"));
CHECK_FALSE(root.IsFile("baz"));
}
void TestFileRootReadEntries(FileRoot const& root,
std::string const& path,
bool has_baz) {
REQUIRE(root.Exists(path));
REQUIRE(root.IsDirectory(path));
auto entries = root.ReadDirectory(path);
CHECK_FALSE(entries.Empty());
CHECK(entries.Contains("foo"));
CHECK(entries.Contains("bar"));
if (has_baz) {
CHECK(entries.Contains("baz"));
}
CHECK_FALSE(entries.Contains("does_not_exist"));
}
void TestFileRootReadDirectory(FileRoot const& root) {
TestFileRootReadEntries(root, ".", true);
TestFileRootReadEntries(root, "baz", true);
TestFileRootReadEntries(root, "baz/baz", false);
}
void TestFileRootReadFileType(FileRoot const& root) {
auto foo_type = root.FileType("baz/foo");
REQUIRE(foo_type);
CHECK(*foo_type == ObjectType::File);
auto bar_type = root.FileType("baz/baz/bar");
REQUIRE(bar_type);
CHECK(*bar_type == ObjectType::Executable);
CHECK_FALSE(root.FileType("baz"));
CHECK_FALSE(root.FileType("does_not_exist"));
}
} // namespace
TEST_CASE("Creating file root", "[file_root]") {
SECTION("local root") {
auto root_path = CreateTestRepo(true);
REQUIRE(root_path);
CHECK(FileRoot{*root_path}.Exists("."));
CHECK_FALSE(FileRoot{"does_not_exist"}.Exists("."));
}
SECTION("git root") {
auto repo_path = CreateTestRepo(false);
REQUIRE(repo_path);
auto root = FileRoot::FromGit(*repo_path, kTreeId);
REQUIRE(root);
CHECK(root->Exists("."));
CHECK_FALSE(FileRoot::FromGit("does_not_exist", kTreeId));
}
}
TEST_CASE("Reading files", "[file_root]") {
SECTION("local root") {
auto root_path = CreateTestRepo(true);
REQUIRE(root_path);
TestFileRootReadFile(FileRoot{*root_path});
}
SECTION("git root") {
auto repo_path = CreateTestRepo(false);
REQUIRE(repo_path);
auto root = FileRoot::FromGit(*repo_path, kTreeId);
REQUIRE(root);
TestFileRootReadFile(*root);
}
}
TEST_CASE("Reading directories", "[file_root]") {
SECTION("local root") {
auto root_path = CreateTestRepo(true);
REQUIRE(root_path);
TestFileRootReadDirectory(FileRoot{*root_path});
}
SECTION("git root") {
auto repo_path = CreateTestRepo(false);
REQUIRE(repo_path);
auto root = FileRoot::FromGit(*repo_path, kTreeId);
REQUIRE(root);
TestFileRootReadDirectory(*root);
}
}
TEST_CASE("Reading blobs", "[file_root]") {
SECTION("local root") {
auto root_path = CreateTestRepo(true);
REQUIRE(root_path);
CHECK_FALSE(FileRoot{*root_path}.ReadBlob(kFooId));
}
SECTION("git root") {
auto repo_path = CreateTestRepo(false);
REQUIRE(repo_path);
auto root = FileRoot::FromGit(*repo_path, kTreeId);
REQUIRE(root);
auto foo = root->ReadBlob(kFooId);
REQUIRE(foo);
CHECK(*foo == "foo");
CHECK_FALSE(root->ReadBlob("does_not_exist"));
}
}
TEST_CASE("Reading file type", "[file_root]") {
SECTION("local root") {
auto root_path = CreateTestRepo(true);
REQUIRE(root_path);
TestFileRootReadFileType(FileRoot{*root_path});
}
SECTION("git root") {
auto repo_path = CreateTestRepo(false);
REQUIRE(repo_path);
auto root = FileRoot::FromGit(*repo_path, kTreeId);
REQUIRE(root);
TestFileRootReadFileType(*root);
}
}
TEST_CASE("Creating artifact descriptions", "[file_root]") {
SECTION("local root") {
auto root_path = CreateTestRepo(true);
REQUIRE(root_path);
auto root = FileRoot{*root_path};
auto desc = root.ToArtifactDescription("baz/foo", "repo");
REQUIRE(desc);
CHECK(*desc ==
ArtifactDescription(std::filesystem::path{"baz/foo"}, "repo"));
CHECK(root.ToArtifactDescription("does_not_exist", "repo"));
}
SECTION("git root") {
auto repo_path = CreateTestRepo(false);
REQUIRE(repo_path);
auto root = FileRoot::FromGit(*repo_path, kTreeId);
REQUIRE(root);
auto foo = root->ToArtifactDescription("baz/foo", "repo");
REQUIRE(foo);
CHECK(*foo == ArtifactDescription{
ArtifactDigest{kFooId, 3}, ObjectType::File, "repo"});
auto bar = root->ToArtifactDescription("baz/baz/bar", "repo");
REQUIRE(bar);
CHECK(*bar == ArtifactDescription{ArtifactDigest{kBarId, 3},
ObjectType::Executable,
"repo"});
CHECK_FALSE(root->ToArtifactDescription("baz", "repo"));
CHECK_FALSE(root->ToArtifactDescription("does_not_exist", "repo"));
}
}
|