-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_modgraph.cpp
More file actions
413 lines (356 loc) · 13.8 KB
/
Copy pathtest_modgraph.cpp
File metadata and controls
413 lines (356 loc) · 13.8 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <gtest/gtest.h>
import std;
import mcpp.modgraph.graph;
import mcpp.modgraph.scanner;
import mcpp.modgraph.validate;
import mcpp.manifest;
using namespace mcpp::modgraph;
namespace {
std::filesystem::path make_tempdir(std::string_view prefix) {
auto tmp = std::filesystem::temp_directory_path();
auto dir = tmp / std::format("{}-{}", prefix, std::random_device{}() );
std::filesystem::create_directories(dir);
return dir;
}
void write(const std::filesystem::path& p, std::string_view content) {
std::filesystem::create_directories(p.parent_path());
std::ofstream os(p);
os << content;
}
} // namespace
TEST(Scanner, ProvidesAndRequires) {
auto dir = make_tempdir("mcpp-scanner");
// NOTE: avoid raw string literal for module source — clang-scan-deps
// on Windows may false-positive on `import bar;` inside R"(...)".
write(dir / "src" / "foo.cppm",
"export module foo;\n"
"import std;\n"
"import bar;\n"
"export int answer();\n");
auto u = scan_file(dir / "src" / "foo.cppm", "pkg");
ASSERT_TRUE(u.has_value()) << u.error().format();
ASSERT_TRUE(u->provides.has_value());
EXPECT_EQ(u->provides->logicalName, "foo");
ASSERT_EQ(u->requires_.size(), 2u);
EXPECT_EQ(u->requires_[0].logicalName, "std");
EXPECT_EQ(u->requires_[1].logicalName, "bar");
std::filesystem::remove_all(dir);
}
// Regression: `import` lines that live INSIDE a multi-line raw-string literal
// (e.g. a `mcpp new --template gui` skeleton embedded as R"GUI( ... )GUI") must
// not be detected as real module imports. Before the fix this produced a
// spurious "module 'imgui.core' imported but not provided" warning.
TEST(Scanner, IgnoresImportsInsideRawStringLiteral) {
auto dir = make_tempdir("mcpp-scanner-raw");
write(dir / "src" / "gen.cppm",
"export module gen;\n"
"import std;\n"
"const char* tmpl = R\"GUI(\n"
"import imgui.core;\n"
"import imgui.app;\n"
"int main() { return 0; }\n"
")GUI\";\n"
"import bar;\n" // a real import AFTER the raw string
"export void f();\n");
auto u = scan_file(dir / "src" / "gen.cppm", "pkg");
ASSERT_TRUE(u.has_value()) << u.error().format();
ASSERT_TRUE(u->provides.has_value());
EXPECT_EQ(u->provides->logicalName, "gen");
// Only the two genuine top-level imports — NOT imgui.core / imgui.app.
ASSERT_EQ(u->requires_.size(), 2u);
EXPECT_EQ(u->requires_[0].logicalName, "std");
EXPECT_EQ(u->requires_[1].logicalName, "bar");
for (auto& r : u->requires_) {
EXPECT_NE(r.logicalName, "imgui.core");
EXPECT_NE(r.logicalName, "imgui.app");
}
std::filesystem::remove_all(dir);
}
// A single-line raw string with an embedded import-looking body stays code.
TEST(Scanner, IgnoresImportInsideSingleLineRawString) {
auto dir = make_tempdir("mcpp-scanner-raw1");
write(dir / "src" / "one.cppm",
"export module one;\n"
"const char* s = R\"(import nope;)\";\n"
"import real;\n");
auto u = scan_file(dir / "src" / "one.cppm", "pkg");
ASSERT_TRUE(u.has_value()) << u.error().format();
ASSERT_EQ(u->requires_.size(), 1u);
EXPECT_EQ(u->requires_[0].logicalName, "real");
std::filesystem::remove_all(dir);
}
TEST(Scanner, RecordsPackageLocalIncludeDirs) {
auto dir = make_tempdir("mcpp-scanner-includes");
write(dir / "src" / "foo.cpp",
"int answer() { return 42; }\n");
std::filesystem::create_directories(dir / "include");
std::filesystem::create_directories(dir / "private" / "nested");
mcpp::manifest::Manifest m;
m.package.name = "pkg";
m.modules.sources = {"src/*.cpp"};
m.buildConfig.includeDirs = {"include", "private/*"};
auto r = scan_packages({PackageRoot{dir, m}});
ASSERT_TRUE(r.errors.empty());
ASSERT_EQ(r.graph.units.size(), 1u);
auto const& dirs = r.graph.units[0].localIncludeDirs;
ASSERT_EQ(dirs.size(), 2u);
EXPECT_EQ(dirs[0], dir / "include");
EXPECT_EQ(dirs[1], dir / "private" / "nested");
std::filesystem::remove_all(dir);
}
TEST(Scanner, UsesResolvedPackagePrivateBuildIncludeDirs) {
auto dir = make_tempdir("mcpp-scanner-resolved-includes");
write(dir / "src" / "foo.cpp",
"int answer() { return 42; }\n");
std::filesystem::create_directories(dir / "legacy");
std::filesystem::create_directories(dir / "resolved");
mcpp::manifest::Manifest m;
m.package.name = "pkg";
m.modules.sources = {"src/*.cpp"};
m.buildConfig.includeDirs = {"legacy"};
PackageRoot p{dir, m};
p.usageResolved = true;
p.privateBuild.includeDirs = {dir / "resolved"};
auto r = scan_packages({p});
ASSERT_TRUE(r.errors.empty());
ASSERT_EQ(r.graph.units.size(), 1u);
auto const& dirs = r.graph.units[0].localIncludeDirs;
ASSERT_EQ(dirs.size(), 1u);
EXPECT_EQ(dirs[0], dir / "resolved");
std::filesystem::remove_all(dir);
}
TEST(Scanner, PartitionImportFromPrimaryInterface) {
// Primary module interface: `export module foo;` → logicalName = "foo".
// `import :tls;` resolves to "foo:tls".
auto dir = make_tempdir("mcpp-scanner");
write(dir / "src" / "foo.cppm",
"export module foo;\n"
"import :tls;\n");
auto u = scan_file(dir / "src" / "foo.cppm", "pkg");
ASSERT_TRUE(u.has_value()) << u.error().format();
ASSERT_EQ(u->requires_.size(), 1u);
EXPECT_EQ(u->requires_[0].logicalName, "foo:tls");
std::filesystem::remove_all(dir);
}
TEST(Scanner, PartitionImportFromAnotherPartition) {
// Partition interface: `export module foo:http;` → logicalName = "foo:http".
// `import :tls;` must resolve to "foo:tls" (the sibling partition),
// NOT "foo:http:tls" (which is what a naive prepend produces).
auto dir = make_tempdir("mcpp-scanner");
write(dir / "src" / "http.cppm",
"export module foo:http;\n"
"import :tls;\n"
"import :socket;\n");
auto u = scan_file(dir / "src" / "http.cppm", "pkg");
ASSERT_TRUE(u.has_value()) << u.error().format();
ASSERT_TRUE(u->provides.has_value());
EXPECT_EQ(u->provides->logicalName, "foo:http");
ASSERT_EQ(u->requires_.size(), 2u);
EXPECT_EQ(u->requires_[0].logicalName, "foo:tls");
EXPECT_EQ(u->requires_[1].logicalName, "foo:socket");
std::filesystem::remove_all(dir);
}
TEST(Scanner, PartitionImportWithDottedModuleName) {
// Dotted module names (xpkg-style, e.g. `mcpplibs.tinyhttps:http`)
// — only the colon-prefixed partition suffix is what we strip.
auto dir = make_tempdir("mcpp-scanner");
write(dir / "src" / "http.cppm",
"export module mcpplibs.tinyhttps:http;\n"
"import :tls;\n");
auto u = scan_file(dir / "src" / "http.cppm", "pkg");
ASSERT_TRUE(u.has_value()) << u.error().format();
ASSERT_EQ(u->requires_.size(), 1u);
EXPECT_EQ(u->requires_[0].logicalName, "mcpplibs.tinyhttps:tls");
std::filesystem::remove_all(dir);
}
TEST(Scanner, RejectsConditionalImport) {
auto dir = make_tempdir("mcpp-scanner");
write(dir / "main.cpp",
"import std;\n"
"#ifdef WANT_X\n"
"import x;\n"
"#endif\n"
"int main(){}");
auto r = scan_file(dir / "main.cpp", "pkg");
EXPECT_FALSE(r.has_value());
EXPECT_NE(r.error().message.find("conditional"), std::string::npos);
std::filesystem::remove_all(dir);
}
TEST(Scanner, RejectsHeaderUnit) {
auto dir = make_tempdir("mcpp-scanner");
write(dir / "main.cpp",
"import std;\n"
"import \"x.h\";\n"
"int main(){}");
auto r = scan_file(dir / "main.cpp", "pkg");
EXPECT_FALSE(r.has_value());
EXPECT_NE(r.error().message.find("header units"), std::string::npos);
std::filesystem::remove_all(dir);
}
TEST(Scanner, ObjectiveCSourceIsCLike) {
auto dir = make_tempdir("mcpp-scanner-objc");
write(dir / "src" / "window.m",
"import Cocoa;\n"
"int answer(void) { return 42; }\n");
auto u = scan_file(dir / "src" / "window.m", "pkg");
ASSERT_TRUE(u.has_value()) << u.error().format();
EXPECT_FALSE(u->provides.has_value());
EXPECT_TRUE(u->requires_.empty());
std::filesystem::remove_all(dir);
}
TEST(Validate, ModuleNameNotRequiredToMatchPackageName) {
// 0.0.10+: module name does NOT need to be prefixed by package name.
// The library author decides the module naming convention.
Graph g;
SourceUnit u;
u.path = "/x/foo.cppm";
u.packageName = "myorg.foo";
u.provides = ModuleId{"completely.different.name"};
g.units.push_back(u);
g.producerOf["completely.different.name"] = 0;
mcpp::manifest::Manifest m;
m.package.name = "myorg.foo";
auto rep = validate(g, m);
EXPECT_TRUE(rep.ok()) << "module name mismatch should not be an error";
}
TEST(Validate, ForbiddenTopName) {
Graph g;
SourceUnit u;
u.path = "/x/foo.cppm";
u.packageName = "myorg.foo";
u.provides = ModuleId{"util"};
g.units.push_back(u);
g.producerOf["util"] = 0;
mcpp::manifest::Manifest m;
m.package.name = "myorg.foo";
auto rep = validate(g, m);
EXPECT_FALSE(rep.ok());
}
TEST(Validate, LibRootHappyPath) {
// Project: lib target "tinyhttps", convention puts the lib root at
// src/tinyhttps.cppm exporting `mcpplibs.tinyhttps`. Two partition
// siblings sit alongside.
Graph g;
SourceUnit root;
root.path = "src/tinyhttps.cppm";
root.packageName = "mcpplibs.tinyhttps";
root.provides = ModuleId{"mcpplibs.tinyhttps"};
g.units.push_back(root);
g.producerOf["mcpplibs.tinyhttps"] = 0;
SourceUnit p1;
p1.path = "src/tls.cppm";
p1.packageName = "mcpplibs.tinyhttps";
p1.provides = ModuleId{"mcpplibs.tinyhttps:tls"};
g.units.push_back(p1);
g.producerOf["mcpplibs.tinyhttps:tls"] = 1;
mcpp::manifest::Manifest m;
m.package.name = "mcpplibs.tinyhttps";
mcpp::manifest::Target t;
t.name = "tinyhttps";
t.kind = mcpp::manifest::Target::Library;
m.targets.push_back(t);
auto rep = validate(g, m); // empty projectRoot → on-disk check skipped
EXPECT_TRUE(rep.ok()) << "errors:" << [&]{
std::string s; for (auto& e : rep.errors) s += "\n " + e.message; return s;
}();
}
TEST(Validate, LibRootExportsPartitionIsError) {
// Lib root file at the conventional path exports `:foo` (a partition
// suffix) — must be rejected: lib root must be the primary module.
Graph g;
SourceUnit u;
u.path = "src/tinyhttps.cppm";
u.packageName = "mcpplibs.tinyhttps";
u.provides = ModuleId{"mcpplibs.tinyhttps:something"};
g.units.push_back(u);
g.producerOf["mcpplibs.tinyhttps:something"] = 0;
mcpp::manifest::Manifest m;
m.package.name = "mcpplibs.tinyhttps";
mcpp::manifest::Target t;
t.name = "tinyhttps";
t.kind = mcpp::manifest::Target::Library;
m.targets.push_back(t);
auto rep = validate(g, m);
EXPECT_FALSE(rep.ok());
bool found = false;
for (auto& e : rep.errors) {
if (e.message.find("partition") != std::string::npos
&& e.message.find("primary module") != std::string::npos) { found = true; break; }
}
EXPECT_TRUE(found) << "expected lib-root partition error";
}
TEST(Validate, LibRootDifferentModuleNameIsAllowed) {
// 0.0.10+: lib root module name does NOT need to match [package].name.
// The library author decides the module name; the build tool auto-detects.
Graph g;
SourceUnit u;
u.path = "src/tinyhttps.cppm";
u.packageName = "mcpplibs.tinyhttps";
u.provides = ModuleId{"some.other.module"};
g.units.push_back(u);
g.producerOf["some.other.module"] = 0;
mcpp::manifest::Manifest m;
m.package.name = "mcpplibs.tinyhttps";
mcpp::manifest::Target t;
t.name = "tinyhttps";
t.kind = mcpp::manifest::Target::Library;
m.targets.push_back(t);
auto rep = validate(g, m);
EXPECT_TRUE(rep.ok()) << "module name mismatch should not be an error";
}
TEST(Validate, LibRootNotEnforcedForBinaryProject) {
// Pure-binary project: no lib target → no lib-root checks. Even if a
// file at src/<tail>.cppm exists exporting an unrelated module, no
// error should fire.
Graph g;
mcpp::manifest::Manifest m;
m.package.name = "myapp";
mcpp::manifest::Target t;
t.name = "myapp";
t.kind = mcpp::manifest::Target::Binary;
t.main = "src/main.cpp";
m.targets.push_back(t);
auto rep = validate(g, m);
EXPECT_TRUE(rep.ok());
}
TEST(Validate, LibRootMissingFileWithExplicitPathIsError) {
Graph g;
mcpp::manifest::Manifest m;
m.package.name = "myorg.foo";
m.lib.path = "src/does-not-exist.cppm";
mcpp::manifest::Target t;
t.name = "foo";
t.kind = mcpp::manifest::Target::Library;
m.targets.push_back(t);
// Pass a non-empty projectRoot so the on-disk check is enabled.
auto rep = validate(g, m, std::filesystem::current_path());
EXPECT_FALSE(rep.ok());
bool found = false;
for (auto& e : rep.errors) {
if (e.message.find("does not exist") != std::string::npos) { found = true; break; }
}
EXPECT_TRUE(found) << "expected explicit-path-missing error";
}
TEST(TopoSort, DetectsCycle) {
Graph g;
g.units.resize(2);
g.units[0].provides = ModuleId{"a"};
g.units[1].provides = ModuleId{"b"};
g.units[0].requires_.push_back({"b"});
g.units[1].requires_.push_back({"a"});
g.producerOf["a"] = 0;
g.producerOf["b"] = 1;
g.edges.push_back({0, 1}); // a->b
g.edges.push_back({1, 0}); // b->a
auto r = topo_sort(g);
EXPECT_FALSE(r.has_value());
}
TEST(IsPublicPackage, DotMarksPublic) {
EXPECT_TRUE(is_public_package_name("myorg.foo"));
EXPECT_FALSE(is_public_package_name("foo"));
}
TEST(IsForbiddenTopModule, KnownNames) {
EXPECT_TRUE(is_forbidden_top_module("core"));
EXPECT_TRUE(is_forbidden_top_module("util.x"));
EXPECT_FALSE(is_forbidden_top_module("myorg.foo"));
}