Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Runner/Runner.cpp
blob: 62acac2c291cddd19089e0a51226b91a55e85012 (plain)
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
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1
#include <Progmasoft/Catch3/Runner.hpp>
#include <Progmasoft/Catch3/Version.hpp>
#include <Progmasoft/Catch3/XmlWriter.hpp>
#include <algorithm>
#include <Progmasoft/Catch3/Config.hpp>
#include <Progmasoft/Catch3/Session.hpp>
#include <Progmasoft/Catch3/UserConfig.hpp>
#include <Progmasoft/Catch3/Interfaces/IRegistryHub.hpp>
#include <Progmasoft/Catch3/Interfaces/ITestCaseRegistry.hpp>
#include <Progmasoft/Catch3/Internal/CompilerCapabilities.hpp>
#include <Progmasoft/Catch3/Internal/Context.hpp>
#include <Progmasoft/Catch3/Internal/StartupExceptionRegistry.hpp>
#include <Progmasoft/Catch3/Internal/TestCaseRegistryImpl.hpp>
#include <chrono>
#include <cstdio>
#include <exception>
#include <filesystem>
#include <map>
#include <random>
#include <system_error>

#include "Analysis.hpp"
#include "Catalog.hpp"
#include "Console.hpp"
#include "Execution.hpp"
#include "Files.hpp"
#include "Git.hpp"
#include "Hash.hpp"
#include "History.hpp"
#include "Json.hpp"
#include "Options.hpp"
#include "Planner.hpp"
#include "Process.hpp"
#include "ProjectConfig.hpp"
#include "ResultsJson.hpp"
#include "Scheduler.hpp"

namespace Progmasoft::Catch3
{
    namespace
    {
        using Runner::JsonValue;

        void
        PrintError(const std::string &message)
        {
            std::fprintf(stderr, "error: %s\n", message.c_str());
        }

        /// Problems found while the tests registered, such as two tests of
        /// the same name; reported before anything runs.
        bool
        ReportStartupErrors()
        {
#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
            const auto &exceptions = Catch::getRegistryHub()
                                         .getStartupExceptionRegistry()
                                         .getExceptions();
            if (exceptions.empty())
            {
                return false;
            }
            std::fprintf(stderr, "error: the tests could not be registered:\n");
            for (const auto &exception : exceptions)
            {
                try
                {
                    std::rethrow_exception(exception);
                }
                catch (const std::exception &error)
                {
                    std::fprintf(stderr, "  %s\n", error.what());
                }
                catch (...)
                {
                    std::fprintf(stderr, "  an unknown exception\n");
                }
            }
            return true;
#else
            return false;
#endif
        }

        bool
        BazelTest()
        {
#if defined(CATCH_CONFIG_BAZEL_SUPPORT)
            return true;
#else
            return Runner::HasEnvironment("BAZEL_TEST");
#endif
        }

        /// Bazel passes what it wants through the environment; it wins over
        /// the command line, as `bazel test --test_filter` expects.
        void
        ApplyBazel(Runner::Options &options, std::vector<std::string> &notes)
        {
            const std::string xml = Runner::ReadEnvironment("XML_OUTPUT_FILE");
            if (!xml.empty() && options.junitPath.empty())
            {
                options.junitPath = xml;
            }
            const std::string filter
                = Runner::ReadEnvironment("TESTBRIDGE_TEST_ONLY");
            if (!filter.empty())
            {
                options.testSpecs = { filter };
            }
            const std::string index
                = Runner::ReadEnvironment("TEST_SHARD_INDEX");
            const std::string total
                = Runner::ReadEnvironment("TEST_TOTAL_SHARDS");
            const std::string status
                = Runner::ReadEnvironment("TEST_SHARD_STATUS_FILE");
            if (!index.empty() && !total.empty())
            {
                auto parsed = Runner::ParseOptions(
                    { "--shard=" + index + "/" + total });
                if (parsed)
                {
                    options.shard = parsed->shard;
                    if (!status.empty())
                    {
                        // Bazel needs to see that the shard was read.
                        static_cast<void>(
                            Runner::WriteFileAtomically(status, ""));
                    }
                }
                else
                {
                    notes.push_back("ignoring Bazel's shard " + index + "/"
                                    + total + ": "
                                    + parsed.GetError().Message());
                }
            }
            const std::string seed
                = Runner::ReadEnvironment("TEST_RANDOM_SEED");
            if (!seed.empty())
            {
                auto parsed = Runner::ParseOptions({ "--seed=" + seed });
                if (parsed)
                {
                    options.seed = parsed->seed;
                    options.seedGiven = true;
                }
            }
            const std::string outputs
                = Runner::ReadEnvironment("TEST_UNDECLARED_OUTPUTS_DIR");
            if (!outputs.empty() && options.jsonPath.empty())
            {
                options.jsonPath
                    = (std::filesystem::path{ outputs } / "catch3.json")
                          .string();
            }
            if (options.historyDirectory.empty()
                && !Runner::HasEnvironment("CATCH3_HISTORY"))
            {
                // A sandboxed test has nowhere lasting to keep history.
                options.historyEnabled = false;
            }
        }

