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/Files.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/Files.cpp')
| -rw-r--r-- | src/Progmasoft/Runner/Files.cpp | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/src/Progmasoft/Runner/Files.cpp b/src/Progmasoft/Runner/Files.cpp new file mode 100644 index 00000000..68bde537 --- /dev/null +++ b/src/Progmasoft/Runner/Files.cpp @@ -0,0 +1,205 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1 +#include "Files.hpp" + +#include "Hash.hpp" +#include "Process.hpp" + +#if defined(_WIN32) +# include <fcntl.h> +# include <io.h> +# include <share.h> +# include <sys/stat.h> +#else +# include <fcntl.h> +# include <sys/stat.h> +# include <unistd.h> +#endif + +#include <atomic> +#include <cstdio> +#include <fstream> +#include <sstream> +#include <system_error> +#include <thread> + +namespace Progmasoft::Catch3::Runner +{ + namespace + { + /// Creates `path` only if it does not exist, in one atomic step. + bool + CreateExclusively(const std::filesystem::path &path) + { +#if defined(_WIN32) + int descriptor = -1; + if (_wsopen_s(&descriptor, + path.wstring().c_str(), + _O_CREAT | _O_EXCL | _O_WRONLY, + _SH_DENYNO, + _S_IREAD | _S_IWRITE) != 0) + { + return false; + } + _close(descriptor); + return true; +#else + const int descriptor = open(path.c_str(), + O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, + S_IRUSR | S_IWUSR); + if (descriptor < 0) + { + return false; + } + close(descriptor); + return true; +#endif + } + } // namespace + + FileLock::FileLock(const std::filesystem::path &file, + std::chrono::milliseconds wait, + std::chrono::milliseconds stale) + : path_{file.string() + ".lock"} + { + std::error_code error; + std::filesystem::create_directories(path_.parent_path(), error); + const auto deadline = std::chrono::steady_clock::now() + wait; + while (true) + { + if (CreateExclusively(path_)) + { + held_ = true; + return; + } + const auto written = std::filesystem::last_write_time(path_, error); + if (!error && + std::filesystem::file_time_type::clock::now() - written > stale) + { + std::filesystem::remove(path_, error); + continue; + } + if (std::chrono::steady_clock::now() >= deadline) + { + return; + } + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } + + FileLock::~FileLock() + { + if (held_) + { + std::error_code error; + std::filesystem::remove(path_, error); + } + } + + Expected<std::string> + ReadFile(const std::filesystem::path &file) + { + std::ifstream input(file, std::ios::binary); + if (!input) + { + return Error{"cannot read '" + file.string() + "'"}; + } + std::ostringstream contents; + contents << input.rdbuf(); + if (input.bad()) + { + return Error{"cannot read '" + file.string() + "'"}; + } + return contents.str(); + } + + Status + WriteFileAtomically(const std::filesystem::path &file, + std::string_view contents) + { + std::error_code error; + if (file.has_parent_path()) + { + std::filesystem::create_directories(file.parent_path(), error); + } + const auto temporary = + std::filesystem::path{file.string() + "." + UniqueName() + ".tmp"}; + { + std::ofstream output(temporary, std::ios::binary | std::ios::trunc); + output.write(contents.data(), + static_cast<std::streamsize>(contents.size())); + if (!output) + { + output.close(); + std::filesystem::remove(temporary, error); + return Error{"cannot write '" + file.string() + "'"}; + } + } + std::filesystem::rename(temporary, file, error); + if (error) + { + std::error_code ignored; + std::filesystem::remove(temporary, ignored); + return Error{"cannot replace '" + file.string() + + "': " + error.message()}; + } + return Status::Ok(); + } + + Status + CreatePrivateFile(const std::filesystem::path &file) + { +#if defined(_WIN32) + std::error_code error; + const auto status = std::filesystem::symlink_status(file, error); + if (!error && std::filesystem::exists(status) && + !std::filesystem::is_regular_file(status)) + { + return Error{"'" + file.string() + "' is not a regular file"}; + } + int descriptor = -1; + if (_wsopen_s(&descriptor, + file.wstring().c_str(), + _O_CREAT | _O_TRUNC | _O_WRONLY | _O_NOINHERIT, + _SH_DENYNO, + _S_IREAD | _S_IWRITE) != 0) + { + return Error{"cannot create '" + file.string() + "'"}; + } + _close(descriptor); + return Status::Ok(); +#else + int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK; +# if defined(O_NOFOLLOW) + flags |= O_NOFOLLOW; +# endif + const int descriptor = open(file.c_str(), flags, S_IRUSR | S_IWUSR); + if (descriptor < 0) + { + return Error{"cannot create '" + file.string() + "'"}; + } + struct stat information = {}; + if (fstat(descriptor, &information) != 0 || + !S_ISREG(information.st_mode)) + { + close(descriptor); + return Error{"'" + file.string() + "' is not a regular file"}; + } + // A file that existed keeps its mode; make it private too. + fchmod(descriptor, S_IRUSR | S_IWUSR); + close(descriptor); + return Status::Ok(); +#endif + } + + std::string + UniqueName() + { + static std::atomic<unsigned> counter{0}; + std::ostringstream text; + text << CurrentProcessId() << '-' + << std::chrono::steady_clock::now().time_since_epoch().count() + << '-' << std::this_thread::get_id() << '-' << ++counter; + return ToHex(Fnv1a64(text.str())); + } +} // namespace Progmasoft::Catch3::Runner |