-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_bmi_cache.cpp
More file actions
163 lines (138 loc) · 5.75 KB
/
Copy pathtest_bmi_cache.cpp
File metadata and controls
163 lines (138 loc) · 5.75 KB
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
#include <gtest/gtest.h>
#include <fcntl.h>
#include <sys/file.h>
#include <unistd.h>
import std;
import mcpp.bmi_cache;
using namespace mcpp::bmi_cache;
namespace {
struct Tmp {
std::filesystem::path path;
Tmp() {
auto base = std::filesystem::temp_directory_path()
/ std::format("mcpp_bmi_cache_test_{}", std::random_device{}());
std::filesystem::create_directories(base);
path = base;
}
~Tmp() {
std::error_code ec;
std::filesystem::remove_all(path, ec);
}
};
CacheKey makeKey(const std::filesystem::path& home) {
return CacheKey{
.mcppHome = home,
.fingerprint = "deadbeef0123abcd",
.indexName = "mcpp-index",
.packageName = "mcpplibs.cmdline",
.version = "0.0.1",
};
}
void writeFile(const std::filesystem::path& p, std::string_view body) {
std::filesystem::create_directories(p.parent_path());
std::ofstream(p) << body;
}
} // namespace
TEST(BmiCache, KeyDirLayoutMatchesDocs26) {
auto k = makeKey("/home/u/.mcpp");
EXPECT_EQ(k.dir().string(),
"/home/u/.mcpp/bmi/deadbeef0123abcd/deps/mcpp-index/[email protected]");
EXPECT_EQ(k.manifestFile().filename().string(), "manifest.txt");
EXPECT_EQ(k.gcmDir().filename().string(), "gcm.cache");
EXPECT_EQ(k.objDir().filename().string(), "obj");
}
TEST(BmiCache, IsCachedFalseWhenManifestMissing) {
Tmp t;
auto k = makeKey(t.path);
EXPECT_FALSE(is_cached(k));
}
TEST(BmiCache, PopulateThenStageRoundTrip) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
std::filesystem::create_directories(project / "gcm.cache");
std::filesystem::create_directories(project / "obj");
writeFile(project / "gcm.cache" / "mcpplibs.cmdline.gcm", "GCM-A");
writeFile(project / "gcm.cache" / "mcpplibs.cmdline-options.gcm", "GCM-B");
writeFile(project / "obj" / "cmdline.m.o", "OBJ-A");
DepArtifacts arts {
.gcmFiles = { "mcpplibs.cmdline.gcm", "mcpplibs.cmdline-options.gcm" },
.objFiles = { "cmdline.m.o" },
};
auto k = makeKey(home);
auto pop = populate_from(k, project, arts);
ASSERT_TRUE(pop) << pop.error();
EXPECT_TRUE(std::filesystem::exists(k.manifestFile()));
EXPECT_TRUE(std::filesystem::exists(k.gcmDir() / "mcpplibs.cmdline.gcm"));
EXPECT_TRUE(std::filesystem::exists(k.gcmDir() / "mcpplibs.cmdline-options.gcm"));
EXPECT_TRUE(std::filesystem::exists(k.objDir() / "cmdline.m.o"));
EXPECT_TRUE(is_cached(k));
// Round-trip: stage into a fresh project dir.
auto project2 = t.path / "proj2" / "target";
auto staged = stage_into(k, project2);
ASSERT_TRUE(staged) << staged.error();
EXPECT_EQ(staged->gcmFiles.size(), 2u);
EXPECT_EQ(staged->objFiles.size(), 1u);
EXPECT_TRUE(std::filesystem::exists(project2 / "gcm.cache" / "mcpplibs.cmdline.gcm"));
EXPECT_TRUE(std::filesystem::exists(project2 / "obj" / "cmdline.m.o"));
// Staged file content must match original.
std::ifstream is(project2 / "obj" / "cmdline.m.o");
std::string body((std::istreambuf_iterator<char>(is)), {});
EXPECT_EQ(body, "OBJ-A");
}
TEST(BmiCache, IsCachedFalseWhenSentinelExistsButFileMissing) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
std::filesystem::create_directories(project / "gcm.cache");
std::filesystem::create_directories(project / "obj");
writeFile(project / "gcm.cache" / "lib.gcm", "G");
writeFile(project / "obj" / "lib.m.o", "O");
DepArtifacts arts { .gcmFiles = {"lib.gcm"}, .objFiles = {"lib.m.o"} };
auto k = makeKey(home);
ASSERT_TRUE(populate_from(k, project, arts));
ASSERT_TRUE(is_cached(k));
// Delete one cached file → is_cached must return false even though manifest still exists.
std::filesystem::remove(k.objDir() / "lib.m.o");
EXPECT_FALSE(is_cached(k));
}
TEST(BmiCache, PopulateFailsIfBuildOutputMissing) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
std::filesystem::create_directories(project / "gcm.cache");
DepArtifacts arts { .gcmFiles = {"missing.gcm"}, .objFiles = {} };
auto k = makeKey(home);
auto pop = populate_from(k, project, arts);
EXPECT_FALSE(pop);
EXPECT_NE(pop.error().find("expected build output missing"), std::string::npos);
}
// M4 #9: when an external holder takes the .lock, populate_from must skip
// (returns success but does NOT clobber the directory).
TEST(BmiCache, PopulateSkipsWhenLockHeld) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
std::filesystem::create_directories(project / "gcm.cache");
std::filesystem::create_directories(project / "obj");
writeFile(project / "gcm.cache" / "lib.gcm", "G2");
writeFile(project / "obj" / "lib.m.o", "O2");
auto k = makeKey(home);
// Take the lock manually before populate runs.
std::filesystem::create_directories(k.dir());
auto lockPath = k.dir() / ".lock";
int fd = ::open(lockPath.c_str(), O_CREAT | O_RDWR, 0644);
ASSERT_GE(fd, 0);
ASSERT_EQ(::flock(fd, LOCK_EX | LOCK_NB), 0);
DepArtifacts arts { .gcmFiles = {"lib.gcm"}, .objFiles = {"lib.m.o"} };
auto pop = populate_from(k, project, arts);
EXPECT_TRUE(pop) << "should silently skip when lock is held";
// manifest.txt must NOT have been written by the second writer.
EXPECT_FALSE(std::filesystem::exists(k.manifestFile()));
::flock(fd, LOCK_UN);
::close(fd);
// After lock released, a fresh populate should succeed.
auto pop2 = populate_from(k, project, arts);
ASSERT_TRUE(pop2) << pop2.error();
EXPECT_TRUE(std::filesystem::exists(k.manifestFile()));
}