918 lines
29 KiB
C++
918 lines
29 KiB
C++
#include <Void/Testing/Runner.hpp>
|
|
#include "Void/Testing/Config.hpp"
|
|
#include <Void/Detail/Enum.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
#include <Void/Testing/Detail/Color.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
|
|
#ifdef VOID_HAS_STACKTRACE
|
|
#include <Void/Stacktrace/Stacktrace.hpp>
|
|
#endif
|
|
#include <Void/Testing/ConsoleReporter.hpp>
|
|
#include <Void/Testing/Detail/Registry.hpp>
|
|
#include <Void/Testing/Detail/SignalHandler.hpp>
|
|
#include <Void/Testing/JUnitReporter.hpp>
|
|
#include <Void/Time/Format.hpp>
|
|
|
|
#ifdef VOID_HAS_ASYNC
|
|
#include <Void/Async/ThreadPool.hpp>
|
|
#include <future>
|
|
#endif
|
|
|
|
#ifdef VOID_HAS_CONFIGURATION
|
|
#include <Void/Configuration/Env.hpp>
|
|
#else
|
|
#include <cstdlib>
|
|
#endif
|
|
|
|
#ifdef VOID_HAS_LOG
|
|
#include <Void/Log/LoggingAdapter.hpp>
|
|
#include <Void/Log/ConsoleLogger.hpp>
|
|
#include <Void/Log/FileLogger.hpp>
|
|
#include <Void/Log/Level.hpp>
|
|
#endif
|
|
|
|
#ifdef VOID_HAS_TEXT
|
|
#include <Void/Text/Str.hpp>
|
|
#endif
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <csignal>
|
|
#include <cstdlib>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <random>
|
|
#include <regex>
|
|
#include <thread>
|
|
#include <unordered_map>
|
|
|
|
namespace Void::Testing
|
|
{
|
|
|
|
namespace
|
|
{
|
|
class OwningJUnitReporter final : public Reporter
|
|
{
|
|
public:
|
|
OwningJUnitReporter(std::unique_ptr<std::ofstream> stream,
|
|
std::string* write_err_out)
|
|
: stream_(std::move(stream))
|
|
, inner_(*stream_)
|
|
, write_err_out_(write_err_out)
|
|
{
|
|
}
|
|
|
|
void on_run_start(const RunConfig& c, int n) override { inner_.on_run_start(c, n); }
|
|
void on_suite_start(const std::string& s, int n) override { inner_.on_suite_start(s, n); }
|
|
void on_test_start(const TestCase& t) override { inner_.on_test_start(t); }
|
|
void on_test_end(const TestResult& r) override { inner_.on_test_end(r); }
|
|
void on_suite_end(const SuiteResult& s) override { inner_.on_suite_end(s); }
|
|
void on_run_end(const RunResult& r) override
|
|
{
|
|
inner_.on_run_end(r);
|
|
if (stream_) {
|
|
stream_->flush();
|
|
if (!stream_->good() && write_err_out_ != nullptr &&
|
|
write_err_out_->empty()) {
|
|
*write_err_out_ = "Failed while writing JUnit report";
|
|
}
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::unique_ptr<std::ofstream> stream_;
|
|
JUnitReporter inner_;
|
|
std::string* write_err_out_{nullptr};
|
|
};
|
|
|
|
struct ContextRestorer {
|
|
Detail::TestContext saved;
|
|
ContextRestorer() : saved(Detail::TestContext::current()) {}
|
|
~ContextRestorer() { Detail::TestContext::current() = saved; }
|
|
ContextRestorer(const ContextRestorer&) = delete;
|
|
ContextRestorer& operator=(const ContextRestorer&) = delete;
|
|
};
|
|
|
|
[[nodiscard]] bool parse_unsigned_strict(const std::string& text,
|
|
unsigned& out) noexcept
|
|
{
|
|
if (text.empty() || text[0] == '-') {
|
|
return false;
|
|
}
|
|
try {
|
|
std::size_t pos = 0;
|
|
const unsigned long val = std::stoul(text, &pos, 10);
|
|
if (pos != text.size() || val > std::numeric_limits<unsigned>::max()) {
|
|
return false;
|
|
}
|
|
out = static_cast<unsigned>(val);
|
|
return true;
|
|
} catch (...) {
|
|
return false;
|
|
}
|
|
}
|
|
// Failures are counted when recorded (TestResult::failed_assertions),
|
|
// independently of the bounded assertion vector kept for reporting.
|
|
[[nodiscard]] bool has_recorded_failure(const TestResult& result) noexcept
|
|
{
|
|
return result.failed_assertions > 0;
|
|
}
|
|
} // namespace
|
|
|
|
RunConfig Runner::parse_args(int argc, char** argv)
|
|
{
|
|
RunConfig config;
|
|
auto need_val = [&](const std::string& flag, int& i, std::string& out) -> bool {
|
|
if (i + 1 >= argc) {
|
|
if (config.parse_error.empty()) {
|
|
config.parse_error = std::format("Missing value for option {}", flag);
|
|
}
|
|
return false;
|
|
}
|
|
out = argv[++i];
|
|
return true;
|
|
};
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
std::string arg = argv[i];
|
|
if (arg == "--filter") {
|
|
(void)need_val(arg, i, config.filter_pattern);
|
|
} else if (arg == "--suite") {
|
|
(void)need_val(arg, i, config.suite_filter);
|
|
} else if (arg == "--reporter") {
|
|
(void)need_val(arg, i, config.reporter);
|
|
} else if (arg == "--output") {
|
|
(void)need_val(arg, i, config.output_file);
|
|
} else if (arg == "--tag") {
|
|
(void)need_val(arg, i, config.tag_filter);
|
|
} else if (arg == "--log-file") {
|
|
(void)need_val(arg, i, config.log_file);
|
|
} else if (arg == "--selftest-child") {
|
|
(void)need_val(arg, i, config.selftest_child);
|
|
} else if (arg == "--seed") {
|
|
std::string val;
|
|
if (need_val(arg, i, val)) {
|
|
unsigned parsed = 0;
|
|
if (!parse_unsigned_strict(val, parsed)) {
|
|
config.parse_error = std::format("Invalid --seed value: {}", val);
|
|
} else {
|
|
config.shuffle = true;
|
|
config.random_seed = parsed;
|
|
}
|
|
}
|
|
} else if (arg == "--repeat") {
|
|
std::string val;
|
|
if (need_val(arg, i, val)) {
|
|
unsigned parsed = 0;
|
|
if (!parse_unsigned_strict(val, parsed)) {
|
|
config.parse_error = std::format("Invalid --repeat value: {}", val);
|
|
} else {
|
|
config.repeat = parsed;
|
|
}
|
|
}
|
|
} else if (arg == "--parallel") {
|
|
std::string val;
|
|
if (need_val(arg, i, val)) {
|
|
unsigned parsed = 0;
|
|
if (!parse_unsigned_strict(val, parsed)) {
|
|
config.parse_error = std::format("Invalid --parallel value: {}", val);
|
|
} else {
|
|
config.parallel_threads = parsed;
|
|
}
|
|
}
|
|
} else if (arg == "--help") {
|
|
config.show_help = true;
|
|
} else if (arg == "--list") {
|
|
config.list_only = true;
|
|
} else if (arg == "--verbose") {
|
|
config.verbose = true;
|
|
} else if (arg == "--no-color") {
|
|
config.no_color = true;
|
|
} else if (arg == "--allow-empty") {
|
|
config.allow_empty = true;
|
|
} else if (arg == "--shuffle") {
|
|
config.shuffle = true;
|
|
std::random_device rd;
|
|
config.random_seed = rd();
|
|
} else if (!arg.empty() && arg[0] == '-') {
|
|
if (config.parse_error.empty()) {
|
|
config.parse_error = std::format("Unknown option: {}", arg);
|
|
}
|
|
}
|
|
}
|
|
return config;
|
|
}
|
|
|
|
std::vector<TestCase>
|
|
Runner::filter_tests(const std::vector<TestCase>& tests,
|
|
const RunConfig& config)
|
|
{
|
|
std::vector<TestCase> result;
|
|
std::regex filter_regex{".*"};
|
|
if (!config.filter_pattern.empty()) {
|
|
try {
|
|
filter_regex = std::regex{config.filter_pattern};
|
|
} catch (...) {
|
|
return {};
|
|
}
|
|
}
|
|
for (const auto& test : tests) {
|
|
if (!config.suite_filter.empty() &&
|
|
test.suite != config.suite_filter) {
|
|
continue;
|
|
}
|
|
if (!std::regex_search(test.name, filter_regex) &&
|
|
!std::regex_search(test.suite + "." + test.name,
|
|
filter_regex)) {
|
|
continue;
|
|
}
|
|
if (!config.tag_filter.empty()) {
|
|
bool has = false;
|
|
for (const auto& t : test.tags) {
|
|
if (t == config.tag_filter) {
|
|
has = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!has) {
|
|
continue;
|
|
}
|
|
}
|
|
result.push_back(test);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::unique_ptr<Reporter> Runner::create_reporter(const RunConfig& config)
|
|
{
|
|
if (config.reporter == "junit") {
|
|
const std::string path =
|
|
config.output_file.empty() ? "test_results.xml" : config.output_file;
|
|
auto file = std::make_unique<std::ofstream>(path, std::ios::out | std::ios::trunc);
|
|
return std::make_unique<OwningJUnitReporter>(std::move(file), nullptr);
|
|
}
|
|
return std::make_unique<ConsoleReporter>(std::cout, !config.no_color);
|
|
}
|
|
|
|
TestResult Runner::execute_case(TestCase& test_case)
|
|
{
|
|
return execute_test(test_case);
|
|
}
|
|
|
|
TestResult Runner::execute_test(TestCase& test_case)
|
|
{
|
|
TestResult result;
|
|
result.name = test_case.name;
|
|
result.suite = test_case.suite;
|
|
result.tags = test_case.tags;
|
|
if (test_case.disabled) {
|
|
result.state = TestState::Skipped;
|
|
result.skip_reason = test_case.skip_reason.empty()
|
|
? "disabled"
|
|
: test_case.skip_reason;
|
|
return result;
|
|
}
|
|
ContextRestorer ctx_restorer;
|
|
Detail::TestContext::current().current_result = &result;
|
|
Detail::TestContext::current().should_abort = false;
|
|
|
|
int crash_signal = 0;
|
|
std::string crash_stacktrace;
|
|
Detail::install_signal_handlers(
|
|
[&](int sig, const std::string& trace) {
|
|
crash_signal = sig;
|
|
crash_stacktrace = trace;
|
|
});
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
try {
|
|
#ifndef _WIN32
|
|
if (sigsetjmp(Detail::get_jump_buffer(), 1) == 0) {
|
|
Detail::set_jump_ready(true);
|
|
run_test_body(test_case, result);
|
|
} else {
|
|
result.state = TestState::Crashed;
|
|
result.crash_info = std::format(
|
|
"Signal: {}", Detail::signal_name(crash_signal));
|
|
result.stacktrace = crash_stacktrace;
|
|
}
|
|
Detail::set_jump_ready(false);
|
|
#else
|
|
run_test_body(test_case, result);
|
|
#endif
|
|
} catch (const Detail::RequireFailure&) {
|
|
if (result.state != TestState::Skipped) {
|
|
result.state = TestState::Failed;
|
|
}
|
|
} catch (const std::exception& e) {
|
|
result.state = TestState::Failed;
|
|
result.crash_info =
|
|
std::format("Unhandled exception: {}", e.what());
|
|
#ifdef VOID_HAS_STACKTRACE
|
|
result.stacktrace = Void::Stacktrace::format_stacktrace(
|
|
Void::Stacktrace::capture_stacktrace(1, 8));
|
|
#endif
|
|
} catch (...) {
|
|
result.state = TestState::Failed;
|
|
result.crash_info = "Unhandled unknown exception";
|
|
#ifdef VOID_HAS_STACKTRACE
|
|
result.stacktrace = Void::Stacktrace::format_stacktrace(
|
|
Void::Stacktrace::capture_stacktrace(1, 8));
|
|
#endif
|
|
}
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
result.duration = end - start;
|
|
Detail::uninstall_signal_handlers();
|
|
|
|
if (result.state != TestState::Skipped &&
|
|
result.state != TestState::Crashed) {
|
|
const bool any = has_recorded_failure(result);
|
|
// An exception that escaped the body fails it too, with no failed
|
|
// assertion recorded.
|
|
const bool unhandled = !result.crash_info.empty() || !result.cleanup_error.empty();
|
|
result.state =
|
|
any || unhandled ? TestState::Failed : TestState::Passed;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Like execute_test() but without sigsetjmp/signal-handler installation,
|
|
// safe to call from multiple threads simultaneously.
|
|
TestResult Runner::execute_test_parallel(TestCase& test_case)
|
|
{
|
|
TestResult result;
|
|
result.name = test_case.name;
|
|
result.suite = test_case.suite;
|
|
result.tags = test_case.tags;
|
|
if (test_case.disabled) {
|
|
result.state = TestState::Skipped;
|
|
result.skip_reason = test_case.skip_reason.empty()
|
|
? "disabled"
|
|
: test_case.skip_reason;
|
|
return result;
|
|
}
|
|
ContextRestorer ctx_restorer;
|
|
Detail::TestContext::current().current_result = &result;
|
|
Detail::TestContext::current().should_abort = false;
|
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
try {
|
|
run_test_body(test_case, result);
|
|
} catch (const Detail::RequireFailure&) {
|
|
if (result.state != TestState::Skipped) {
|
|
result.state = TestState::Failed;
|
|
}
|
|
} catch (const std::exception& e) {
|
|
result.state = TestState::Failed;
|
|
result.crash_info =
|
|
std::format("Unhandled exception: {}", e.what());
|
|
} catch (...) {
|
|
result.state = TestState::Failed;
|
|
result.crash_info = "Unhandled unknown exception";
|
|
}
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
result.duration = end - start;
|
|
|
|
if (result.state != TestState::Skipped &&
|
|
result.state != TestState::Crashed) {
|
|
const bool any = has_recorded_failure(result);
|
|
// An exception that escaped the body fails it too, with no failed
|
|
// assertion recorded.
|
|
const bool unhandled = !result.crash_info.empty() || !result.cleanup_error.empty();
|
|
result.state =
|
|
any || unhandled ? TestState::Failed : TestState::Passed;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void Runner::run_test_body(TestCase& test_case, TestResult& result)
|
|
{
|
|
if (test_case.fixture_create) {
|
|
void* fixture = test_case.fixture_create();
|
|
Detail::TestContext::current().fixture = fixture;
|
|
bool teardown_attempted = false;
|
|
auto run_teardown_once = [&]() {
|
|
if (teardown_attempted) {
|
|
return;
|
|
}
|
|
teardown_attempted = true;
|
|
try {
|
|
if (test_case.fixture_teardown) {
|
|
test_case.fixture_teardown(fixture);
|
|
}
|
|
} catch (const std::exception& e) {
|
|
result.cleanup_error =
|
|
std::format("Fixture teardown error: {}", e.what());
|
|
if (result.state == TestState::Passed) {
|
|
result.state = TestState::Failed;
|
|
}
|
|
} catch (...) {
|
|
result.cleanup_error = "Fixture teardown unknown exception";
|
|
if (result.state == TestState::Passed) {
|
|
result.state = TestState::Failed;
|
|
}
|
|
}
|
|
};
|
|
try {
|
|
if (test_case.fixture_setup) {
|
|
test_case.fixture_setup(fixture);
|
|
}
|
|
test_case.run();
|
|
run_teardown_once();
|
|
} catch (...) {
|
|
run_teardown_once();
|
|
if (test_case.fixture_destroy) {
|
|
test_case.fixture_destroy(fixture);
|
|
}
|
|
Detail::TestContext::current().fixture = nullptr;
|
|
throw;
|
|
}
|
|
if (test_case.fixture_destroy) {
|
|
test_case.fixture_destroy(fixture);
|
|
}
|
|
Detail::TestContext::current().fixture = nullptr;
|
|
} else {
|
|
test_case.run();
|
|
}
|
|
}
|
|
|
|
RunResult Runner::run(const RunConfig& config)
|
|
{
|
|
Void::Testing::Detail::Color::g_color_enabled = !config.no_color;
|
|
if (config.show_help) {
|
|
using namespace Void::Testing::Detail::Color;
|
|
|
|
std::cout
|
|
<< "\n"
|
|
<< Void::Testing::Detail::Color::apply(bold_blue,
|
|
"\xe2\x95\xad\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x95\xae")
|
|
<< "\n"
|
|
<< Void::Testing::Detail::Color::apply(bold_blue, "\xe2\x94\x82")
|
|
<< Void::Testing::Detail::Color::apply(bold_white, " Void Testing")
|
|
<< Void::Testing::Detail::Color::apply(dim, " \xe2\x80\x94 Command Line Interface")
|
|
<< std::string(28, ' ') << Void::Testing::Detail::Color::apply(bold_blue, "\xe2\x94\x82")
|
|
<< "\n"
|
|
<< Void::Testing::Detail::Color::apply(bold_blue,
|
|
"\xe2\x95\xb0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x95\xaf")
|
|
<< "\n"
|
|
<< "\n"
|
|
<< " " << Void::Testing::Detail::Color::apply(bold_white, "USAGE:") << "\n"
|
|
<< " " << Void::Testing::Detail::Color::apply(cyan, "<executable>") << " "
|
|
<< Void::Testing::Detail::Color::apply(dim, "[options]") << "\n"
|
|
<< "\n"
|
|
<< " " << Void::Testing::Detail::Color::apply(bold_white, "OPTIONS:") << "\n";
|
|
|
|
constexpr std::size_t OPT_WIDTH = 23;
|
|
auto opt = [&](std::string_view name) {
|
|
#ifdef VOID_HAS_TEXT
|
|
return Void::Testing::Detail::Color::apply(green, Void::Str::pad_right(name, OPT_WIDTH));
|
|
#else
|
|
std::string s{name};
|
|
if (s.size() < OPT_WIDTH) {
|
|
s.append(OPT_WIDTH - s.size(), ' ');
|
|
}
|
|
return Void::Testing::Detail::Color::apply(green, s);
|
|
#endif
|
|
};
|
|
|
|
std::cout
|
|
<< " " << opt("--filter <pattern>")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Run tests matching the regex pattern") << "\n"
|
|
<< " " << opt("--suite <name>")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Run only tests in the specified suite") << "\n"
|
|
<< " " << opt("--tag <name>")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Run only tests with the specified tag") << "\n"
|
|
<< " " << opt("--list")
|
|
<< Void::Testing::Detail::Color::apply(dim, "List discovered tests without running them")
|
|
<< "\n"
|
|
<< " " << opt("--shuffle")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Randomize the order of test execution") << "\n"
|
|
<< " " << opt("--seed <n>")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Set the random seed (implies --shuffle)") << "\n"
|
|
<< " " << opt("--repeat <n>")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Repeat the test run n times") << "\n"
|
|
<< " " << opt("--reporter <type>")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Choose the output reporter: console, junit")
|
|
<< "\n"
|
|
<< " " << opt("--output <file>")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Write reporter output to a file") << "\n"
|
|
#ifdef VOID_HAS_LOG
|
|
<< " " << opt("--log-file <file>")
|
|
<< Void::Testing::Detail::Color::apply(dim,
|
|
"Mirror test events to <file> via Void::Logging")
|
|
<< "\n"
|
|
#endif
|
|
#ifdef VOID_HAS_ASYNC
|
|
<< " " << opt("--parallel <n>")
|
|
<< Void::Testing::Detail::Color::apply(dim,
|
|
"Run tests across n threads (no crash detection)")
|
|
<< "\n"
|
|
#endif
|
|
<< " " << opt("--verbose")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Enable verbose output") << "\n"
|
|
<< " " << opt("--no-color")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Disable colored console output") << "\n"
|
|
<< " " << opt("--allow-empty")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Do not fail when selection matches zero tests") << "\n"
|
|
<< " " << opt("--help")
|
|
<< Void::Testing::Detail::Color::apply(dim, "Show this help message") << "\n"
|
|
<< "\n"
|
|
<< " " << Void::Testing::Detail::Color::apply(bold_white, "EXAMPLES:") << "\n"
|
|
<< " " << Void::Testing::Detail::Color::apply(cyan, "./run_tests") << " "
|
|
<< Void::Testing::Detail::Color::apply(yellow, "--filter") << " \"math_tests.*\"" << "\n"
|
|
<< " " << Void::Testing::Detail::Color::apply(cyan, "./run_tests") << " "
|
|
<< Void::Testing::Detail::Color::apply(yellow, "--tag") << " integration "
|
|
<< Void::Testing::Detail::Color::apply(yellow, "--shuffle") << "\n"
|
|
<< " " << Void::Testing::Detail::Color::apply(cyan, "./run_tests") << " "
|
|
<< Void::Testing::Detail::Color::apply(yellow, "--reporter") << " junit "
|
|
<< Void::Testing::Detail::Color::apply(yellow, "--output") << " results.xml" << "\n"
|
|
<< "\n";
|
|
|
|
return {};
|
|
}
|
|
|
|
if (!config.parse_error.empty()) {
|
|
RunResult err_res;
|
|
err_res.config_error = config.parse_error;
|
|
std::cerr << "error: " << err_res.config_error << "\n";
|
|
return err_res;
|
|
}
|
|
if (config.reporter != "console" && config.reporter != "junit") {
|
|
RunResult err_res;
|
|
err_res.config_error =
|
|
std::format("Unknown reporter: {}", config.reporter);
|
|
std::cerr << "error: " << err_res.config_error << "\n";
|
|
return err_res;
|
|
}
|
|
if (config.repeat == 0) {
|
|
RunResult err_res;
|
|
err_res.config_error = "--repeat must be >= 1";
|
|
std::cerr << "error: " << err_res.config_error << "\n";
|
|
return err_res;
|
|
}
|
|
if (!config.filter_pattern.empty()) {
|
|
try {
|
|
(void)std::regex{config.filter_pattern};
|
|
} catch (const std::regex_error& e) {
|
|
RunResult err_res;
|
|
err_res.config_error = std::format(
|
|
"Invalid --filter regex '{}': {}", config.filter_pattern, e.what());
|
|
std::cerr << "error: " << err_res.config_error << "\n";
|
|
return err_res;
|
|
}
|
|
}
|
|
|
|
auto all_tests = Detail::Registry::instance().tests();
|
|
auto tests = filter_tests(all_tests, config);
|
|
const bool has_explicit_selection =
|
|
!config.filter_pattern.empty() ||
|
|
!config.suite_filter.empty() ||
|
|
!config.tag_filter.empty();
|
|
if (!config.list_only && has_explicit_selection &&
|
|
tests.empty() && !config.allow_empty) {
|
|
RunResult err_res;
|
|
err_res.config_error =
|
|
"Test selection matched 0 tests (pass --allow-empty to permit empty selection)";
|
|
std::cerr << "error: " << err_res.config_error << "\n";
|
|
return err_res;
|
|
}
|
|
|
|
std::string report_write_error;
|
|
std::unique_ptr<Reporter> reporter;
|
|
if (config.reporter == "junit") {
|
|
const std::string path =
|
|
config.output_file.empty() ? "test_results.xml" : config.output_file;
|
|
auto file = std::make_unique<std::ofstream>(
|
|
path, std::ios::out | std::ios::trunc);
|
|
if (!file->is_open()) {
|
|
RunResult err_res;
|
|
err_res.config_error =
|
|
std::format("Failed to open JUnit output file: {}", path);
|
|
std::cerr << "error: " << err_res.config_error << "\n";
|
|
return err_res;
|
|
}
|
|
reporter = std::make_unique<OwningJUnitReporter>(
|
|
std::move(file), &report_write_error);
|
|
} else {
|
|
reporter = create_reporter(config);
|
|
}
|
|
|
|
// ── Optional: file logger sink (requires Void::Logging) ──────────────────
|
|
#ifdef VOID_HAS_LOG
|
|
std::unique_ptr<Void::Logging::FileLogger> log_sink;
|
|
if (!config.log_file.empty()) {
|
|
try {
|
|
log_sink = std::make_unique<Void::Logging::FileLogger>(
|
|
config.log_file);
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "warning: --log-file failed: " << e.what()
|
|
<< "\n";
|
|
}
|
|
}
|
|
#else
|
|
if (!config.log_file.empty()) {
|
|
std::cerr
|
|
<< "warning: --log-file requires Void::Logging (not linked)\n";
|
|
}
|
|
#endif
|
|
|
|
// ── Optional: VOID_LOG_LEVEL auto-sink ───────────────────────────────────
|
|
#ifdef VOID_HAS_LOG
|
|
std::unique_ptr<Void::Logging::ConsoleLogger> env_log;
|
|
{
|
|
#ifdef VOID_HAS_CONFIGURATION
|
|
const auto lvl_str =
|
|
Void::Configuration::get_env_or("VOID_LOG_LEVEL");
|
|
#else
|
|
const char* raw_env =
|
|
std::getenv("VOID_LOG_LEVEL"); // NOLINT(concurrency-mt-unsafe)
|
|
const std::string lvl_str = raw_env ? raw_env : "";
|
|
#endif
|
|
if (!lvl_str.empty()) {
|
|
#ifdef VOID_HAS_ENUM_REFLECTION
|
|
auto level =
|
|
Void::Detail::enum_parse<Void::Logging::Level>(lvl_str)
|
|
.value_or(Void::Logging::Level::Info);
|
|
#else
|
|
Void::Logging::Level level = Void::Logging::Level::Info;
|
|
if (lvl_str == "Trace") level = Void::Logging::Level::Trace;
|
|
else if (lvl_str == "Debug") level = Void::Logging::Level::Debug;
|
|
else if (lvl_str == "Warning") level = Void::Logging::Level::Warning;
|
|
else if (lvl_str == "Error") level = Void::Logging::Level::Error;
|
|
else if (lvl_str == "Fatal") level = Void::Logging::Level::Fatal;
|
|
#endif
|
|
env_log =
|
|
std::make_unique<Void::Logging::ConsoleLogger>(std::cerr);
|
|
env_log->set_level(level);
|
|
Void::Log::set_sink(Void::Log::make_sink(*env_log));
|
|
}
|
|
}
|
|
#endif // VOID_HAS_LOG
|
|
|
|
if (config.list_only) {
|
|
for (const auto& t : tests) {
|
|
std::cout << t.suite << "." << t.name
|
|
<< (t.disabled ? " [DISABLED]" : "") << "\n";
|
|
}
|
|
return {};
|
|
}
|
|
|
|
RunResult run_result;
|
|
|
|
std::vector<SuiteResult> suite_results;
|
|
std::unordered_map<std::string, size_t> suite_index;
|
|
for (auto& test : tests) {
|
|
if (suite_index.find(test.suite) == suite_index.end()) {
|
|
suite_index[test.suite] = suite_results.size();
|
|
suite_results.push_back({.name = test.suite});
|
|
}
|
|
}
|
|
|
|
auto log_result = [&](const TestResult& r, const std::string& q) {
|
|
#ifdef VOID_HAS_LOG
|
|
if (!log_sink) {
|
|
return;
|
|
}
|
|
const auto ts = Void::Time::utc_now_iso8601_ms();
|
|
const auto dur = Void::Time::format_duration(r.duration);
|
|
switch (r.state) {
|
|
case TestState::Passed:
|
|
log_sink->info(std::format("[{}] test pass: {} ({})", ts,
|
|
q, dur));
|
|
break;
|
|
case TestState::Failed:
|
|
log_sink->error(std::format(
|
|
"[{}] test fail: {} ({}) \xe2\x80\x94 {}", ts, q, dur,
|
|
r.crash_info.empty() ? "assertion failed"
|
|
: r.crash_info));
|
|
break;
|
|
case TestState::Skipped:
|
|
log_sink->warning(std::format(
|
|
"[{}] test skip: {} \xe2\x80\x94 {}", ts, q,
|
|
r.skip_reason.empty() ? "no reason" : r.skip_reason));
|
|
break;
|
|
case TestState::Crashed:
|
|
log_sink->fatal(std::format(
|
|
"[{}] test crash: {} \xe2\x80\x94 {}", ts, q,
|
|
r.crash_info));
|
|
break;
|
|
}
|
|
#else
|
|
(void)r;
|
|
(void)q;
|
|
#endif
|
|
};
|
|
|
|
auto start_time = std::chrono::high_resolution_clock::now();
|
|
|
|
// ── Optional: parallel execution (requires Void::Concurrency) ────────────
|
|
bool run_parallel = config.parallel_threads > 0;
|
|
#ifndef VOID_HAS_ASYNC
|
|
if (run_parallel) {
|
|
std::cerr << "warning: --parallel requires Void::Concurrency "
|
|
"(not linked); running sequentially\n";
|
|
run_parallel = false;
|
|
}
|
|
#endif
|
|
|
|
if (run_parallel) {
|
|
#ifdef VOID_HAS_ASYNC
|
|
reporter->on_run_start(config, static_cast<int>(tests.size()));
|
|
#ifdef VOID_HAS_LOG
|
|
if (log_sink) {
|
|
log_sink->info(std::format(
|
|
"run start (parallel, {} threads): {} test(s)",
|
|
config.parallel_threads, tests.size()));
|
|
}
|
|
#endif
|
|
Void::Concurrency::ThreadPool pool{config.parallel_threads};
|
|
std::vector<std::future<TestResult>> futures;
|
|
futures.reserve(tests.size());
|
|
for (auto& test : tests) {
|
|
futures.push_back(pool.submit(
|
|
[this, &test] { return execute_test_parallel(test); }));
|
|
}
|
|
std::vector<TestResult> par_results;
|
|
par_results.reserve(tests.size());
|
|
for (auto& fut : futures) {
|
|
par_results.push_back(fut.get());
|
|
}
|
|
|
|
std::unordered_map<std::string, bool> suite_started;
|
|
for (std::size_t i = 0; i < tests.size(); ++i) {
|
|
auto& test = tests[i];
|
|
auto& result = par_results[i];
|
|
if (!suite_started[test.suite]) {
|
|
suite_started[test.suite] = true;
|
|
reporter->on_suite_start(
|
|
test.suite,
|
|
static_cast<int>(
|
|
std::count_if(tests.begin(), tests.end(),
|
|
[&](const auto& t) {
|
|
return t.suite == test.suite;
|
|
})));
|
|
}
|
|
reporter->on_test_start(test);
|
|
reporter->on_test_end(result);
|
|
log_result(result, std::format("{}.{}", result.suite,
|
|
result.name));
|
|
suite_results[suite_index[test.suite]].tests.push_back(
|
|
std::move(result));
|
|
}
|
|
#endif // VOID_HAS_ASYNC
|
|
} else {
|
|
reporter->on_run_start(config, static_cast<int>(tests.size()));
|
|
#ifdef VOID_HAS_LOG
|
|
if (log_sink) {
|
|
log_sink->info(std::format("run start: {} test(s) discovered",
|
|
tests.size()));
|
|
}
|
|
#endif
|
|
if (config.shuffle) {
|
|
std::shuffle(tests.begin(), tests.end(),
|
|
std::mt19937{config.random_seed});
|
|
#ifdef VOID_HAS_LOG
|
|
if (log_sink) {
|
|
log_sink->info(std::format("shuffle enabled (seed = {})",
|
|
config.random_seed));
|
|
}
|
|
#endif
|
|
}
|
|
|
|
for (unsigned rep = 0; rep < config.repeat; ++rep) {
|
|
std::string current_suite;
|
|
for (auto& test : tests) {
|
|
auto& suite = suite_results[suite_index[test.suite]];
|
|
// Emit suite header only when the suite name changes.
|
|
if (test.suite != current_suite) {
|
|
current_suite = test.suite;
|
|
reporter->on_suite_start(
|
|
suite.name,
|
|
std::count_if(
|
|
tests.begin(), tests.end(),
|
|
[&](const auto& t) {
|
|
return t.suite == suite.name;
|
|
}));
|
|
#ifdef VOID_HAS_LOG
|
|
if (log_sink) {
|
|
log_sink->info(std::format("suite start: {}",
|
|
suite.name));
|
|
}
|
|
#endif
|
|
}
|
|
reporter->on_test_start(test);
|
|
auto result = execute_test(test);
|
|
reporter->on_test_end(result);
|
|
log_result(result, std::format("{}.{}", result.suite,
|
|
result.name));
|
|
if (rep == config.repeat - 1) {
|
|
suite.tests.push_back(std::move(result));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
auto end_time = std::chrono::high_resolution_clock::now();
|
|
|
|
for (auto& suite : suite_results) {
|
|
for (const auto& test : suite.tests) {
|
|
switch (test.state) {
|
|
case TestState::Passed:
|
|
run_result.passed++;
|
|
break;
|
|
case TestState::Failed:
|
|
run_result.failed++;
|
|
break;
|
|
case TestState::Skipped:
|
|
run_result.skipped++;
|
|
break;
|
|
case TestState::Crashed:
|
|
run_result.crashed++;
|
|
break;
|
|
}
|
|
run_result.total++;
|
|
}
|
|
reporter->on_suite_end(suite);
|
|
run_result.suites.push_back(std::move(suite));
|
|
}
|
|
run_result.total_duration = end_time - start_time;
|
|
reporter->on_run_end(run_result);
|
|
reporter.reset();
|
|
if (!report_write_error.empty()) {
|
|
run_result.config_error = std::move(report_write_error);
|
|
std::cerr << "error: " << run_result.config_error << "\n";
|
|
}
|
|
#ifdef VOID_HAS_LOG
|
|
if (log_sink) {
|
|
log_sink->info(
|
|
std::format("run end: {} total / {} passed / {} failed / {} "
|
|
"skipped / {} crashed",
|
|
run_result.total, run_result.passed,
|
|
run_result.failed, run_result.skipped,
|
|
run_result.crashed));
|
|
}
|
|
if (env_log) {
|
|
Void::Log::set_sink({});
|
|
}
|
|
#endif
|
|
return run_result;
|
|
}
|
|
|
|
int Runner::main(int argc, char** argv)
|
|
{
|
|
auto config = parse_args(argc, argv);
|
|
if (!config.selftest_child.empty()) {
|
|
if (config.selftest_child == "abort") {
|
|
std::signal(SIGSEGV, SIG_DFL);
|
|
std::signal(SIGABRT, SIG_DFL);
|
|
std::abort();
|
|
}
|
|
if (config.selftest_child == "hang") {
|
|
std::this_thread::sleep_for(std::chrono::seconds{5});
|
|
return 0;
|
|
}
|
|
if (config.selftest_child == "flood") {
|
|
const std::string out_chunk(131072, 'O');
|
|
const std::string err_chunk(131072, 'E');
|
|
std::cout << out_chunk << std::flush;
|
|
std::cerr << err_chunk << std::flush;
|
|
return 0;
|
|
}
|
|
std::cerr << "Unknown --selftest-child mode: " << config.selftest_child << "\n";
|
|
return 2;
|
|
}
|
|
auto result = run(config);
|
|
if (!result.config_error.empty()) {
|
|
return 2;
|
|
}
|
|
return (result.failed + result.crashed > 0) ? 1 : 0;
|
|
}
|
|
} // namespace Void::Testing
|