Repository navigation
Expand file tree
/
Copy pathtest_test_targets.cpp
More file actions
382 lines (331 loc) · 15.9 KB
/
Copy pathtest_test_targets.cpp
File metadata and controls
382 lines (331 loc) · 15.9 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
import std;
import mcpplibs.mtest;
import mcpp.build.plan;
import mcpp.build.test_targets;
import mcpp.manifest;
import mcpp.modgraph.graph;
import mcpp.modgraph.scanner;
import mcpp.project;
import mcpp.toolchain.model;
using namespace mcpplibs::mtest;
namespace {
struct Tmp {
std::filesystem::path path;
Tmp() {
static std::atomic_uint64_t sequence{0};
auto timestamp = std::chrono::steady_clock::now().time_since_epoch().count();
path = std::filesystem::temp_directory_path()
/ std::format("mcpp_test_targets_{}_{}", timestamp,
sequence.fetch_add(1, std::memory_order_relaxed));
std::filesystem::create_directories(path);
}
~Tmp() {
std::error_code ec;
std::filesystem::remove_all(path, ec);
}
};
void write_file(const std::filesystem::path& path, std::string_view body) {
std::filesystem::create_directories(path.parent_path());
std::ofstream(path) << body;
}
void write_manifest(const std::filesystem::path& root, std::string_view body) {
write_file(root / "mcpp.toml", body);
}
UTest nested_names_use_tests_relative_path{"TestTargets/NestedNamesUseTestsRelativePath", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
)");
write_file(tmp.path / "tests/tagged/nested/smoke.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(fatal(eq(result->targets.size(), 1u)));
expect(eq(result->targets[0].name, "tagged/nested/smoke"));
expect(eq(std::filesystem::path(result->targets[0].main), std::filesystem::path("tests") / "tagged" / "nested" / "smoke.cpp"));
}};
UTest a_test_directory_cannot_replace_the_packages_executable{"TestTargets/ATestDirectoryCannotReplaceThePackagesExecutable", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "collide"
version = "0.1.0"
[targets.collide]
kind = "bin"
main = "src/main.cpp"
[test]
programs = "directory"
)");
write_file(tmp.path / "src/main.cpp", "int main() { return 0; }\n");
write_file(tmp.path / "tests/collide/one_test.cpp", "int main() { return 0; }\n");
auto project = mcpp::project::load_effective_manifest(tmp.path);
if (auto&& failed = expect(fatal(project.has_value())); !failed) failed << project.error();
auto discovered = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(discovered.has_value())); !failed) failed << discovered.error();
auto& manifest = project->manifest;
manifest.targets.insert(manifest.targets.end(), discovered->targets.begin(), discovered->targets.end());
mcpp::modgraph::PackageRoot root;
root.root = tmp.path;
root.manifest = manifest;
mcpp::toolchain::Toolchain toolchain;
toolchain.compiler = mcpp::toolchain::CompilerId::Clang;
for (const auto& [triple, binary] : {
std::pair{"aarch64-apple-darwin", "bin/collide"},
std::pair{"x86_64-w64-mingw32", "bin/collide.exe"}}) {
toolchain.targetTriple = triple;
const auto plan = mcpp::build::make_plan(manifest, toolchain, {}, {}, {}, {root},
tmp.path, tmp.path / "target", {}, {});
if (auto&& failed = expect(fatal(!plan.has_value())); !failed) failed << triple;
const auto& error = plan.error();
expect(error.contains("[test-output-collision]")) << error;
expect(error.contains("test program 'collide'") && error.contains("executable target 'collide'")) << error;
expect(error.contains(binary)) << error;
}
}};
UTest member_test_paths_keep_their_directories{"TestTargets/MemberTestPathsKeepTheirDirectories", [] {
Tmp tmp;
write_manifest(tmp.path, R"([workspace]
members = ["member"]
)");
const auto memberDir = tmp.path / "member";
write_manifest(memberDir, R"([package]
name = "member"
version = "0.1.0"
[targets."nested/tool"]
kind = "bin"
main = "src/main.cpp"
[test]
programs = "directory"
)");
write_file(memberDir / "src/main.cpp", "int main() { return 0; }\n");
write_file(memberDir / "tests/tool/main.cpp", "int main() { return 0; }\n");
write_file(memberDir / "tests/nested/tool/main.cpp", "int main() { return 0; }\n");
auto workspace = mcpp::project::load_effective_manifest(tmp.path);
if (auto&& failed = expect(fatal(workspace.has_value())); !failed) failed << workspace.error();
auto project = mcpp::project::load_effective_manifest(memberDir);
if (auto&& failed = expect(fatal(project.has_value())); !failed) failed << project.error();
auto discovered = mcpp::build::discover_test_targets(tmp.path, "member");
if (auto&& failed = expect(fatal(discovered.has_value())); !failed) failed << discovered.error();
mcpp::modgraph::PackageRoot root;
root.root = tmp.path;
root.manifest = mcpp::project::virtual_workspace_root(workspace->manifest, project->manifest, tmp.path);
mcpp::modgraph::PackageRoot member;
member.root = memberDir;
member.selectedMember = true;
member.memberProducts = "member";
mcpp::toolchain::Toolchain toolchain;
toolchain.compiler = mcpp::toolchain::CompilerId::Clang;
toolchain.targetTriple = "aarch64-apple-darwin";
for (const auto& [name, collides] : {std::pair{"nested/tool", false}, std::pair{"tool", true}}) {
const auto test = std::ranges::find(discovered->targets, name, &mcpp::manifest::Target::name);
if (auto&& failed = expect(fatal(test != discovered->targets.end())); !failed) failed << name;
member.manifest = project->manifest;
member.manifest.targets.push_back(*test);
const auto plan = mcpp::build::make_plan(root.manifest, toolchain, {}, {}, {}, {root, member},
tmp.path, tmp.path / "target", {}, {});
if (!collides) {
if (auto&& failed = expect(fatal(plan.has_value())); !failed) failed << plan.error();
expect(fatal(eq(plan->linkUnits.size(), 1u)));
expect(eq(plan->linkUnits.front().output.generic_string(), "bin/member/nested/tool"));
} else {
if (auto&& failed = expect(fatal(!plan.has_value())); !failed) failed << name;
expect(plan.error().contains("[test-output-collision]")) << plan.error();
expect(plan.error().contains("executable target 'nested/tool'")) << plan.error();
expect(plan.error().contains("'bin/member/tool'")) << plan.error();
}
}
}};
UTest glob_flags_become_per_target_flags{"TestTargets/GlobFlagsBecomePerTargetFlags", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
[build]
flags = [{ glob = "tests/**/*.cpp", defines = ["TEST_FEATURE"], cflags = ["-Wall"], cxxflags = ["-Wextra"] }]
)");
write_file(tmp.path / "tests/main.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(fatal(eq(result->targets.size(), 1u)));
expect(eq(result->targets[0].defines, std::vector<std::string>{"TEST_FEATURE"}));
expect(eq(result->targets[0].cflags, std::vector<std::string>{"-Wall"}));
expect(eq(result->targets[0].cxxflags, std::vector<std::string>{"-Wextra"}));
}};
UTest package_filter_scopes_workspace_member{"TestTargets/PackageFilterScopesWorkspaceMember", [] {
Tmp tmp;
write_manifest(tmp.path, R"([workspace]
members = ["a", "b"]
)");
write_manifest(tmp.path / "a", R"([package]
name = "a"
version = "0.1.0"
)");
write_manifest(tmp.path / "b", R"([package]
name = "b"
version = "0.1.0"
)");
write_file(tmp.path / "a/tests/main.cpp", "int main() {}\n");
write_file(tmp.path / "b/tests/main.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, "a");
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(fatal(eq(result->packageRoot, tmp.path / "a")));
expect(fatal(eq(result->targets.size(), 1u)));
expect(eq(std::filesystem::path(result->targets[0].main), std::filesystem::path("tests") / "main.cpp"));
}};
UTest directory_symlink_keeps_lexical_package_boundary{"TestTargets/DirectorySymlinkKeepsLexicalPackageBoundary", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
)");
Tmp outside;
write_file(outside.path / "external.cpp", "int main() {}\n");
std::filesystem::create_directories(tmp.path / "tests");
std::error_code ec;
std::filesystem::create_directory_symlink(
outside.path, tmp.path / "tests/vendor", ec);
if (ec) skip([&] { std::ostringstream text; text << "directory symlink unavailable: " << ec.message(); return text.str(); }());
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(fatal(eq(result->targets.size(), 1u)));
expect(eq(result->targets[0].name, "vendor/external"));
expect(eq(std::filesystem::path(result->targets[0].main), std::filesystem::path("tests") / "vendor" / "external.cpp"));
}};
// `[test] discover` (#634 A5): names are relative to the fixed directory of
// the glob that found the file, `main` stays relative to the package root.
UTest discover_names_are_relative_to_the_globs_directory{"TestTargets/DiscoverNamesAreRelativeToTheGlobsDirectory", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
[test]
discover = ["checks/**/*.cpp"]
)");
write_file(tmp.path / "checks/deep/a.cpp", "int main() {}\n");
write_file(tmp.path / "tests/ignored.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(bool(result->discoverDeclared));
expect(fatal(eq(result->targets.size(), 1u)));
expect(eq(result->targets[0].name, "deep/a"));
expect(eq(std::filesystem::path(result->targets[0].main), std::filesystem::path("checks") / "deep" / "a.cpp"));
}};
// An exclusion removes a file whichever positive glob found it, and a glob
// with no fixed directory names files relative to the package root.
UTest discover_exclusions_apply_across_globs{"TestTargets/DiscoverExclusionsApplyAcrossGlobs", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
[test]
discover = ["tests/**/*.cpp", "*_check.cpp", "!tests/fixtures/**"]
)");
write_file(tmp.path / "tests/kept.cpp", "int main() {}\n");
write_file(tmp.path / "tests/fixtures/dropped.cpp", "int main() {}\n");
write_file(tmp.path / "root_check.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
std::set<std::string> names;
for (auto const& t : result->targets) names.insert(t.name);
expect(eq(names, (std::set<std::string>{"kept", "root_check"})));
}};
UTest an_empty_discover_finds_nothing{"TestTargets/AnEmptyDiscoverFindsNothing", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
[test]
discover = []
)");
write_file(tmp.path / "tests/main.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(bool(result->discoverDeclared));
expect(result->discover.empty());
expect(result->targets.empty());
}};
UTest two_files_with_one_name_are_refused_naming_both{"TestTargets/TwoFilesWithOneNameAreRefusedNamingBoth", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
[test]
discover = ["tests/*.cpp", "checks/*.cpp"]
)");
write_file(tmp.path / "tests/same.cpp", "int main() {}\n");
write_file(tmp.path / "checks/same.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
expect(fatal(!result.has_value()));
if (auto&& failed = expect(ne(result.error().find("duplicate test name 'same'"), std::string::npos)); !failed) failed << result.error();
if (auto&& failed = expect(ne(result.error().find("tests/same.cpp"), std::string::npos)); !failed) failed << result.error();
if (auto&& failed = expect(ne(result.error().find("checks/same.cpp"), std::string::npos)); !failed) failed << result.error();
}};
// Without the key the set, and every name, is the one earlier releases gave.
UTest without_discover_the_default_is_tests_glob{"TestTargets/WithoutDiscoverTheDefaultIsTestsGlob", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
)");
write_file(tmp.path / "tests/unit/a.cpp", "int main() {}\n");
write_file(tmp.path / "checks/b.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(!result->discoverDeclared);
expect(eq(result->discover, std::vector<std::string>{"tests/**/*.cpp"}));
expect(fatal(eq(result->targets.size(), 1u)));
expect(eq(result->targets[0].name, "unit/a"));
}};
UTest a_package_below_tests_is_not_its_tests{"TestTargets/APackageBelowTestsIsNotItsTests", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
[test]
discover = ["tests/**/*.cpp"]
)");
write_file(tmp.path / "tests/unit/a.cpp", "int main() {}\n");
write_manifest(tmp.path / "tests/cts", "[package]\nname = \"cts\"\nversion = \"0.1.0\"\n");
write_file(tmp.path / "tests/cts/src/main.cpp", "int main() {}\n");
write_manifest(tmp.path / "tests/e2e/data/app", "[package]\nname = \"app\"\nversion = \"0.1.0\"\n");
write_file(tmp.path / "tests/e2e/data/app/src/main.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(fatal(eq(result->targets.size(), 1u)));
expect(eq(result->targets[0].name, "unit/a"));
}};
UTest broken_manifest_still_returns_inventory{"TestTargets/BrokenManifestStillReturnsInventory", [] {
Tmp tmp;
write_file(tmp.path / "mcpp.toml", "this is not valid TOML\n");
write_file(tmp.path / "tests/main.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(result.has_value())); !failed) failed << result.error();
expect(fatal(eq(result->packageRoot, tmp.path)));
expect(fatal(eq(result->targets.size(), 1u)));
expect(eq(result->targets[0].name, "main"));
}};
} // namespace
// A test's name is the path of its program: a directory program `unit` and a
// program `unit/own` below it would be a file and a directory of one path.
UTest a_name_that_is_another_s_directory_is_refused{"TestTargets/ANameThatIsAnothersDirectoryIsRefused", [] {
Tmp tmp;
write_manifest(tmp.path, R"([package]
name = "demo"
version = "0.1.0"
[test]
programs = "directory"
)");
write_file(tmp.path / "tests/unit/a.cpp", "int main() {}\n");
write_file(tmp.path / "tests/unit/b.cpp", "int b() { return 0; }\n");
write_file(tmp.path / "tests/unit/own/c.cpp", "int main() {}\n");
write_file(tmp.path / "tests/unit-x/d.cpp", "int main() {}\n");
auto result = mcpp::build::discover_test_targets(tmp.path, {});
expect(fatal(!result.has_value()));
if (auto&& failed = expect(result.error().find("test 'unit'") != std::string::npos
&& result.error().find("test 'unit/own'") != std::string::npos);
!failed)
failed << result.error();
std::filesystem::rename(tmp.path / "tests/unit/own", tmp.path / "tests/own");
auto moved = mcpp::build::discover_test_targets(tmp.path, {});
if (auto&& failed = expect(fatal(moved.has_value())); !failed) failed << moved.error();
expect(eq(moved->targets.size(), 3u));
}};