diff options
| author | Leitwolf11 | 2026-10-23 23:25:41 +0300 |
|---|---|---|
| committer | Leitwolf11 | 2026-10-23 23:25:41 +0300 |
| commit | 93d4e7dbebab867e0a7f59fc25308460a7a76280 (patch) | |
| tree | 54c568a3063d49e21f039b6d38f40f51be7991f0 /src/Progmasoft/Runner/Catalog.cpp | |
| parent | 83c6fc3ac13abe83fa39f7b5f8559ccaf4412233 (diff) | |
| download | catch3-93d4e7dbebab867e0a7f59fc25308460a7a76280.tar.gz | |
Replace Catch2's runner with the Catch3 runner
Catch3 keeps Catch2's API for writing tests (test cases, sections,
assertions, matchers, generators, listeners, benchmarks) and replaces its
session, command line and reporters with a runner of its own.
The runner starts the test binary again as worker processes that take the
next test when they are free. A crash, an exit or a hang ends one test, not
the run. On top of that it adds:
- retries that tell deterministic, flaky and order-dependent failures
apart, and a bisection that names the test leaving state behind;
- a history of verdicts and durations, kept under a file lock, that drives
automatic quarantine, adaptive time limits, predictive ordering,
incremental runs, failure replay and analytics;
- selection by change, past failure or prediction, dependency closure,
and sharding by a stable hash or by recorded durations;
- tags for serial and isolated tests, shared resources, dependencies,
time limits and retries;
- a failure category, a signature, an analysis and a replay command for
every failure;
- console, JUnit XML (with flakyFailure and rerunFailure), JSON and Chrome
trace reports, and a versioned discovery listing (--list=json);
- a temporary directory per test (TMPDIR, TMP, TEMP and
TestTemporaryDirectory());
- Bazel's test environment: XML output file, test filter, sharding, seed,
premature exit file and undeclared outputs.
The command line is parsed by a hand-written, typed parser. Catch::Session
remains as a thin facade over the runner for custom main functions.
The result model in Progmasoft/Catch3/Results.hpp was rewritten around
tests and attempts, and the JUnit writer now writes it. The property
engine's example database is updated under a file lock with an atomic
rename, so parallel workers can write it at the same time.
Removed: Clara, the command-line and reporter-spec parsers, every reporter
and CATCH_REGISTER_REPORTER, listing, text flow, console colour and width,
the JSON and XML writers, sharding in the test registry, approval tests and
their baselines, Conan packaging, and the unused third_party/clara.hpp.
Listeners keep working; reporters' list and skip events are gone.
The Catch2 library is now compiled as C++20; code that includes only
Catch2's headers still needs C++14. Duplicate test cases are still refused
at start-up. Version 0.3.0.
ExtraTests now turn exceptions off for clang-cl with MSVC-style options,
which -fno-exceptions did not do.
Diffstat (limited to 'src/Progmasoft/Runner/Catalog.cpp')
| -rw-r--r-- | src/Progmasoft/Runner/Catalog.cpp | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/src/Progmasoft/Runner/Catalog.cpp b/src/Progmasoft/Runner/Catalog.cpp new file mode 100644 index 00000000..c34e81e0 --- /dev/null +++ b/src/Progmasoft/Runner/Catalog.cpp @@ -0,0 +1,272 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1 +#include <algorithm> +#include <catch2/catch_test_case_info.hpp> +#include <catch2/catch_test_spec.hpp> +#include <catch2/interfaces/catch_interfaces_config.hpp> +#include <catch2/interfaces/catch_interfaces_registry_hub.hpp> +#include <catch2/interfaces/catch_interfaces_testcase.hpp> +#include <catch2/internal/catch_test_case_registry_impl.hpp> +#include <cmath> +#include <iterator> +#include <map> + +#include "Catalog.hpp" +#include "Hash.hpp" + +namespace Progmasoft::Catch3::Runner +{ + namespace + { + std::optional<double> + ParseSeconds(std::string_view text) + { + if (text.empty()) + { + return std::nullopt; + } + double value = 0.0; + double scale = 0.0; + for (const char character : text) + { + if (character == '.' && scale <= 0.0) + { + scale = 1.0; + continue; + } + if (character < '0' || character > '9') + { + return std::nullopt; + } + if (scale > 0.0) + { + scale /= 10.0; + value += (character - '0') * scale; + } + else + { + value = value * 10.0 + (character - '0'); + } + } + return value; + } + + bool + HasPrefix(std::string_view text, std::string_view prefix) + { + return text.substr(0, prefix.size()) == prefix; + } + + Status + ApplyRunnerTag(TestEntry &entry, const std::string &tag) + { + const auto where = [&] { + return "test '" + entry.name + "' at " + entry.file + ":" + + std::to_string(entry.line); + }; + if (tag == "!serial") + { + entry.serial = true; + } + else if (tag == "!isolated") + { + entry.isolated = true; + } + else if (tag == "!quarantine") + { + entry.quarantined = true; + } + else if (HasPrefix(tag, "!resource:")) + { + entry.resources.push_back(tag.substr(10)); + } + else if (HasPrefix(tag, "!depends:")) + { + entry.dependsOn.push_back(tag.substr(9)); + } + else if (HasPrefix(tag, "!timeout:")) + { + const auto seconds = ParseSeconds(tag.substr(9)); + if (!seconds || *seconds > 1e7) + { + return Error{ "[" + tag + "] of " + where() + + " needs a number of seconds, such as " + "[!timeout:30]" }; + } + entry.timeout = std::chrono::milliseconds{ + static_cast<long long>(std::llround(*seconds * 1000.0)) + }; + } + else if (HasPrefix(tag, "!retries:")) + { + const auto count = ParseSeconds(tag.substr(9)); + if (!count || *count > 100 || std::floor(*count) < *count) + { + return Error{ "[" + tag + "] of " + where() + + " needs a whole number up to 100, such as " + "[!retries:3]" }; + } + entry.retries = static_cast<unsigned>(*count); + } + return Status::Ok(); + } + } // namespace + + Expected<Catalog> + Catalog::Load() + { + Catalog catalog; + const auto &handles + = Catch::getRegistryHub().getTestCaseRegistry().getAllTests(); + catalog.handles_ = handles; + // Two tests with the same class, name and tags cannot be told apart + // by any filter, history record or report, so they are refused. + std::vector<const Catch::TestCaseInfo *> sorted; + sorted.reserve(handles.size()); + for (const auto &handle : handles) + { + sorted.push_back(&handle.getTestCaseInfo()); + } + std::stable_sort(sorted.begin(), + sorted.end(), + [](const auto *lhs, const auto *rhs) { + return *lhs < *rhs; + }); + const auto duplicate + = std::adjacent_find(sorted.begin(), + sorted.end(), + [](const auto *lhs, const auto *rhs) { + return *lhs == *rhs; + }); + if (duplicate != sorted.end()) + { + const auto *first = *duplicate; + const auto *again = *std::next(duplicate); + return Error{ "test case \"" + again->name + "\", with tags \"" + + again->tagsAsString() + "\" already defined.\n" + + "\tFirst seen at " + + std::string{ first->lineInfo.file } + ":" + + std::to_string(first->lineInfo.line) + "\n" + + "\tRedefined at " + + std::string{ again->lineInfo.file } + ":" + + std::to_string(again->lineInfo.line) }; + } + std::map<std::string, std::size_t> nameCounts; + for (const auto &handle : handles) + { + const auto &info = handle.getTestCaseInfo(); + const std::string className{ info.className.data(), + info.className.size() }; + ++nameCounts[className.empty() ? info.name + : className + "::" + info.name]; + } + Sha256 identity; + catalog.tests_.reserve(handles.size()); + for (std::size_t index = 0; index < handles.size(); ++index) + { + const auto &info = handles[index].getTestCaseInfo(); + TestEntry entry; + entry.index = index; + entry.name = info.name; + entry.className + = std::string{ info.className.data(), info.className.size() }; + entry.file = std::string{ info.lineInfo.file }; + entry.line = info.lineInfo.line; + entry.hidden = info.isHidden(); + entry.mayFail = info.okToFail() && !info.expectedToFail(); + entry.shouldFail = info.expectedToFail(); + for (const auto &tag : info.tags) + { + entry.tags.emplace_back(tag.original.data(), + tag.original.size()); + } + entry.benchmark + = std::find(entry.tags.begin(), entry.tags.end(), "!benchmark") + != entry.tags.end(); + for (const auto &tag : entry.tags) + { + if (!tag.empty() && tag.front() == '!') + { + if (auto status = ApplyRunnerTag(entry, tag); !status) + { + return status.GetError(); + } + } + } + const std::string base = entry.className.empty() + ? entry.name + : entry.className + "::" + entry.name; + entry.key = base; + if (nameCounts[base] > 1) + { + entry.key += " " + info.tagsAsString(); + } + identity.Update(entry.key); + identity.Update("\n"); + catalog.tests_.push_back(std::move(entry)); + } + catalog.identity_ = identity.HexDigest(); + return catalog; + } + + const TestEntry * + Catalog::Find(const std::string &keyOrName) const + { + for (const auto &entry : tests_) + { + if (entry.key == keyOrName) + { + return &entry; + } + } + for (const auto &entry : tests_) + { + if (entry.name == keyOrName) + { + return &entry; + } + } + return nullptr; + } + + std::vector<std::size_t> + Catalog::Match(const Catch::IConfig &config, + std::vector<std::string> &unmatched) const + { + std::vector<std::size_t> selected; + const auto &spec = config.testSpec(); + if (!spec.hasFilters()) + { + for (const auto &entry : tests_) + { + if (!entry.hidden + && Catch::isThrowSafe(handles_[entry.index], config)) + { + selected.push_back(entry.index); + } + } + return selected; + } + std::vector<bool> chosen(tests_.size(), false); + for (const auto &match : spec.matchesByFilter(handles_, config)) + { + if (match.tests.empty()) + { + unmatched.push_back(match.name); + } + for (const auto *handle : match.tests) + { + chosen[static_cast<std::size_t>(handle - handles_.data())] + = true; + } + } + for (std::size_t index = 0; index < chosen.size(); ++index) + { + if (chosen[index]) + { + selected.push_back(index); + } + } + return selected; + } +} // namespace Progmasoft::Catch3::Runner |