Repository navigation
Expand file tree
/
Copy pathengine.cpp
More file actions
1154 lines (1143 loc) · 68.5 KB
/
Copy pathengine.cpp
File metadata and controls
1154 lines (1143 loc) · 68.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The engine, as its users run it: the `mtest` program over programs of each level, written here as
// scripts with the Bash adapter (adapters/script/mtest.sh), in a project of their own.
import std;
import mcpplibs.mtest;
import mcpplibs.mtest.e2e;
import mcpplibs.os;
import nlohmann.json;
using namespace mcpplibs::mtest;
using json = nlohmann::json;
namespace {
// The repository's root, from this file's place in it.
std::filesystem::path repository() {
return std::filesystem::path(std::source_location::current().file_name())
.parent_path()
.parent_path()
.parent_path();
}
std::string adapter() {
return (repository() / "adapters" / "script" / "mtest.sh").generic_string();
}
// A project directory holding `files`, scripts made executable. The tests name a script `bash
// t.sh`, as Windows starts no script by its first line.
std::filesystem::path
project(std::initializer_list<std::pair<std::string_view, std::string_view>> files) {
auto root = e2e::tree(files);
for (const auto& [name, _] : files)
if (name.ends_with(".sh"))
(void)mcpplibs::os::fs::set_executable(root / name, true);
return root;
}
e2e::outcome mtest(const std::filesystem::path& cwd, std::vector<std::string> args,
std::string input = {}) {
args.insert(args.begin(), e2e::program("mtest"));
// The engine under test is a runner of its own. Inside the runner that runs these tests (mcpp
// 2026.11.21.1 states its budget to every program), it would take that budget, record none of
// its own, and the cases that state a run's budget would answer for the outer runner.
return e2e::run({.argv = args,
.cwd = cwd,
.env = {{"MTEST_BUDGET", std::nullopt}, {"MTEST_BUDGET_HELD", std::nullopt}},
.input = std::move(input),
.timeout = std::chrono::seconds{120}});
}
json json_of(const e2e::outcome& r) {
auto j = json::parse(r.out, nullptr, false);
if (j.is_discarded())
fail("not JSON: " + r.out.substr(0, 400) + r.err.substr(0, 400));
return j;
}
// Whether a timeout's record holds every process of a program's unit here: Linux lists its
// processes in /proc, where the engine, as this program, reads it.
bool sees_the_tree() {
return mcpplibs::os::host() == mcpplibs::os::platform::linux &&
std::filesystem::exists("/proc/self/stat");
}
// A protocol script: the cases given as `id:status`, each with a function that does what its status
// says.
std::string script(std::initializer_list<std::string_view> cases, std::string_view extra = {}) {
std::string s = "#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() + "'\n";
s += "ok() { :; }\nbad() { mtest_eq 1 2; }\n";
s += std::string(extra);
for (auto c : cases) {
const auto colon = c.find(':');
const auto id = c.substr(0, colon);
const auto what = c.substr(colon + 1);
s += std::format("mtest_case '{}' {}\n", id,
what == "fail" ? "bad"
: what == "pass" ? "ok"
: what);
}
return s + "mtest_report\n";
}
} // namespace
int main() {
"an L0 program is one case, judged by its exit status"_test[kind::e2e] = [] {
auto root = project({{"good.sh", "exit 0\n"}, {"bad.sh", "exit 4\n"}});
auto r = mtest(root, {"run", "--json", "bash good.sh", "bash bad.sh"});
expect(eq(r.status, 1));
auto j = json_of(r);
expect(eq(j["counts"].value("pass", 0), 1));
expect(eq(j["counts"].value("fail", 0), 1));
expect(eq(j["failing"][0]["why"].get<std::string>(), std::string("exit 4")));
};
"a protocol program's cases, their failures and a selection"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"fs/a:pass", "fs/b:fail", "net/c:pass"})}});
auto r = mtest(root, {"run", "--json", "bash t.sh"});
auto j = json_of(r);
expect(eq(j["cases"].get<int>(), 3));
expect(eq(j["failing"].size(), std::size_t(1)));
expect(eq(j["failing"][0]["case"].get<std::string>(), std::string("fs/b")));
expect(contains(j["failing"][0]["where"].get<std::string>(), "t.sh:"));
expect(contains(j["failing"][0]["again"].get<std::string>(), "--case fs/b"));
auto only = json_of(mtest(root, {"run", "--json", "--case", "net/*", "bash t.sh"}));
expect(eq(only["cases"].get<int>(), 1));
expect(eq(only["status"].get<std::string>(), std::string("pass")));
};
"a program only a runner starts is listed and run through it (--run-under)"_test[kind::e2e] =
[] {
// Not executable by itself, as a program of another system is not on this one: both the
// listing (P3) and the run start it under the runner.
auto root = project({{"t.cross", script({"a:pass", "b:pass"})}});
auto j = json_of(mtest(root, {"run", "--json", "--run-under", "bash", "./t.cross"}));
expect(eq(j["cases"].get<int>(), 2));
expect(eq(j["status"].get<std::string>(), std::string("pass")));
};
"a case a crash ended fails with the program's status (P5.4)"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"dies:crash"}, "crash() { exit 9; }\n")}});
auto j = json_of(mtest(root, {"run", "--json", "bash t.sh"}));
expect(eq(j["status"].get<std::string>(), std::string("fail")));
expect(contains(j["failing"][0]["why"].get<std::string>(), "exit 9"));
};
"a program past its timeout is ended and reported so"_test[kind::e2e][tag{"slow"}] = [] {
auto root = project({{"hang.sh", "sleep 30\n"}});
auto j = json_of(mtest(root, {"run", "--json", "--timeout", "1s", "bash hang.sh"}));
expect(eq(j["counts"].value("timeout", 0), 1));
};
"a timeout records the program's processes, then asks it to end"_test[kind::e2e][tag{"slow"}] =
[] {
// bash ends on the request; the `sleep` it started does not receive it, and is ended
// with the program's unit. A grace that long is not waited for by a program that ends.
auto root = project({{"hang.sh", "sleep 30\n"}});
auto r = mtest(root, {"run", "--timeout", "1s", "--grace", "60s", "bash hang.sh"});
expect(contains(r.out, "asked to end, it ended within its grace of 60s")) << r.out;
auto j = json_of(mtest(root, {"explain", "--json"}));
auto t = j["cases"][0]["timeout"];
expect(fatal(t.is_object())) << j.dump();
expect(eq(t.value("ended", ""), std::string("requested"))) << t.dump();
expect(eq(t.value("grace_ms", 0.0), 60000.0)) << t.dump();
if (!sees_the_tree()) {
expect(eq(t.value("seen", ""), std::string("program"))) << t.dump();
expect(le(t["processes"].size(), std::size_t(1))) << t.dump();
return;
}
expect(eq(t.value("seen", ""), std::string("tree"))) << t.dump();
expect(contains(r.out, "processes at the timeout:")) << r.out;
const auto& seen = t["processes"];
const auto sleeping = std::find_if(seen.begin(), seen.end(), [](const json& p) {
return p.value("name", "") == "sleep";
});
expect(fatal(sleeping != seen.end())) << t.dump();
expect(eq(sleeping->value("state", ""), std::string("sleeping"))) << t.dump();
expect(eq(sleeping->value("request", ""), std::string("ends"))) << t.dump();
expect(contains(t["remaining"].dump(), "\"sleep\"")) << t.dump();
};
"a program that ignores the request to end is ended after its grace"_test[kind::e2e][tag{
"slow"}] = [] {
auto root =
project({{"stubborn.sh", "trap '' TERM\nsleep 30\n"},
{"mtest.toml", "[programs.stubborn]\ncommand = [\"bash\", "
"\"stubborn.sh\"]\ntimeout = \"1s\"\ngrace = \"1s\"\n"}});
const auto began = std::chrono::steady_clock::now();
auto r = mtest(root, {"run"});
const std::chrono::duration<double> took = std::chrono::steady_clock::now() - began;
auto j = json_of(mtest(root, {"explain", "--json"}));
auto t = j["cases"][0]["timeout"];
expect(fatal(t.is_object())) << j.dump();
expect(eq(j["cases"][0].value("status", ""), std::string("timeout")));
// openkal's request to end a program is TerminateProcess on Windows, which no
// program declines.
if (mcpplibs::os::host() == mcpplibs::os::platform::windows) {
expect(eq(t.value("ended", ""), std::string("requested"))) << t.dump();
return;
}
expect(eq(t.value("ended", ""), std::string("terminated"))) << t.dump();
expect(ge(took.count(), 2.0)) << "the timeout, then the grace";
expect(contains(r.out, "still running after its grace of 1s, and was ended")) << r.out;
if (!sees_the_tree())
return;
// bash, and the `sleep` that inherits its disposition, ignore the request.
const auto& left = t["remaining"];
const auto ignoring = std::count_if(left.begin(), left.end(), [](const json& p) {
return p.value("request", "") == "ignored";
});
expect(ge(ignoring, 2)) << t.dump();
};
"a protocol program's case says how its timeout ended it; timeout_record = false records "
"nothing"_test[kind::e2e][tag{"slow"}] = [] {
auto root =
project({{"t.sh", script({"quick:pass", "stuck:hang"}, "hang() { sleep 30; }\n")},
{"mtest.toml", "[programs.t]\ncommand = [\"bash\", \"t.sh\"]\ntimeout = "
"\"1s\"\ntimeout_record = false\n"}});
auto j = json_of(mtest(root, {"run", "--json"}));
expect(eq(j["counts"].value("timeout", 0), 1)) << j.dump();
expect(eq(j["counts"].value("pass", 0), 1)) << j.dump();
expect(contains(j["failing"][0].value("why", ""), "asked to end")) << j.dump();
auto e = json_of(mtest(root, {"explain", "--json"}));
auto t = e["cases"][0]["timeout"];
expect(fatal(t.is_object())) << e.dump();
expect(!t.contains("seen") && !t.contains("processes")) << t.dump();
};
"a program that does not start is the machine's failure, not the test's"_test[kind::e2e] = [] {
auto root = project({{"x.txt", ""}});
auto r = mtest(root, {"run", "--json", "./no-such-program"});
expect(eq(r.status, 3));
expect(eq(json_of(r)["status"].get<std::string>(), std::string("infra")));
};
"explain says where, why and how to repeat it"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"x:fail"})}});
(void)mtest(root, {"run", "bash t.sh"});
auto j = json_of(mtest(root, {"explain", "--json"}));
const auto& c = j["cases"][0];
expect(eq(c["case"].get<std::string>(), std::string("x")));
expect(eq(c["why"][0]["expected"].get<std::string>(), std::string("2")));
expect(eq(c["kind_of_failure"].get<std::string>(), std::string("first seen")));
expect(contains(j["conclusion"].get<std::string>(), "again:"));
};
"rerun runs the cases that failed last"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"a:pass", "b:fail"})},
{"mtest.toml", "[programs.t]\ncommand = [\"bash\", \"t.sh\"]\n"}});
(void)mtest(root, {"run"});
auto j = json_of(mtest(root, {"rerun", "--json"}));
expect(eq(j["cases"].get<int>(), 1));
};
"a declared program, a set and a plan that says why"_test[kind::e2e] = [] {
auto root = project(
{{"t.sh", script({"a:pass", "b:pass"})},
{"mtest.toml", "[programs.t]\ncommand = [\"bash\", \"t.sh\"]\ntags = [\"smoke\"]\n"
"sources = [\"src/*\"]\n"}});
auto plan = json_of(mtest(root, {"plan", "--json", "--set", "smoke"}));
expect(eq(plan["selected"].get<int>(), 2));
auto changed = json_of(mtest(root, {"plan", "--json", "--changed", "docs/x.md"}));
expect(eq(changed["selected"].get<int>(), 0))
<< "a change of nothing the program reads affects none of it";
auto src = json_of(mtest(root, {"plan", "--json", "--changed", "src/a.cpp"}));
expect(eq(src["selected"].get<int>(), 2));
expect(contains(src["units"][0]["cases"][0]["why"].get<std::string>(), "src/a.cpp"));
};
"the pr set runs every case when the change cannot be read"_test[kind::e2e] = [] {
// A repository of its own, with no `origin/main`: `git diff` cannot say what changed.
auto root =
project({{"t.sh", script({"a:pass", "b:pass"})},
{"mtest.toml",
"[programs.t]\ncommand = [\"bash\", \"t.sh\"]\nsources = [\"src/*\"]\n"}});
if (!mcpplibs::os::env::find_executable("git"))
return;
(void)e2e::run({.argv = {"git", "init", "-q", "."}, .cwd = root});
auto plan = json_of(mtest(root, {"plan", "--json", "--set", "pr"}));
expect(eq(plan["selected"].get<int>(), 2)) << plan.dump();
expect(contains(plan["notes"].dump(), "every case is affected"));
};
"a quarantined case fails without failing the run"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"flaky:fail", "ok:pass"})}});
(void)mtest(root, {"quarantine", "add", "--case", "bash t.sh::flaky", "--reason", "known"});
auto r = mtest(root, {"run", "--json", "bash t.sh"});
expect(eq(r.status, 0));
expect(eq(json_of(r)["status"].get<std::string>(), std::string("pass")));
};
"a bench is judged against its baseline by the policy"_test[kind::e2e][kind::bench] = [] {
auto root = project(
{{"mtest.toml", "[programs.b]\ncommand = [\"bash\", \"b.sh\"]\n[policy.bench]\nlimit = "
"1.10\nspread = 0.05\n"},
{"b.sh", script({"m:measure"}, "measure() { mtest_metric t \"$T\" s $T $T $T; }\n")}});
(void)mtest(root, {"run", "--env", "T=1.0"});
auto same = json_of(mtest(root, {"run", "--json", "--env", "T=1.05"}));
auto pass = json_of(mtest(root, {"analyze", "bench", "--json"}));
expect(eq(pass["items"][0]["verdict"].get<std::string>(), std::string("pass")))
<< pass.dump();
(void)mtest(root, {"run", "--env", "T=2.0"});
auto over = json_of(mtest(root, {"gate", "--set", "release", "--json"}));
expect(!over["pass"].get<bool>());
expect(contains(over["reasons"][0].get<std::string>(), "fail"));
};
"a case over its processor budget fails the gate unless a waiver names it"_test[kind::
e2e] = [] {
const std::string slow =
script({"slow:spend", "light:pass"}, "spend() { mtest_metric cpu.user 5000 ms; }\n");
auto root = project(
{{"mtest.toml",
"[programs.b]\ncommand = [\"bash\", \"b.sh\"]\n[budget]\ncase_cpu = \"1s\"\n"},
{"b.sh", slow}});
(void)mtest(root, {"run"});
auto over = json_of(mtest(root, {"gate", "--set", "full", "--json"}));
expect(!over["pass"].get<bool>()) << over.dump();
expect(contains(over.dump(), "over the case budget")) << over.dump();
(void)mcpplibs::os::fs::
write(root / "mtest.toml",
"[programs.b]\ncommand = [\"bash\", \"b.sh\"]\n[budget]\ncase_cpu = \"1s\"\n"
"[[budget.waiver]]\ncase = \"b::slow\"\nreason = \"it builds a toolchain\"\n");
auto waived = json_of(mtest(root, {"gate", "--set", "full", "--json"}));
expect(waived["pass"].get<bool>()) << waived.dump();
(void)mcpplibs::os::fs::
write(root / "mtest.toml",
"[programs.b]\ncommand = [\"bash\", \"b.sh\"]\n[budget]\ncase_cpu = \"10s\"\n"
"[[budget.waiver]]\ncase = \"b::slow\"\nreason = \"it builds a toolchain\"\n");
auto stale = json_of(mtest(root, {"gate", "--set", "full", "--json"}));
expect(!stale["pass"].get<bool>()) << stale.dump();
expect(contains(stale.dump(), "excuses nothing")) << stale.dump();
};
"a ledger's requirement is met by a passing case's clause or excused by a waiver"_test
[kind::e2e] = [] {
auto root = project({{"mtest.toml", "[programs.b]\ncommand = [\"bash\", "
"\"b.sh\"]\n[ledger.spec]\nfile = \"reqs.txt\"\n"},
{"reqs.txt", "# the clauses\nR1\nR2\n"},
{"b.sh", script({"a:ok --clause R1"})}});
(void)mtest(root, {"run"});
auto missing = json_of(mtest(root, {"gate", "--set", "full", "--json"}));
expect(!missing["pass"].get<bool>()) << missing.dump();
expect(contains(missing.dump(), "R2 has no passing case")) << missing.dump();
expect(!contains(missing.dump(), "R1 has no").held) << missing.dump();
// A set that runs a part of the cases does not judge a ledger, and says so.
auto part = json_of(mtest(root, {"gate", "--set", "pr", "--json"}));
expect(part["pass"].get<bool>()) << part.dump();
expect(contains(part.dump(), "judged by a set that runs every case")) << part.dump();
(void)mcpplibs::os::fs::
write(root / "mtest.toml",
"[programs.b]\ncommand = [\"bash\", \"b.sh\"]\n[ledger.spec]\nfile = "
"\"reqs.txt\"\n"
"[[ledger.spec.waiver]]\nitem = \"R2\"\nreason = \"not implemented yet\"\n");
auto excused = json_of(mtest(root, {"gate", "--set", "full", "--json"}));
expect(excused["pass"].get<bool>()) << excused.dump();
};
"a program is told the run's budget and the slot held for it"_test[kind::e2e] = [] {
auto root = project(
{{"mtest.toml", "[programs.b]\ncommand = [\"bash\", \"b.sh\"]\n"},
{"b.sh", script({"budget:told"},
"told() { [ -d \"$MTEST_BUDGET\" ] && [ -f \"$MTEST_BUDGET_HELD\" ] "
"|| mtest_eq told untold; }\n")}});
auto r = mtest(root, {"run", "--jobs", "2"});
expect(e2e::succeeded(r)) << r.out << r.err;
std::optional<json> run;
for (const auto& e : std::filesystem::recursive_directory_iterator(root / ".mtest"))
if (e.path().filename() == "run.json")
run = json::parse(*mcpplibs::os::fs::read(e.path()), nullptr, false);
expect(fatal(run.has_value()));
expect(eq(run->value("budget", 0), 2)) << run->dump();
};
"jitter is stated to the programs and recorded with its seed"_test[kind::e2e] = [] {
auto root = project(
{{"mtest.toml", "[programs.b]\ncommand = [\"bash\", \"b.sh\"]\n"},
{"b.sh", script({"j:told"},
"told() { [ \"$MTEST_JITTER\" = 5 ] && [ -n \"$MTEST_JITTER_SEED\" ] "
"|| mtest_eq told untold; }\n")}});
auto r = mtest(root, {"run", "--jitter", "5", "--jitter-seed", "7"});
expect(e2e::succeeded(r)) << r.out << r.err;
std::optional<json> run;
for (const auto& e : std::filesystem::recursive_directory_iterator(root / ".mtest"))
if (e.path().filename() == "run.json")
run = json::parse(*mcpplibs::os::fs::read(e.path()), nullptr, false);
expect(fatal(run.has_value()));
expect(eq((*run)["jitter"].value("max_ms", 0), 5)) << run->dump();
expect(eq((*run)["jitter"].value("seed", std::uint64_t{0}), std::uint64_t{7}))
<< run->dump();
};
"a run records the machine's class, its processors and memory"_test[kind::e2e] = [] {
auto root = project({{"mtest.toml", "[programs.b]\ncommand = [\"bash\", \"b.sh\"]\n"},
{"b.sh", script({"a:pass"})}});
(void)mtest(root, {"run"});
std::optional<json> run;
for (const auto& e : std::filesystem::recursive_directory_iterator(root / ".mtest"))
if (e.path().filename() == "run.json")
run = json::parse(*mcpplibs::os::fs::read(e.path()), nullptr, false);
expect(fatal(run.has_value()));
expect(!run->value("machine", "").empty()) << run->dump();
expect(gt(run->value("processors", 0), 0)) << run->dump();
};
"the graph names programs, cases, resources and artifacts"_test[kind::e2e] = [] {
auto root = project({{"t.sh", "#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() +
"'\nok() { :; }\nmtest_case db ok --resource serial:db "
"--artifact file:data.txt\nmtest_report\n"}});
auto j = json_of(mtest(root, {"graph", "--json", "bash t.sh"}));
std::set<std::string> kinds;
for (const auto& n : j["nodes"])
kinds.insert(n["kind"].get<std::string>());
expect(kinds == std::set<std::string>{"program", "case", "resource", "artifact"})
<< j.dump();
auto dot = mtest(root, {"graph", "--format", "dot", "bash t.sh"});
expect(contains(dot.out, "digraph mtest"));
};
"a run as JUnit XML"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"a:pass", "b:fail"})}});
(void)mtest(root, {"run", "--junit", "report.xml", "bash t.sh"});
auto xml = std::filesystem::exists(root / "report.xml");
expect(xml);
};
"the store keeps every run and verifies itself"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"a:pass"})}});
for (int i = 0; i < 3; ++i)
(void)mtest(root, {"run", "bash t.sh"});
auto v = mtest(root, {"store", "verify", "--json"});
expect(eq(v.status, 0)) << v.out;
auto g =
json_of(mtest(root, {"store", "gc", "--keep-days", "0", "--keep-runs", "1", "--json"}));
expect(eq(g["removed"].get<int>(), 2));
expect(std::filesystem::exists(root / ".mtest" / ".gitignore"));
};
"the service answers JSON-RPC and MCP on its standard streams"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"a:pass"})}});
const std::string input =
R"({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}})"
"\n"
R"({"jsonrpc":"2.0","id":2,"method":"tools/list"})"
"\n"
R"({"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"run","arguments":{"programs":["bash t.sh"]}}})"
"\n"
R"({"jsonrpc":"2.0","id":4,"method":"status"})"
"\n";
auto r = mtest(root, {"serve"}, input);
std::map<int, json> replies;
std::string_view rest = r.out;
while (!rest.empty()) {
auto nl = rest.find('\n');
auto j = json::parse(rest.substr(0, nl), nullptr, false);
rest = nl == std::string_view::npos ? std::string_view{} : rest.substr(nl + 1);
if (!j.is_discarded() && j.contains("id") && j["id"].is_number())
replies[j["id"].get<int>()] = j;
}
expect(fatal(replies.size() == 4)) << r.out;
expect(eq(replies[1]["result"]["serverInfo"]["name"].get<std::string>(),
std::string("mtest")));
expect(gt(replies[2]["result"]["tools"].size(), std::size_t(10)));
expect(eq(replies[3]["result"]["structuredContent"]["status"].get<std::string>(),
std::string("pass")));
expect(!replies[3]["result"]["isError"].get<bool>());
expect(replies[4]["result"]["data"].contains("conclusion"));
};
"a test session runs the cases asked for, from the launches mcpp gives (spec O9)"_test
[kind::e2e] = [] {
// mcpp's part, B5: the program built, and how it starts.
auto root =
project({{"t.sh", script({"a:pass", "b:fail"})},
{"mcpp.sh", "echo '{\"test\":\"t\",\"status\":\"built\",\"protocol\":1,"
"\"launch\":{\"argv\":[\"bash\",\"t.sh\"],\"env\":{},\"cwd\":"
"\"\",\"via\":\"\"}}'\n"}});
const json run = {{"jsonrpc", "2.0"},
{"id", 1},
{"method", "test/run"},
{"params",
{{"project", root.generic_string()},
{"options", {{"mcpp", {"bash", "mcpp.sh"}}, {"isolation", "case"}}},
{"selection", {{{"program", "t"}, {"cases", {"a", "b"}}}}}}}};
auto r = mtest(root, {"serve"}, run.dump() + "\n");
std::vector<json> notes;
json reply;
std::string_view rest = r.out;
while (!rest.empty()) {
auto nl = rest.find('\n');
auto j = json::parse(rest.substr(0, nl), nullptr, false);
rest = nl == std::string_view::npos ? std::string_view{} : rest.substr(nl + 1);
if (j.is_discarded())
continue;
if (j.contains("id"))
reply = j;
else
notes.push_back(j);
}
expect(fatal(reply.contains("result"))) << r.out << r.err;
const auto id = reply["result"]["run"].get<std::string>();
auto count = [&](std::string_view method, std::string_view key,
std::string_view value) {
return std::ranges::count_if(notes, [&](const json& n) {
return n.value("method", "") == method &&
n["params"].value(std::string(key), "") == value;
});
};
expect(eq(count("test/build", "event", "end"), 1)) << r.out;
// A process per case: two programs, each case's end.
expect(eq(count("test/program", "program", "t"), 2)) << r.out;
expect(eq(count("test/case", "event", "end"), 2)) << r.out;
const auto end = std::ranges::find_if(notes, [](const json& n) {
return n.value("method", "") == "test/run";
});
expect(fatal(end != notes.end())) << r.out;
expect(eq((*end)["params"]["run"].get<std::string>(), id));
expect(eq((*end)["params"]["status"].get<std::string>(), std::string("fail")));
expect(eq((*end)["params"]["counts"].value("pass", 0), 1));
expect(eq((*end)["params"]["counts"].value("fail", 0), 1));
};
"every answer carries a conclusion"_test[kind::e2e] = [] {
auto root = project({{"t.sh", script({"a:pass"})}});
(void)mtest(root, {"run", "bash t.sh"});
for (std::vector<std::string> args :
{std::vector<std::string>{"status", "--json"},
{"report", "--format", "json"},
{"analyze", "durations", "--json"},
{"analyze", "failures", "--json"},
{"analyze", "flaky", "--json"},
{"diff", "--before", "last", "--after", "last", "--json"},
{"history", "--case", "a", "--json"},
{"query", "--status", "pass", "--json"}}) {
auto j = json_of(mtest(root, args));
expect(j.contains("conclusion")) << args.front();
}
};
"a program judged by its exit status says it did not apply: skip_exit"_test[kind::e2e] = [] {
auto root = project(
{{"suite/01_holds.sh", "exit 0\n"},
{"suite/02_absent.sh", "echo 'SKIP: no emulator on this machine'\nexit 77\n"},
{"suite/03_broken.sh", "exit 3\n"},
{"mtest.toml", "[discover.e2e]\nglobs = [\"suite/[0-9]*.sh\"]\ncommand = [\"bash\", "
"\"{path}\"]\nskip_exit = 77\n"}});
auto j = json_of(mtest(root, {"run", "--json"}));
expect(eq(j["counts"].value("pass", 0), 1)) << j.dump();
expect(eq(j["counts"].value("skip", 0), 1)) << j.dump();
expect(eq(j["counts"].value("fail", 0), 1)) << j.dump();
// The reason is the script's own `SKIP` line, kept with the case.
auto skipped = json_of(mtest(root, {"query", "--status", "skip", "--json"}));
expect(contains(skipped.dump(), "02_absent")) << skipped.dump();
expect(contains(skipped.dump(), "no emulator on this machine")) << skipped.dump();
// Without the key the same status is a failure, as it always was.
auto plain = project({{"a.sh", "exit 77\n"}});
auto k = json_of(mtest(plain, {"run", "--json", "bash a.sh"}));
expect(eq(k["counts"].value("fail", 0), 1)) << k.dump();
expect(eq(k["counts"].value("skip", 0), 0)) << k.dump();
};
"a discovered group: a command per file, tags and resources from its first lines"_test
[kind::e2e] = [] {
auto root = project(
{{"suite/01_a.sh", "# requires: gcc elf\n# resources: serial:home\nexit 0\n"},
{"suite/02_b.sh", "# requires:\nexit 3\n"},
{"suite/helper.txt", ""},
{"suite/helper.sh", "exit 7\n"},
{"mtest.toml",
"[discover.e2e]\nglobs = [\"suite/[0-9]*.sh\"]\ncommand = [\"bash\", "
"\"{path}\"]\n"
"kind = \"e2e\"\ntimeout = \"30s\"\n"
"tags_from = { \"# requires:\" = \"requires:\" }\nresources_from = [\"# "
"resources:\"]\n"
"[cases.\"suite/02_*\"]\ntags = [\"slow\"]\n"}});
auto plan = json_of(mtest(root, {"plan", "--json"}));
expect(eq(plan["selected"].get<int>(), 2)) << plan.dump();
auto graph = json_of(mtest(root, {"graph", "--json"}));
std::set<std::string> nodes;
for (const auto& n : graph["nodes"])
nodes.insert(n.value("id", ""));
expect(nodes.contains("resource:serial:home")) << graph.dump();
auto gcc = json_of(mtest(root, {"plan", "--json", "--exclude-tag", "requires:gcc"}));
expect(eq(gcc["selected"].get<int>(), 1));
auto rest = json_of(mtest(root, {"plan", "--json", "--exclude-program", "suite/02*"}));
expect(eq(rest["selected"].get<int>(), 1));
auto slow = json_of(mtest(root, {"plan", "--json", "--tag", "slow"}));
expect(
eq(slow["units"][0]["program"].get<std::string>(), std::string("suite/02_b.sh")));
auto both =
json_of(mtest(root, {"plan", "--json", "--tag", "requires:gcc&requires:elf"}));
expect(eq(both["selected"].get<int>(), 1)) << "a case with both tags";
auto neither = json_of(mtest(root, {"plan", "--json", "--tag", "requires:gcc&slow"}));
expect(eq(neither["selected"].get<int>(), 0)) << "no case has both";
auto run = json_of(mtest(root, {"run", "--json"}));
expect(eq(run["counts"].value("fail", 0), 1));
expect(eq(run["failing"][0]["kind"].get<std::string>(), std::string("e2e")))
<< run.dump();
};
"parts of a selection: every program in one part, the longest first"_test[kind::e2e] = [] {
auto root =
project({{"a.sh", "sleep 0.4\n"},
{"b.sh", "sleep 0.2\n"},
{"c.sh", "exit 0\n"},
{"d.sh", "exit 0\n"},
{"mtest.toml",
"[discover.s]\nglobs = [\"*.sh\"]\ncommand = [\"bash\", \"{path}\"]\n"}});
(void)mtest(root, {"run"});
std::multiset<std::string> seen;
for (const char* part : {"1/2", "2/2"}) {
auto j = json_of(mtest(root, {"plan", "--json", "--shard", part}));
for (const auto& u : j["units"])
seen.insert(u["program"].get<std::string>());
}
expect(eq(seen.size(), std::size_t(4)));
expect(eq(seen.count("a.sh"), std::size_t(1)));
auto first = json_of(mtest(root, {"plan", "--json", "--shard", "1/2"}));
expect(eq(first["units"][0]["program"].get<std::string>(), std::string("a.sh")))
<< "the longest program opens the first part";
expect(eq(mtest(root, {"plan", "--shard", "3/2"}).status, 2));
};
"a coverage map decides which cases a change affects"_test[kind::e2e] = [] {
auto root = project(
{{"a.sh", "exit 0\n"},
{"b.sh", "exit 0\n"},
{"mtest.toml", "[discover.s]\nglobs = [\"*.sh\"]\ncommand = [\"bash\", \"{path}\"]\n"},
{"map.json",
R"({"format":"mtest-coverage/1","commit":"c0ffee","time":"t","epoch":)" +
std::to_string(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()) +
R"(,"cases":{"a.sh::a.sh":["src/x.cpp"],"b.sh::b.sh":["src/y.cpp"]}})"}});
auto before = json_of(mtest(root, {"plan", "--json", "--changed", "src/x.cpp"}));
expect(eq(before["selected"].get<int>(), 2))
<< "without a map, a program that states nothing is affected";
expect(eq(mtest(root, {"coverage", "import", "--from", "map.json"}).status, 0));
auto x = json_of(mtest(root, {"plan", "--json", "--changed", "src/x.cpp"}));
expect(eq(x["selected"].get<int>(), 1)) << x.dump();
expect(contains(x["units"][0]["cases"][0]["why"].get<std::string>(), "ran src/x.cpp"));
auto shown = json_of(mtest(root, {"coverage", "--json"}));
expect(shown["fresh"].get<bool>());
};
"a run under --coverage reads the counters each program's .gcda files record"_test[kind::
e2e] = [] {
// GCC's format, written by the program itself: a.gcda with an arc that ran, b.gcda with a
// run of zero counters (a negative length, GCC 12 and later).
const std::string header =
R"(\x61\x64\x63\x67\x2a\x31\x36\x42\x01\x02\x03\x04\x05\x06\x07\x08)"
R"(\x00\x00\x00\x01\x0c\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00)";
auto root = project(
{{"t.sh",
"mkdir -p \"$GCOV_PREFIX\"\nprintf '" + header +
R"(\x00\x00\xa1\x01\x08\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' > "$GCOV_PREFIX/a.gcda"
printf ')" + header +
R"(\x00\x00\xa1\x01\xf8\xff\xff\xff\x00\x00\x00\x00' > "$GCOV_PREFIX/b.gcda"
)"},
{"compile_commands.json",
R"([{"directory":".","file":"a.c","output":"obj/a.o"},{"directory":".","file":"b.c","output":"obj/b.o"}])"},
{"mtest.toml",
"[programs.t]\ncommand = [\"bash\", \"t.sh\"]\n"
"[coverage]\nobjects = \"obj\"\ndatabase = \"compile_commands.json\"\n"}});
std::filesystem::create_directories(root / "obj");
auto run = json_of(mtest(root, {"run", "--json", "--coverage"}));
expect(eq(run["status"].get<std::string>(), std::string("pass"))) << run.dump();
auto map = json_of(mtest(root, {"coverage", "--json"}));
expect(eq(map["cases"]["t::t"], json::array({"a.c"}))) << map.dump();
// The function whose arcs ran, by its ident (0.11.0), and how widely the cases share it.
expect(eq(map["functions"]["t::t"], json::array({"a.c#1"}))) << map.dump();
auto spread = json_of(mtest(root, {"coverage", "spread", "--json"}));
expect(eq(spread["functions"].get<int>(), 1)) << spread.dump();
expect(near(spread["median_function_share"].get<double>(), 1.0, 1e-9)) << spread.dump();
};
"a run under --coverage maps each case to the sources it executed"_test[kind::e2e] = [] {
// GCC's own `.gcda` files, where a plain `gcc` builds a program here.
auto gcc = mcpplibs::os::env::find_executable("gcc");
if (mcpplibs::os::host() != mcpplibs::os::platform::linux || !gcc)
return;
auto root =
project({{"a.c", "int used(int x) { return x + 1; }\n"},
{"b.c", "int unused(int x) { return x * 2; }\n"},
{"main.c", "int used(int);\nint main(void) { return used(-1); }\n"},
{"mtest.toml",
"[programs.p]\ncommand = [\"./prog\"]\n"
"[coverage]\nobjects = \"obj\"\ndatabase = \"compile_commands.json\"\n"}});
std::filesystem::create_directories(root / "obj");
std::string db = "[";
for (const char* f : {"a", "b", "main"}) {
auto r =
e2e::run({.argv = {mcpplibs::os::fs::utf8(*gcc), "--coverage", "-c",
std::string(f) + ".c", "-o", "obj/" + std::string(f) + ".o"},
.cwd = root});
if (r.status != 0)
return; // no plain C toolchain on this machine: the case above covers the format
db += std::format(R"({}{{"directory":"{}","file":"{}.c","output":"obj/{}.o"}})",
db.size() > 1 ? "," : "", mcpplibs::os::fs::generic_utf8(root), f, f);
}
std::ofstream(root / "compile_commands.json") << db << "]";
auto link = e2e::run({.argv = {mcpplibs::os::fs::utf8(*gcc), "--coverage", "obj/a.o",
"obj/b.o", "obj/main.o", "-o", "prog"},
.cwd = root});
if (link.status != 0)
return;
auto run = json_of(mtest(root, {"run", "--json", "--coverage"}));
expect(eq(run["status"].get<std::string>(), std::string("pass"))) << run.dump();
auto map = json_of(mtest(root, {"coverage", "--json"}));
const auto& files = map["cases"]["p::p"];
expect(std::ranges::find(files, json("a.c")) != files.end()) << map.dump();
expect(std::ranges::find(files, json("b.c")) == files.end()) << map.dump();
auto changed = json_of(mtest(root, {"plan", "--json", "--changed", "b.c"}));
expect(eq(changed["selected"].get<int>(), 0)) << changed.dump();
};
"the cases a test file declares, read without building it"_test[kind::e2e] = [] {
auto root = project(
{{"tests/unit/parse.cpp",
"import mcpplibs.mtest;\nusing namespace mcpplibs::mtest;\n"
"suite parsing{\"parsing\", [] {\n"
" \"an empty line\"_test[tag{\"fast\"}] = [] { expect(true); };\n"
" // \"commented\"_test = [] {};\n"
" \"fields\"_test[kind::e2e] = [] {\n \"one\"_test = [] {};\n };\n"
"}};\n"},
{"mcpp.toml",
"[package]\nname = \"p\"\nversion = \"0.1.0\"\n"
"[package.metadata.mtest.cases.\"parsing/fields*\"]\nresources = [\"serial:db\"]\n"},
{"mtest.toml", "[host]\nkind = \"mcpp\"\n"}});
auto j = json_of(mtest(root, {"list", "--static", "--json"}));
expect(fatal(eq(j["cases"].get<int>(), 4))) << j.dump();
const auto& cases = j["programs"][0]["cases"];
expect(eq(j["programs"][0]["program"].get<std::string>(), std::string("unit/parse")));
expect(eq(cases[0]["case"].get<std::string>(), std::string("parsing")));
expect(eq(cases[0]["kind"].get<std::string>(), std::string("unit")));
expect(eq(cases[1]["case"].get<std::string>(), std::string("parsing/an empty line")));
expect(eq(cases[1]["tags"][0].get<std::string>(), std::string("fast")));
expect(eq(cases[1]["line"].get<int>(), 4));
expect(eq(cases[2]["kind"].get<std::string>(), std::string("e2e")));
expect(eq(cases[3]["case"].get<std::string>(), std::string("parsing/fields/one")));
expect(eq(cases[2]["resources"][0].get<std::string>(), std::string("serial:db")));
};
"a case that states no kind is of its program's directory"_test[kind::e2e] = [] {
const std::string head =
"#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() + "'\nok() { :; }\n";
auto root = project(
{{"tests/e2e/t.sh",
head + "mtest_case plain ok\nmtest_case stated ok --kind unit\nmtest_report\n"},
{"tests/l0.sh", "exit 0\n"},
{"tests/cts/l0.sh", "exit 0\n"},
{"mtest.toml",
"[discover.all]\nglobs = [\"tests/**/*.sh\"]\ncommand = [\"bash\", \"{path}\"]\n"}});
auto plan = json_of(mtest(root, {"plan", "--json"}));
std::map<std::string, std::string> kinds;
for (const auto& u : plan["units"])
for (const auto& c : u["cases"])
kinds[u.value("program", "") + "::" + c.value("case", "")] = c.value("kind", "");
expect(eq(kinds["tests/e2e/t.sh::plain"], std::string("e2e"))) << plan.dump();
expect(eq(kinds["tests/e2e/t.sh::stated"], std::string("unit")));
expect(eq(kinds["tests/l0.sh::tests/l0.sh"], std::string("unit")));
expect(eq(kinds["tests/cts/l0.sh::tests/cts/l0.sh"], std::string("cts")));
};
"a case's time limit ends the process that runs it alone"_test[kind::e2e][tag{"slow"}] = [] {
const std::string head = "#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() + "'\n";
auto root = project(
{{"t.sh",
head + "ok() { :; }\nhang() { sleep 30; }\n"
"mtest_case quick ok\nmtest_case hangs hang --timeout 1\nmtest_report\n"}});
const auto began = std::chrono::steady_clock::now();
auto j = json_of(mtest(root, {"run", "--json", "--isolate", "bash t.sh"}));
expect(eq(j["counts"].value("pass", 0), 1)) << j.dump();
expect(eq(j["counts"].value("timeout", 0), 1));
expect(lt(std::chrono::steady_clock::now() - began, std::chrono::seconds{20}));
};
"a suite's kind and attributes, read without building"_test[kind::e2e] = [] {
auto root = project({{"tests/cts/spec.cpp",
"import mcpplibs.mtest;\nusing namespace mcpplibs::mtest;\n"
"CTS spec{\"spec\", clause{\"S1\"}, resource{\"machine\"}, [] {\n"
" \"answers\"_test[clause{\"S2\"}] = [] {};\n}};\n"
"Suite plain{\"plain\", [] { \"by place\"_test = [] {}; }};\n"},
{"mcpp.toml", "[package]\nname = \"p\"\nversion = \"0.1.0\"\n"},
{"mtest.toml", "[host]\nkind = \"mcpp\"\n"}});
auto j = json_of(mtest(root, {"list", "--static", "--json"}));
const auto& cases = j["programs"][0]["cases"];
expect(fatal(eq(cases.size(), std::size_t(4)))) << j.dump();
expect(eq(cases[0]["case"].get<std::string>(), std::string("spec")));
expect(eq(cases[0]["kind"].get<std::string>(), std::string("cts")));
expect(eq(cases[0]["clauses"], json{"S1"}));
expect(eq(cases[1]["clauses"], json{"S1", "S2"}));
expect(eq(cases[1]["resources"], json{"machine"}));
expect(eq(cases[3]["case"].get<std::string>(), std::string("plain/by place")));
expect(eq(cases[3]["kind"].get<std::string>(), std::string("cts"))) << "by its directory";
};
"a suite type a module beside the tests declares"_test[kind::e2e] = [] {
auto root = project(
{{"tests/bench/timed.cppm",
"export module timed;\nimport mcpplibs.mtest;\n"
"export class Timed : public mcpplibs::mtest::Bench {\npublic:\n"
" using Bench::Bench;\n};\n"},
{"tests/bench/startup.cpp", "import timed;\nTimed startup{\"startup\", [] {}};\n"},
{"mcpp.toml", "[package]\nname = \"p\"\nversion = \"0.1.0\"\n"},
{"mtest.toml", "[host]\nkind = \"mcpp\"\n"}});
auto j = json_of(mtest(root, {"list", "--static", "--json"}));
const auto& cases = j["programs"][0]["cases"];
expect(fatal(eq(cases.size(), std::size_t(1)))) << j.dump();
expect(eq(cases[0]["kind"].get<std::string>(), std::string("bench")));
expect(eq(cases[0]["resources"], json{"machine"}));
};
"a case's isolation, retries and diagnosis, as the program states them"_test[kind::e2e] = [] {
const std::string head = "#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() + "'\n";
auto root = project(
{{"t.sh", head + "pid() { echo $$ >> pids.txt; }\n"
"second() { [ -e tried ] || { touch tried; mtest_eq 1 2; }; }\n"
"diag() { echo \"diagnosing ${DIAG:-no}\"; mtest_eq 1 2; }\n"
"mtest_case alone pid --isolate\nmtest_case with pid\n"
"mtest_case again second --retries 2\n"
"mtest_case diag diag --diagnose-env DIAG=yes\nmtest_report\n"}});
auto j = json_of(mtest(root, {"run", "--json", "bash t.sh"}));
std::ifstream pids(root / "pids.txt");
std::set<std::string> distinct;
for (std::string line; std::getline(pids, line);)
distinct.insert(line);
expect(eq(distinct.size(), std::size_t(2))) << "the isolated case shared a process";
expect(eq(j["counts"].value("flaky", 0), 1)) << j.dump();
expect(fatal(eq(j["failing"].size(), std::size_t(1))));
const auto dir = j["failing"][0]["diagnosis"].get<std::string>();
auto out = mcpplibs::os::fs::read(mcpplibs::os::fs::from_utf8(dir) / "out.txt");
expect(fatal(out.has_value())) << dir;
expect(contains(*out, "diagnosing yes"));
auto explained = mtest(root, {"explain"});
expect(contains(explained.out, "diagnosis: "));
};
"mtest.toml states what a program cannot: the same attributes, after the program's"_test
[kind::e2e] = [] {
const std::string head =
"#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() + "'\n";
auto root = project(
{{"t.sh", head + "ok() { :; }\nbad() { mtest_eq 1 2; }\n"
"mtest_case known bad\nmtest_case off bad\nmtest_case own ok "
"--kind e2e\nmtest_report\n"},
{"l0.sh", "exit 1\n"},
{"mtest.toml", "[cases.\"known\"]\nexpect_fail = \"a defect upstream\"\n"
"[cases.\"off\"]\nskip = \"not on this host\"\n"
"[cases.\"own\"]\nkind = \"bench\"\nclauses = [\"S1\"]\n"
"[cases.\"bash l0.sh\"]\nexpect_fail = \"exits 1 by design\"\n"}});
auto j = json_of(mtest(root, {"run", "--json", "--report-json", "r.json", "bash t.sh",
"bash l0.sh"}));
expect(eq(j["status"].get<std::string>(), std::string("pass"))) << j.dump();
expect(eq(j["counts"].value("xfail", 0), 2));
expect(eq(j["counts"].value("skip", 0), 1));
auto record = json::parse(*mcpplibs::os::fs::read(root / "r.json"));
for (const auto& c : record["cases"])
if (c.value("id", "") == "own")
expect(eq(c.value("kind", ""), std::string("e2e")))
<< "the program's kind comes first";
};
"a run's timeline: when each program started and what it held"_test[kind::e2e] = [] {
auto root =
project({{"a.sh", "sleep 0.3\n"},
{"b.sh", "sleep 0.3\n"},
{"mtest.toml", "[cases.\"bash a.sh\"]\nresources = [\"serial:x\"]\n"
"[cases.\"bash b.sh\"]\nresources = [\"serial:x\"]\n"}});
json_of(mtest(root, {"run", "--json", "--jobs", "2", "--report-json", "r.json", "bash a.sh",
"bash b.sh"}));
auto record = json::parse(*mcpplibs::os::fs::read(root / "r.json"));
std::vector<double> starts;
for (const auto& p : record["timeline"]) {
expect(eq(p["resources"], json{"serial:x"}));
starts.push_back(p.value("started_ms", 0.0));
}
expect(fatal(eq(starts.size(), std::size_t(2))));
expect(ge(std::abs(starts[0] - starts[1]), 250.0))
<< "two holders of one serial group overlapped";
};
"a case's metrics across runs beside their bounds (analyze trend)"_test[kind::e2e] = [] {
const std::string head = "#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() + "'\n";
auto root = project(
{{"t.sh", head + "fast() { mtest_metric ready \"${READY:-3}\" s; }\n"
"mtest_case startup fast --at-most ready=15\nmtest_report\n"}});
auto first = json_of(mtest(root, {"run", "--json", "bash t.sh"}));
expect(eq(first["status"].get<std::string>(), std::string("pass"))) << first.dump();
auto over = e2e::run(
{.argv = {e2e::program("mtest"), "run", "--json", "--env", "READY=20", "bash t.sh"},
.cwd = root});
auto second = json_of(over);
expect(eq(second["status"].get<std::string>(), std::string("fail"))) << second.dump();
expect(contains(second["failing"][0]["why"].get<std::string>(), "over its bound 15"));
auto t = json_of(mtest(root, {"analyze", "trend", "--json"}));
expect(fatal(eq(t["items"].size(), std::size_t(1)))) << t.dump();
const auto& item = t["items"][0];
expect(eq(item["values"], json{3, 20}));
expect(eq(item["bound"]["max"].get<double>(), 15.0));
expect(lt(item["margin"].get<double>(), 0.0)) << "the last value is past its bound";
expect(contains(t["conclusion"].get<std::string>(), "1 past its bound"));
};
"mtest.toml bounds a metric of a program that states none"_test[kind::e2e] = [] {
const std::string head = "#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() + "'\n";
auto root = project(
{{"t.sh", head + "m() { mtest_metric size 5 MB; }\nmtest_case sized m\nmtest_report\n"},
{"mtest.toml", "[cases.\"sized\"]\nat_most = { size = 3 }\n"}});
auto j = json_of(mtest(root, {"run", "--json", "bash t.sh"}));
expect(eq(j["status"].get<std::string>(), std::string("fail"))) << j.dump();
expect(contains(j["failing"][0]["why"].get<std::string>(), "over its bound 3"));
};
"output that is not UTF-8 is stored and reported, not refused"_test[kind::e2e] = [] {
auto root = project(
{{"bad.sh", "printf 'cp1252 \\x97 \\xff\\n'; printf '\\x97\\n' >&2; exit 1\n"}});
auto r = mtest(root, {"run", "--json", "bash bad.sh"});
expect(eq(r.status, 1)) << r.err;
auto j = json_of(r);
expect(eq(j["counts"].value("fail", 0), 1));
expect(mcpplibs::os::fs::read(root / ".mtest" / "index.ndjson").has_value());
};
// ---- 0.5.0: the run as a graph, its report, its use of the machine, its fixtures ----------
"a run's critical path is the longest path of its graph"_test[kind::e2e] = [] {
// One worker: the second program waited for the first, so both are on the path.
auto root = project({{"a.sh", "sleep 0.3\n"}, {"b.sh", "sleep 0.1\n"}});
expect(fatal(
eq(mtest(root, {"run", "--json", "--jobs", "1", "bash a.sh", "bash b.sh"}).status, 0)));
auto j = json_of(mtest(root, {"analyze", "path", "--json"}));
expect(fatal(eq(j["path"].size(), std::size_t(2)))) << j.dump();
expect(eq(j["path"][1]["waited_for"].get<std::string>(), std::string("worker")));
expect(ge(j["path_ms"].get<double>(), 350.0));
auto g = mtest(root, {"graph", "--run", "last", "--format", "mermaid"});
expect(contains(g.out, "flowchart LR"));
expect(contains(g.out, "class n0,n1 crit")) << g.out;
};
"a report gives the builds a host recorded and the cases near their limit"_test[kind::
e2e] = [] {
auto root = project(
// The case takes 85% of its limit; the rest is for a slow host's shell to start.
{{"mtest.toml", "[programs.b]\ncommand = [\"bash\", \"b.sh\"]\ntimeout = \"4s\"\n"},
{"b.sh", script({"near:nap"}, "nap() { sleep 3.4; }\n")}});
(void)mtest(root, {"run"});
std::filesystem::path dir;
for (const auto& e : std::filesystem::recursive_directory_iterator(root / ".mtest"))
if (e.path().filename() == "run.json")
dir = e.path().parent_path();
expect(fatal(!dir.empty()));
(void)mcpplibs::os::fs::
write(dir / "builds.ndjson",
"{\"build\":\"b1\",\"output\":\"obj/big.o\",\"package\":\"app\",\"started_ms\":0,"
"\"ms\":4000,\"cpu_ms\":3900}\n"
"{\"build\":\"b1\",\"package\":\"dep\",\"cached\":true,\"units\":12}\n");
auto r = mtest(root, {"report", "--format", "md", "--sections", "builds,margin"});
expect(contains(r.out, "## Builds")) << r.out;
expect(contains(r.out, "obj/big.o")) << r.out;
expect(contains(r.out, "12 unit(s)")) << r.out;
expect(contains(r.out, "## Margin")) << r.out;
expect(contains(r.out, "b::near")) << r.out;
};
"a markdown report has the sections asked for, with mermaid"_test[kind::e2e] = [] {
auto root = project({{"a.sh", "exit 0\n"}, {"b.sh", "echo broken >&2; exit 3\n"}});
(void)mtest(root, {"run", "--json", "bash a.sh", "bash b.sh"});
auto r =
mtest(root, {"report", "--format", "md", "--sections", "summary,failures,path,gantt"});
expect(contains(r.out, "## Summary"));
expect(contains(r.out, "## Failures"));
expect(contains(r.out, "exit 3"));
expect(contains(r.out, "## Critical path"));
expect(contains(r.out, "```mermaid\ngantt"));
expect(!contains(r.out, "## Slowest cases")) << "a section not asked for is left out";
};
"a run writes its markdown report when asked"_test[kind::e2e] = [] {
auto root =
project({{"a.sh", "exit 0\n"}, {"mtest.toml", "[report]\nsections = [\"summary\"]\n"}});
expect(fatal(eq(mtest(root, {"run", "--json", "--md", "r.md", "bash a.sh"}).status, 0)));
auto text = mcpplibs::os::fs::read(root / "r.md");
expect(fatal(text.has_value()));
expect(contains(*text, "## Summary"));
};
"the runs of a group are one report"_test[kind::e2e] = [] {
auto root = project({{"a.sh", "exit 0\n"}, {"b.sh", "exit 0\n"}});
(void)mtest(root, {"run", "--json", "--group", "ci-7", "bash a.sh"});
(void)mtest(root, {"run", "--json", "--group", "ci-7", "bash b.sh"});
auto r =
mtest(root, {"report", "--format", "md", "--group", "ci-7", "--sections", "summary"});
expect(contains(r.out, "| **all** |")) << r.out;
};
"a program's cases are divided among idle workers"_test[kind::e2e] = [] {
const std::string head = "#!/usr/bin/env bash\n# mtest-protocol=1\n. '" + adapter() + "'\n";
auto root = project({{"t.sh", head + "w() { sleep 0.2; }\nmtest_case a w\nmtest_case b w\n"
"mtest_case c w\nmtest_case d w\nmtest_report\n"}});
expect(fatal(
eq(mtest(root, {"run", "--json", "--jobs", "2", "--report-json", "r.json", "bash t.sh"})
.status,
0)));
auto r = json::parse(*mcpplibs::os::fs::read(root / "r.json"));
expect(fatal(eq(r["timeline"].size(), std::size_t(2)))) << r["timeline"].dump();
expect(eq(r["timeline"][0]["select"].size() + r["timeline"][1]["select"].size(),
std::size_t(4)));
expect(ne(r["timeline"][0]["worker"], r["timeline"][1]["worker"]));
};
"a run fixture is served once and its endpoint reaches its users"_test[kind::e2e] = [] {
auto root = project(
// Ready once the endpoint exists: announced first, a user started at once found no
// directory (seen once in a macOS run of CI, 43359543216).
{{"serve.sh",
"mkdir -p served; echo \"mtest-ready $PWD/served\"; cat >/dev/null; rmdir served\n"},
{"u1.sh", "test -d \"$MTEST_FIXTURE_HOME\" && echo one >> "
"\"$MTEST_FIXTURE_HOME/../uses.txt\"\n"},
{"u2.sh", "test -d \"$MTEST_FIXTURE_HOME\" && echo two >> "
"\"$MTEST_FIXTURE_HOME/../uses.txt\"\n"},
{"mtest.toml", "[fixtures.home]\ncommand = [\"bash\", \"serve.sh\"]\n"
"[cases.\"*u*\"]\nuses = [\"home\"]\n"}});
auto j = json_of(mtest(root, {"run", "--json", "--jobs", "2", "bash u1.sh", "bash u2.sh"}));
expect(eq(j["status"].get<std::string>(), std::string("pass"))) << j.dump();
auto uses = mcpplibs::os::fs::read(root / "uses.txt");
expect(fatal(uses.has_value()));
expect(eq(std::ranges::count(*uses, '\n'), 2));
expect(!std::filesystem::exists(root / "served"))
<< "the fixture ended after its last user";
auto path = json_of(mtest(root, {"graph", "--run", "last", "--json"}));
expect(contains(path.dump(), "fixture:home"));
};
"a program that does not speak the protocol is measured from outside"_test[kind::e2e] = [] {
if (mcpplibs::os::host() == mcpplibs::os::platform::windows)
skip("Windows does not account a program's children");
if (!mcpplibs::os::process::resources(mcpplibs::os::process::of::children))
skip("this system tells no program its children's use of resources");
auto root = project({{"big.sh", "head -c 40000000 /dev/zero | tail -c 1 >/dev/null\n"}});
expect(fatal(
eq(mtest(root, {"run", "--json", "--report-json", "r.json", "bash big.sh"}).status,
0)));
auto r = json::parse(*mcpplibs::os::fs::read(root / "r.json"));
const auto& u = r["timeline"][0]["usage"];
expect(eq(u["source"].get<std::string>(), std::string("engine"))) << u.dump();
expect(gt(u["peak_bytes"].get<double>(), 1.0e6));
};
// --- 0.7.0: capabilities (M1), coverage over collected runs, plan per host (M6), resume (M5)
const std::string discovered =
"[discover.s]\nglobs = [\"[0-9]*.sh\"]\ncommand = [\"bash\", \"{path}\"]\nlevel = 0\n"
"skip_exit = 77\ntags_from = { \"# requires:\" = \"requires:\" }\n";
"a capability this machine lacks makes a skip that names it"_test[kind::e2e] = [&] {
auto root = project(
{{"1_has.sh", "# requires: here\nexit 0\n"},
{"2_lacks.sh", "# requires: absent\nexit 0\n"},
{"mtest.toml", discovered + "[capabilities]\nhere = {}\n"
"absent = { program = \"no-such-program-mtest\" }\n"}});
auto j = json_of(mtest(root, {"run", "--json", "--report-json", "r.json"}));
expect(eq(j["status"].get<std::string>(), std::string("pass"))) << j.dump();
auto r = json::parse(*mcpplibs::os::fs::read(root / "r.json"));
std::string reason;
for (const auto& c : r["cases"])
if (c["program"] == "2_lacks.sh")
reason = c.value("reason", "");
expect(contains(reason, "missing capability: absent")) << r.dump();
expect(eq(r["capabilities"]["here"].get<bool>(), true));
auto caps = json_of(mtest(root, {"capabilities", "--json"}));
expect(eq(caps["capabilities"]["absent"]["present"].get<bool>(), false));
};
"a capability no entry declares is an error, not a skip"_test[kind::e2e] = [&] {