        std::filesystem::path
        HistoryDirectory(const Runner::Options &options,
                         const std::filesystem::path &program)
        {
            if (!options.historyDirectory.empty())
            {
                return options.historyDirectory;
            }
            const std::string fromEnvironment
                = Runner::ReadEnvironment("CATCH3_HISTORY");
            if (!fromEnvironment.empty())
            {
                return fromEnvironment;
            }
            return program.parent_path() / ".catch3";
        }

        JsonValue
        Strings(const std::vector<std::string> &values)
        {
            JsonValue array = JsonValue::Array();
            for (const auto &value : values)
            {
                array.Append(value);
            }
            return array;
        }

        /// The discovery protocol: what `--list=json` prints. Its shape is
        /// fixed for a given version; IDE adapters and CMake read it.
        std::string
        ListJson(const Runner::Catalog &catalog,
                 const std::vector<std::size_t> &matched,
                 const std::string &programName)
        {
            JsonValue list = JsonValue::Object();
            list.Set("format", "catch3-tests");
            list.Set("version", 1);
            list.Set("binary", programName);
            list.Set("catch3", kVersion);
            JsonValue tests = JsonValue::Array();
            for (const std::size_t index : matched)
            {
                const auto &entry = catalog.Tests()[index];
                JsonValue test = JsonValue::Object();
                test.Set("id", entry.key);
                test.Set("name", entry.name);
                test.Set("className", entry.className);
                test.Set("file", entry.file);
                test.Set("line", entry.line);
                test.Set("tags", Strings(entry.tags));
                test.Set("hidden", entry.hidden);
                test.Set("serial", entry.serial);
                test.Set("isolated", entry.isolated);
                test.Set("quarantined", entry.quarantined);
                test.Set("resources", Strings(entry.resources));
                test.Set("dependsOn", Strings(entry.dependsOn));
                if (entry.timeout)
                {
                    test.Set("timeoutSeconds",
                             static_cast<double>(entry.timeout->count())
                                 / 1000.0);
                }
                if (entry.retries)
                {
                    test.Set("retries", *entry.retries);
                }
                tests.Append(std::move(test));
            }
            list.Set("tests", std::move(tests));
            return list.ToPrettyString();
        }

        int
        List(const Runner::Options &options,
             const Runner::Catalog &catalog,
             const std::vector<std::size_t> &matched,
             const std::string &programName)
        {
            if (options.mode == Runner::Mode::kListTags)
            {
                std::map<std::string, std::size_t> counts;
                for (const std::size_t index : matched)
                {
                    for (const auto &tag : catalog.Tests()[index].tags)
                    {
                        ++counts[tag];
                    }
                }
                for (const auto &[tag, count] : counts)
                {
                    std::printf("%6zu  [%s]\n", count, tag.c_str());
                }
                std::printf("%zu tag(s)\n", counts.size());
                return 0;
            }
            switch (options.listFormat)
            {
                case Runner::ListFormat::kJson:
                    std::fputs(ListJson(catalog, matched, programName).c_str(),
                               stdout);
                    break;
                case Runner::ListFormat::kNames:
                    for (const std::size_t index : matched)
                    {
                        std::printf("%s\n",
                                    catalog.Tests()[index].name.c_str());
                    }
                    break;
                case Runner::ListFormat::kText:
                    for (const std::size_t index : matched)
                    {
                        const auto &entry = catalog.Tests()[index];
                        std::string tags;
                        for (const auto &tag : entry.tags)
                        {
                            tags += "[" + tag + "]";
                        }
                        std::printf("%s\n    %s:%zu\n",
                                    entry.name.c_str(),
                                    entry.file.c_str(),
                                    entry.line);
                        if (!tags.empty())
                        {
                            std::printf("    %s\n", tags.c_str());
                        }
                    }
                    std::printf("%zu matching test(s)\n", matched.size());
                    break;
            }
            return 0;
        }

