-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_provisions.cpp
More file actions
353 lines (310 loc) · 15.5 KB
/
Copy pathtest_provisions.cpp
File metadata and controls
353 lines (310 loc) · 15.5 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <gtest/gtest.h>
import std;
import mcpp.build.provisions;
// A build-time provision — a host tool, a host build rule, a dependency's
// directory — is something a dependency hands to its consumer's build PROGRAM.
// Before #359 each kind invented its own reach, and none could be re-exported,
// so a library could not stand up a toolchain on its user's behalf.
//
// Two properties are load-bearing and both are supply-chain properties, which
// is why they are pinned here rather than left to an e2e:
// * nothing propagates unless the edge says `reexport = true`;
// * an unqualified name is bound by a fixed ladder, never by "whoever was
// appended last" — otherwise two libraries that have never heard of each
// other could decide which `protoc` runs.
namespace prov = mcpp::build::provisions;
namespace {
struct Edge {
std::size_t consumerPackageIndex = 0;
std::size_t dependencyPackageIndex = 0;
std::vector<std::string> requestedTools;
bool hostModule = false;
bool reexport = false;
};
bool sees_tool(const prov::Propagation& p, std::size_t consumer,
std::size_t provider, std::string_view tool) {
if (consumer >= p.visible.size()) return false;
return p.visible[consumer].contains(
prov::Provision{ prov::Kind::Tool, provider, std::string(tool) });
}
bool sees_dir(const prov::Propagation& p, std::size_t consumer,
std::size_t provider) {
if (consumer >= p.visible.size()) return false;
return p.visible[consumer].contains(
prov::Provision{ prov::Kind::DepDir, provider, {} });
}
} // namespace
// ── Table integrity ────────────────────────────────────────────────────────
TEST(Provisions, EveryKindAnswersBothQuestions) {
// The table exists so a new provision kind cannot be added without someone
// stating how far it travels. A row that answered neither would compile
// fine and silently pick a default, which is the exact failure #359 fixed.
std::set<prov::Kind> seen;
for (auto const& d : prov::kTable) {
EXPECT_FALSE(d.name.empty());
EXPECT_TRUE(d.needsReexport) << d.name;
seen.insert(d.kind);
}
EXPECT_EQ(seen.size(), 3u);
EXPECT_TRUE(prov::def_of(prov::Kind::Tool).bareAddressable);
EXPECT_TRUE(prov::def_of(prov::Kind::DepDir).bareAddressable);
// A host module is addressed by `import <name>;` — the compiler resolves
// it, so there is no bare-name channel for mcpp to bind.
EXPECT_FALSE(prov::def_of(prov::Kind::HostModule).bareAddressable);
}
// ── Propagation ────────────────────────────────────────────────────────────
TEST(Provisions, ToolIsVisibleToTheRequesterWithoutAnyReexport) {
// 0=root, 1=protobuf. root --tools=[protoc]--> protobuf
std::vector<Edge> edges{ { 0, 1, { "protoc" }, false, false } };
auto p = prov::propagate(edges, 2);
EXPECT_TRUE(sees_tool(p, 0, 1, "protoc"));
// Nothing to export: the root has no consumers, and it never said it
// re-exported anything anyway.
EXPECT_TRUE(p.exported[0].empty());
}
TEST(Provisions, WithoutReexportALibrarysToolStaysWithTheLibrary) {
// 0=app, 1=lib, 2=protobuf. app -> lib --tools=[protoc]--> protobuf
std::vector<Edge> edges{
{ 0, 1, {}, false, false },
{ 1, 2, { "protoc" }, false, /*reexport=*/false },
};
auto p = prov::propagate(edges, 3);
EXPECT_TRUE(sees_tool(p, 1, 2, "protoc"));
// THE test: an ordinary dependency must not push entries into its
// consumer's tool namespace. That is a supply-chain rule, not a
// convenience — which is why `reexport` defaults to false.
EXPECT_FALSE(sees_tool(p, 0, 2, "protoc"));
}
TEST(Provisions, ReexportHandsTheToolToTheLibrarysConsumer) {
std::vector<Edge> edges{
{ 0, 1, {}, false, false },
{ 1, 2, { "protoc" }, false, /*reexport=*/true },
};
auto p = prov::propagate(edges, 3);
EXPECT_TRUE(sees_tool(p, 0, 2, "protoc"));
EXPECT_TRUE(sees_tool(p, 1, 2, "protoc"));
}
TEST(Provisions, ReexportTravelsFurtherOnlyWhenEachHopSaysSo) {
// 0=app, 1=mid, 2=lib, 3=protobuf.
// lib re-exports protoc to mid; mid does NOT re-export to app.
std::vector<Edge> edges{
{ 0, 1, {}, false, /*reexport=*/false },
{ 1, 2, {}, false, /*reexport=*/false },
{ 2, 3, { "protoc" }, false, /*reexport=*/true },
};
auto p = prov::propagate(edges, 4);
EXPECT_TRUE(sees_tool(p, 2, 3, "protoc"));
EXPECT_TRUE(sees_tool(p, 1, 3, "protoc"));
EXPECT_FALSE(sees_tool(p, 0, 3, "protoc"));
// Flip the middle hop and it reaches all the way. Each package decides
// what IT hands to ITS consumers; nothing decides on someone else's
// behalf.
edges[1].reexport = true;
auto q = prov::propagate(edges, 4);
EXPECT_TRUE(sees_tool(q, 0, 3, "protoc"));
}
TEST(Provisions, HostModuleFollowsTheSameRule) {
std::vector<Edge> edges{
{ 0, 1, {}, false, false },
{ 1, 2, {}, /*hostModule=*/true, /*reexport=*/true },
};
auto p = prov::propagate(edges, 3);
const prov::Provision rule{ prov::Kind::HostModule, 2, {} };
EXPECT_TRUE(p.visible[0].contains(rule));
EXPECT_TRUE(p.visible[1].contains(rule));
}
TEST(Provisions, EveryEdgeExposesTheDependencyDirectory) {
// dep_dir() has always worked for a DIRECT dependency without anyone
// declaring anything; that stays true. Re-export is what extends it.
std::vector<Edge> edges{
{ 0, 1, {}, false, false },
{ 1, 2, {}, false, /*reexport=*/true },
{ 1, 3, {}, false, /*reexport=*/false },
};
auto p = prov::propagate(edges, 4);
EXPECT_TRUE(sees_dir(p, 0, 1));
EXPECT_TRUE(sees_dir(p, 0, 2));
EXPECT_FALSE(sees_dir(p, 0, 3));
EXPECT_TRUE(sees_dir(p, 1, 3));
}
TEST(Provisions, ACycleTerminates) {
// Resolution should not produce one, but the fixpoint must not depend on
// that: sets only grow and are bounded, so a cycle simply stops changing.
std::vector<Edge> edges{
{ 0, 1, { "a" }, false, true },
{ 1, 0, { "b" }, false, true },
};
auto p = prov::propagate(edges, 2);
EXPECT_TRUE(sees_tool(p, 0, 1, "a"));
EXPECT_TRUE(sees_tool(p, 1, 0, "b"));
}
// ── Bare-name binding ──────────────────────────────────────────────────────
TEST(Provisions, ALoneCandidateOwnsItsBareName) {
auto b = prov::bind_bare_names({ "compat.protobuf" });
ASSERT_TRUE(b.contains("protobuf"));
EXPECT_EQ(b["protobuf"].owner, "compat.protobuf");
EXPECT_FALSE(b["protobuf"].contested);
EXPECT_TRUE(prov::contest_note("protobuf", b["protobuf"]).empty());
}
TEST(Provisions, ANonDefaultNamespaceStillGetsItsBareName) {
// grpc-m's rule calls dep_bin("grpc-plugin", …); (grpc, grpc-plugin) is on
// no rung of the package-identity ladder, so without the
// unique-candidate rung the spelling already in the wild would break.
auto b = prov::bind_bare_names({ "grpc.grpc-plugin" });
EXPECT_EQ(b["grpc-plugin"].owner, "grpc.grpc-plugin");
}
TEST(Provisions, TheLadderPicksTheDefaultNamespaceFirst) {
auto b = prov::bind_bare_names({ "compat.zlib", "mcpplibs.zlib", "acme.zlib" });
EXPECT_EQ(b["zlib"].owner, "mcpplibs.zlib");
EXPECT_TRUE(b["zlib"].contested);
// Contested but bound: the user is told which one won and how to name the
// other, rather than getting whichever the loop appended last.
auto note = prov::contest_note("zlib", b["zlib"]);
EXPECT_NE(note.find("mcpplibs.zlib"), std::string::npos);
EXPECT_NE(note.find("acme.zlib"), std::string::npos);
}
TEST(Provisions, CompatIsTheSecondRung) {
auto b = prov::bind_bare_names({ "compat.zlib", "acme.zlib" });
EXPECT_EQ(b["zlib"].owner, "compat.zlib");
}
TEST(Provisions, TwoUnreachableNamespacesLeaveTheBareNameUnbound) {
// Neither is on the ladder and neither is unique. Binding either one would
// be an arbitrary choice that decides which binary runs.
auto b = prov::bind_bare_names({ "acme.protoc-ish", "other.protoc-ish" });
EXPECT_TRUE(b["protoc-ish"].owner.empty());
EXPECT_TRUE(b["protoc-ish"].contested);
auto note = prov::contest_note("protoc-ish", b["protoc-ish"]);
EXPECT_NE(note.find("NOT"), std::string::npos);
}
TEST(Provisions, AnUnnamespacedPackageIsTheThirdRung) {
auto b = prov::bind_bare_names({ "grpcgen", "acme.grpcgen" });
EXPECT_EQ(b["grpcgen"].owner, "grpcgen");
}
// ── Host module identity ───────────────────────────────────────────────────
//
// A rule's module name is authored API. The host-module path used to derive it
// from `package.name`, which made a divergent name build under GCC (implicit
// gcm.cache, keyed by the declared name) and fail under Clang and MSVC (both
// are handed an explicit `<name>=<bmi>` mapping). These pin the reading, the
// fallback, and the collision that used to be silent.
namespace {
std::filesystem::path write_iface(std::string_view stem, std::string_view body) {
auto dir = std::filesystem::temp_directory_path() / "mcpp-prov-tests";
std::filesystem::create_directories(dir);
auto p = dir / (std::string(stem) + ".cppm");
std::ofstream os(p, std::ios::trunc);
os << body;
return p;
}
} // namespace
TEST(HostModuleIdentity, TheDeclaredNameWinsOverThePackageName) {
auto p = write_iface("declared-wins",
"export module mcpp.rules.protobuf;\nimport std;\n");
EXPECT_EQ(prov::host_module_name(p, "protobufgen"), "mcpp.rules.protobuf");
}
TEST(HostModuleIdentity, ADottedNameSurvivesWhole) {
// `declared_module_roots` is named for module ROOTS as opposed to
// partitions; it must not truncate `a.b.c` to `a`, or every namespaced
// rule would register under one colliding name.
auto p = write_iface("dotted", "export module a.b.c;\n");
EXPECT_EQ(prov::host_module_name(p, "fallback"), "a.b.c");
}
TEST(HostModuleIdentity, AgreementIsTheCommonCaseAndIsUnchanged) {
// The zero-break claim: every rule package that works today declares a
// name equal to its package name, so the scan returns what the old code
// returned.
auto p = write_iface("grpcgen", "export module grpcgen;\nimport std;\n");
EXPECT_EQ(prov::host_module_name(p, "grpcgen"), "grpcgen");
}
TEST(HostModuleIdentity, AnUnreadableInterfaceFallsBackToThePackageName) {
// build_host_module reports a missing interface unit by path, and that
// diagnostic needs a name to report the package with.
EXPECT_EQ(prov::host_module_name("/nonexistent/nowhere.cppm", "rulepkg"),
"rulepkg");
}
TEST(HostModuleIdentity, AnInterfaceDeclaringNoModuleFallsBack) {
auto p = write_iface("nomodule", "int main() { return 0; }\n");
EXPECT_EQ(prov::host_module_name(p, "rulepkg"), "rulepkg");
}
TEST(HostModuleIdentity, TwoRulesProvidingOneModuleNameAreRefused) {
// Designated initialisers on purpose. Positional aggregate init put the
// interface path into a field added later, and the test then asserted that
// a path it had never passed was absent — a green that meant nothing until
// the field order changed under it.
std::vector<prov::HostModule> mods{
{.module = "tidy", .package = "acme.tidy",
.interface = "/a/src/tidy.cppm"},
{.module = "tidy", .package = "other.tidy",
.interface = "/b/src/rules.cppm"},
};
auto clash = prov::host_module_collision(mods);
ASSERT_TRUE(clash.has_value());
// Both identities and both paths are the CONTENT of this diagnostic: with
// one module name shared, they are the only way to tell the two apart.
EXPECT_NE(clash->find("acme.tidy"), std::string::npos);
EXPECT_NE(clash->find("other.tidy"), std::string::npos);
EXPECT_NE(clash->find("/a/src/tidy.cppm"), std::string::npos);
EXPECT_NE(clash->find("/b/src/rules.cppm"), std::string::npos);
}
TEST(HostModuleIdentity, DistinctModuleNamesFromOnePackageNameAreFine) {
// The mirror of the previous test, and the reason I1 and I3 ship together:
// decoupling the names is what makes two packages able to share a package
// name while differing in what they declare.
std::vector<prov::HostModule> mods{
{.module = "tidy", .package = "acme.rules",
.interface = "/a/src/rules.cppm"},
{.module = "lints", .package = "other.rules",
.interface = "/b/src/rules.cppm"},
};
EXPECT_FALSE(prov::host_module_collision(mods).has_value());
}
TEST(ReservedPrefix, AnOutsidePackageClaimingMcppIsWarnedAbout) {
auto w = prov::reserved_prefix_warning("mcpp.rules.protobuf", "acme",
"acme.protobufgen");
ASSERT_TRUE(w.has_value());
EXPECT_NE(w->find("mcpp.rules.protobuf"), std::string::npos);
EXPECT_NE(w->find("acme.protobufgen"), std::string::npos);
}
TEST(ReservedPrefix, TheOfficialNamespaceIsSilent) {
EXPECT_FALSE(prov::reserved_prefix_warning("mcpp.rules.protobuf", "mcpp",
"mcpp.protobuf").has_value());
}
TEST(ReservedPrefix, AnOrdinaryNameIsSilent) {
EXPECT_FALSE(prov::reserved_prefix_warning("grpcgen", "mcpplibs",
"mcpplibs.grpcgen").has_value());
// `mcppish` is not under the `mcpp.` prefix: the dot is what makes the
// claim, and a substring test would have flagged this.
EXPECT_FALSE(prov::reserved_prefix_warning("mcppish", "acme",
"acme.mcppish").has_value());
}
// A package that offers several rules through features (mcpp 2026.9.5.3+)
// contributes every module INTERFACE unit among its resolved sources. The
// detector decides what counts as one, and the cases below are the ones a
// wrong answer would turn into a compile of something that cannot be compiled
// alone -- an implementation unit, a partition -- or into a phantom module
// named by a comment.
TEST(InterfaceUnit, ThePrimaryInterfaceDeclarationIsRecognised) {
EXPECT_EQ(prov::declared_interface_name("export module mcpp.rules.cuda;\n"),
"mcpp.rules.cuda");
EXPECT_EQ(prov::declared_interface_name(
"module;\n#include <cstdio>\nexport module a.b;\nimport std;\n"),
"a.b");
EXPECT_EQ(prov::declared_interface_name("\texport module\tx ; // trailing\n"),
"x");
}
TEST(InterfaceUnit, WhatIsNotAPrimaryInterfaceIsNotNamed) {
EXPECT_EQ(prov::declared_interface_name("module a.b;\n"), ""); // implementation
EXPECT_EQ(prov::declared_interface_name("export module a.b:part;\n"), ""); // partition
EXPECT_EQ(prov::declared_interface_name("module;\n"), ""); // global fragment
EXPECT_EQ(prov::declared_interface_name("export module;\n"), "");
EXPECT_EQ(prov::declared_interface_name("// export module a.b;\n"), ""); // documentation
EXPECT_EQ(prov::declared_interface_name("exportmodule a.b;\n"), "");
EXPECT_EQ(prov::declared_interface_name("int x; // not a module at all\n"), "");
}
TEST(InterfaceUnit, TheFirstDeclarationWins) {
// A file names one module; anything after the first declaration is that
// module's body, and a quoted declaration inside it is text.
EXPECT_EQ(prov::declared_interface_name(
"export module first;\n/* export module second; */\n"),
"first");
}