        std::string
        FormatMicroseconds(std::int64_t microseconds)
        {
            char buffer[32];
            std::snprintf(buffer,
                          sizeof(buffer),
                          "%.3f s",
                          static_cast<double>(microseconds) / 1e6);
            return buffer;
        }

        /// What the history says about the tests: `--analytics`.
        int
        Analytics(const Runner::Options &options,
                  const Runner::History &history,
                  const std::filesystem::path &file)
        {
            struct Row
            {
                std::string key;
                double failureRate = 0.0;
                std::size_t runs = 0;
                std::size_t flakyEvents = 0;
                std::int64_t median = 0;
                std::int64_t recentMedian = 0;
                std::int64_t olderMedian = 0;
                std::string quarantine;
            };
            std::vector<Row> rows;
            for (const auto &[key, test] : history.Tests())
            {
                Row row;
                row.key = key;
                std::size_t failures = 0;
                std::vector<std::int64_t> recent;
                std::vector<std::int64_t> older;
                std::size_t position = 0;
                for (auto record = test.records.rbegin();
                     record != test.records.rend();
                     ++record, ++position)
                {
                    ++row.runs;
                    failures += IsFailure(record->outcome) ? 1 : 0;
                    if (record->outcome == Outcome::kPassed)
                    {
                        (position < 5 ? recent : older)
                            .push_back(record->durationMicroseconds);
                    }
                }
                const auto median = [](std::vector<std::int64_t> values) {
                    if (values.empty())
                    {
                        return std::int64_t{ 0 };
                    }
                    std::sort(values.begin(), values.end());
                    return values[values.size() / 2];
                };
                row.failureRate = row.runs == 0
                                      ? 0.0
                                      : static_cast<double>(failures)
                                            / static_cast<double>(row.runs);
                row.flakyEvents = test.FlakyEvents(20);
                row.median = test.DurationPercentile(0.5).value_or(0);
                row.recentMedian = median(recent);
                row.olderMedian = median(older);
                if (test.quarantine)
                {
                    row.quarantine = test.quarantine->reason;
                }
                rows.push_back(std::move(row));
            }
            const auto top = [&](auto key, std::size_t count) {
                std::vector<const Row *> ordered;
                for (const auto &row : rows)
                {
                    ordered.push_back(&row);
                }
                std::stable_sort(ordered.begin(),
                                 ordered.end(),
                                 [&](const Row *left, const Row *right) {
                                     return key(*left) > key(*right);
                                 });
                ordered.resize(std::min(count, ordered.size()));
                return ordered;
            };
            const auto flaky = top(
                [](const Row &row) {
                    return static_cast<double>(row.flakyEvents);
                },
                10);
            const auto failing = top(
                [](const Row &row) {
                    return row.failureRate;
                },
                10);
            const auto slowest = top(
                [](const Row &row) {
                    return static_cast<double>(row.median);
                },
                10);
            const auto slower = top(
                [](const Row &row) {
                    return row.olderMedian > 0
                               ? static_cast<double>(row.recentMedian)
                                     / static_cast<double>(row.olderMedian)
                               : 0.0;
                },
                10);

            if (options.analyticsJson)
            {
                JsonValue report = JsonValue::Object();
                report.Set("format", "catch3-analytics");
                report.Set("version", 1);
                report.Set("runs", history.Runs().size());
                report.Set("tests", rows.size());
                JsonValue tests = JsonValue::Array();
                for (const auto &row : rows)
                {
                    JsonValue item = JsonValue::Object();
                    item.Set("id", row.key);
                    item.Set("runs", row.runs);
                    item.Set("failureRate", row.failureRate);
                    item.Set("flakyEvents", row.flakyEvents);
                    item.Set("medianMicroseconds", row.median);
                    item.Set("recentMedianMicroseconds", row.recentMedian);
                    item.Set("olderMedianMicroseconds", row.olderMedian);
                    if (!row.quarantine.empty())
                    {
                        item.Set("quarantine", row.quarantine);
                    }
                    tests.Append(std::move(item));
                }
                report.Set("tests", std::move(tests));
                std::fputs(report.ToPrettyString().c_str(), stdout);
                return 0;
            }
            std::printf("history: %s\n%zu run(s), %zu test(s)\n",
                        file.string().c_str(),
                        history.Runs().size(),
                        rows.size());
            std::printf("\nflaky (last 20 runs):\n");
            for (const Row *row : flaky)
            {
                if (row->flakyEvents > 0)
                {
                    std::printf("  %3zu  %s\n",
                                row->flakyEvents,
                                row->key.c_str());
                }
            }
            std::printf("\nin quarantine:\n");
            for (const auto &row : rows)
            {
                if (!row.quarantine.empty())
                {
                    std::printf("  %s: %s\n",
                                row.key.c_str(),
                                row.quarantine.c_str());
                }
            }
            std::printf("\nfailing most often:\n");
            for (const Row *row : failing)
            {
                if (row->failureRate > 0.0)
                {
                    std::printf("  %3.0f%%  %s\n",
                                row->failureRate * 100.0,
                                row->key.c_str());
                }
            }
            std::printf("\nslowest (median):\n");
            for (const Row *row : slowest)
            {
                std::printf("  %10s  %s\n",
                            FormatMicroseconds(row->median).c_str(),
                            row->key.c_str());
            }
            std::printf(
                "\ngetting slower (last 5 runs against the 15 before):\n");
            for (const Row *row : slower)
            {
                if (row->olderMedian > 0
                    && row->recentMedian > row->olderMedian * 3 / 2)
                {
                    std::printf("  %10s -> %-10s  %s\n",
                                FormatMicroseconds(row->olderMedian).c_str(),
                                FormatMicroseconds(row->recentMedian).c_str(),
                                row->key.c_str());
                }
            }
            return 0;
        }

        std::filesystem::path
        SourceDirectory(const Runner::Catalog &catalog)
        {
            for (const auto &entry : catalog.Tests())
            {
                const std::filesystem::path file{ entry.file };
                if (file.is_absolute())
                {
                    return file.parent_path();
                }
            }
            std::error_code error;
            return std::filesystem::current_path(error);
        }

        std::int64_t
        UnixMilliseconds()
        {
            return std::chrono::duration_cast<std::chrono::milliseconds>(
                       std::chrono::system_clock::now().time_since_epoch())
                .count();
        }

        int
        Run(const std::vector<std::string> &arguments)
        {
            const std::filesystem::path program = Runner::CurrentExecutable(
                arguments.empty() ? nullptr : arguments.front().c_str());
            const std::string programName
                = program.empty() ? "tests" : program.stem().string();
            std::vector<std::string> rest;
            if (arguments.size() > 1)
            {
                rest.assign(arguments.begin() + 1, arguments.end());
            }
            std::error_code here;
            auto parsed = Runner::ParseCommandLine(
                rest, std::filesystem::current_path(here));
            if (!parsed)
            {
                PrintError(parsed.GetError().Message());
                return Catch::UnspecifiedErrorExitCode;
            }
            Runner::Options options = std::move(*parsed);
            options.programName = programName;

            if (options.mode == Runner::Mode::kHelp)
            {
                PrintHelp(programName);
                return 0;
            }
            if (options.mode == Runner::Mode::kVersion)
            {
                std::printf("Catch3 %s\n", kVersion);
                return 0;
            }
            if (ReportStartupErrors())
            {
                return Catch::UnspecifiedErrorExitCode;
            }
            if (options.filenamesAsTags)
            {
                Catch::getMutableRegistryHub()
                    .getMutableTestCaseRegistry()
                    .enableFilenameTags();
            }
            if (options.mode == Runner::Mode::kWorker)
            {
                return Runner::RunWorker(options, programName);
            }

            std::vector<std::string> notes;
            if (!options.configPath.empty()
                && options.verbosity == Runner::Verbosity::kVerbose)
            {
                notes.push_back("configuration from " + options.configPath);
            }
            if (BazelTest())
            {
                ApplyBazel(options, notes);
            }
            if (!options.seedGiven)
            {
                std::random_device device;
                options.seed = device();
            }

            auto catalog = Runner::Catalog::Load();
            if (!catalog)
            {
                PrintError(catalog.GetError().Message());
                return Catch::UnspecifiedErrorExitCode;
            }
            const Catch::Config config{ Runner::ToConfigData(options,
                                                             programName) };
            const auto &invalid = config.testSpec().getInvalidSpecs();
            if (!invalid.empty())
            {
                for (const auto &spec : invalid)
                {
                    PrintError("invalid test spec: " + spec);
                }
                return Catch::InvalidTestSpecExitCode;
            }
            std::vector<std::string> unmatched;
            auto matched = catalog->Match(config, unmatched);
            if (options.mode == Runner::Mode::kListTests
                || options.mode == Runner::Mode::kListTags)
            {
                if (options.shard.count > 1)
                {
                    // A shard lists the tests it would run, dependencies
                    // included, in registration order.
                    Runner::PlanInputs inputs;
                    inputs.catalog = &*catalog;
                    inputs.matched = matched;
                    Runner::Options listing = options;
                    listing.order = Runner::Order::kDeclared;
                    auto plan = Runner::MakePlan(listing, inputs);
                    if (!plan)
                    {
                        PrintError(plan.GetError().Message());
                        return Catch::UnspecifiedErrorExitCode;
                    }
                    matched.clear();
                    for (const auto &test : plan->tests)
                    {
                        matched.push_back(test.entry->index);
                    }
                    std::sort(matched.begin(), matched.end());
                }
                return List(options, *catalog, matched, programName);
            }

            const std::filesystem::path historyFile
                = Runner::HistoryFile(HistoryDirectory(options, program),
                                      programName);
            Runner::History history;
            if (options.historyEnabled
                || options.mode == Runner::Mode::kAnalytics)
            {
                auto loaded = Runner::History::Load(historyFile);
                if (loaded)
                {
                    history = std::move(*loaded);
                }
                else
                {
                    notes.push_back("the history could not be read: "
                                    + loaded.GetError().Message());
                }
            }
            if (options.mode == Runner::Mode::kAnalytics)
            {
                return Analytics(options, history, historyFile);
            }

            std::string binaryFingerprint;
            if (options.historyEnabled || options.incremental)
            {
                auto digest = Runner::Sha256File(program);
                binaryFingerprint = digest ? *digest : std::string{};
            }

            std::optional<Runner::Changes> changes;
            const auto readChanges = [&] {
                if (changes)
                {
                    return;
                }
                auto read = Runner::ReadChanges(SourceDirectory(*catalog),
                                                options.changesSince);
                if (read)
                {
                    changes = std::move(*read);
                }
                else if (options.selection == Runner::Selection::kChanged
                         || options.selection == Runner::Selection::kSmart)
                {
                    notes.push_back(read.GetError().Message());
                }
            };
            if (options.selection == Runner::Selection::kChanged
                || options.selection == Runner::Selection::kSmart)
            {
                readChanges();
            }

            Runner::PlanInputs inputs;
            inputs.catalog = &*catalog;
            inputs.matched = matched;
            inputs.unmatchedSpecs = unmatched;
            inputs.history = options.historyEnabled ? &history : nullptr;
            inputs.changes = changes ? &*changes : nullptr;
            inputs.binaryFingerprint = binaryFingerprint;
            auto plan = Runner::MakePlan(options, inputs);
            if (!plan)
            {
                PrintError(plan.GetError().Message());
                return Catch::UnspecifiedErrorExitCode;
            }
            notes.insert(notes.end(), plan->notes.begin(), plan->notes.end());

            Runner::ConsoleReport console{ options, stdout };
            const std::size_t workers
                = options.inProcess
                      ? 1
                      : std::max<std::size_t>(
                            1,
                            std::min<std::size_t>(
                                options.jobs == 0 ? Runner::HardwareThreads()
                                                  : options.jobs,
                                plan->tests.size()));
            console.RunStarting(plan->tests.size(),
                                workers,
                                options.seed,
                                notes);

            std::string guard = options.exitGuardPath;
            if (BazelTest() && guard.empty())
            {
                guard = Runner::ReadEnvironment("TEST_PREMATURE_EXIT_FILE");
            }
            if (!guard.empty())
            {
                if (auto status = Runner::CreatePrivateFile(guard); !status)
                {
                    PrintError("the exit guard file: "
                               + status.GetError().Message());
                    return Catch::UnspecifiedErrorExitCode;
                }
            }

            const auto started = std::chrono::steady_clock::now();
            RunResult run;
            run.binary = programName;
            run.seed = options.seed;
            run.shardIndex = options.shard.index;
            run.shardCount = options.shard.count;
            run.candidates = plan->candidates;
            run.unmatchedSpecs = plan->unmatchedSpecs;
            run.startUnixMilliseconds = UnixMilliseconds();
            Runner::SchedulerEvents events;
            events.testFinished = [&](const TestResult &test) {
                console.TestFinished(test);
            };
            events.retrying
                = [&](const TestResult &test, const Attempt &attempt) {
                      console.Retrying(test, attempt);
                  };
            auto outcome = Runner::R