422 lines
13 KiB
C++
422 lines
13 KiB
C++
#include <Void/Testing/Runner.hpp>
|
|
#include "Void/Testing/Config.hpp"
|
|
#include <Void/Logging/FileLogger.hpp>
|
|
#include <Void/Logging/Level.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/ConsoleReporter.hpp>
|
|
#include <Void/Stacktrace/Color.hpp>
|
|
#include <Void/Stacktrace/Stacktrace.hpp>
|
|
#include <Void/Testing/Detail/Registry.hpp>
|
|
#include <Void/Testing/Detail/SignalHandler.hpp>
|
|
#include <Void/Testing/JUnitReporter.hpp>
|
|
#include <algorithm>
|
|
#include <csignal>
|
|
#include <cstdlib>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <random>
|
|
#include <regex>
|
|
#include <unordered_map>
|
|
|
|
namespace Void::Testing
|
|
{
|
|
|
|
RunConfig Runner::parse_args(int argc, char** argv)
|
|
{
|
|
RunConfig config;
|
|
for (int i = 1; i < argc; ++i) {
|
|
std::string arg = argv[i];
|
|
if ((arg == "--filter" || arg == "--suite" || arg == "--reporter" ||
|
|
arg == "--output" || arg == "--tag" || arg == "--seed" ||
|
|
arg == "--repeat" || arg == "--log-file") &&
|
|
i + 1 < argc) {
|
|
std::string val = argv[++i];
|
|
if (arg == "--filter") {
|
|
config.filter_pattern = val;
|
|
} else if (arg == "--suite") {
|
|
config.suite_filter = val;
|
|
} else if (arg == "--reporter") {
|
|
config.reporter = val;
|
|
} else if (arg == "--output") {
|
|
config.output_file = val;
|
|
} else if (arg == "--tag") {
|
|
config.tag_filter = val;
|
|
} else if (arg == "--log-file") {
|
|
config.log_file = val;
|
|
} else if (arg == "--seed") {
|
|
config.shuffle = true;
|
|
config.random_seed = static_cast<unsigned>(std::stoul(val));
|
|
} else if (arg == "--repeat") {
|
|
config.repeat = static_cast<unsigned>(std::stoul(val));
|
|
}
|
|
|
|
} 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 == "--shuffle") {
|
|
config.shuffle = true;
|
|
std::random_device rd;
|
|
config.random_seed = rd();
|
|
}
|
|
}
|
|
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") {
|
|
static std::ofstream file{config.output_file.empty()
|
|
? "test_results.xml"
|
|
: config.output_file};
|
|
return std::make_unique<JUnitReporter>(file);
|
|
}
|
|
return std::make_unique<ConsoleReporter>(std::cout, !config.no_color);
|
|
}
|
|
|
|
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;
|
|
}
|
|
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());
|
|
result.stacktrace = Void::Stacktrace::format_stacktrace(
|
|
Void::Stacktrace::capture_stacktrace(1, 8));
|
|
} catch (...) {
|
|
result.state = TestState::Failed;
|
|
result.crash_info = "Unhandled unknown exception";
|
|
result.stacktrace = Void::Stacktrace::format_stacktrace(
|
|
Void::Stacktrace::capture_stacktrace(1, 8));
|
|
}
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
result.duration = end - start;
|
|
Detail::uninstall_signal_handlers();
|
|
Detail::TestContext::current().current_result = nullptr;
|
|
|
|
if (result.state != TestState::Skipped &&
|
|
result.state != TestState::Crashed) {
|
|
bool any =
|
|
std::any_of(result.assertions.begin(), result.assertions.end(),
|
|
[](const auto& a) { return !a.passed; });
|
|
result.state = any ? TestState::Failed : TestState::Passed;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void Runner::run_test_body(TestCase& test_case, TestResult&)
|
|
{
|
|
if (test_case.fixture_create) {
|
|
void* fixture = test_case.fixture_create();
|
|
Detail::TestContext::current().fixture = fixture;
|
|
try {
|
|
test_case.fixture_setup(fixture);
|
|
test_case.run();
|
|
test_case.fixture_teardown(fixture);
|
|
} catch (...) {
|
|
test_case.fixture_teardown(fixture);
|
|
test_case.fixture_destroy(fixture);
|
|
Detail::TestContext::current().fixture = nullptr;
|
|
throw;
|
|
}
|
|
test_case.fixture_destroy(fixture);
|
|
Detail::TestContext::current().fixture = nullptr;
|
|
} else {
|
|
test_case.run();
|
|
}
|
|
}
|
|
|
|
RunResult Runner::run(const RunConfig& config)
|
|
{
|
|
Void::Stacktrace::Color::g_color_enabled = !config.no_color;
|
|
if (config.show_help) {
|
|
using namespace Void::Stacktrace::Color;
|
|
|
|
std::cout
|
|
<< "\n"
|
|
<< apply(bold_blue, "╭─────────────────────────────────────────"
|
|
"─────────────────────────╮")
|
|
<< "\n"
|
|
<< apply(bold_blue, "│") << apply(bold_white, " Void Testing")
|
|
<< apply(dim, " — Command Line Interface")
|
|
<< std::string(28, ' ') << apply(bold_blue, "│") << "\n"
|
|
<< apply(bold_blue, "╰─────────────────────────────────────────"
|
|
"─────────────────────────╯")
|
|
<< "\n"
|
|
<< "\n"
|
|
<< " " << apply(bold_white, "USAGE:") << "\n"
|
|
<< " " << apply(cyan, "<executable>") << " "
|
|
<< apply(dim, "[options]") << "\n"
|
|
<< "\n"
|
|
<< " " << apply(bold_white, "OPTIONS:") << "\n"
|
|
<< " " << apply(green, "--filter <pattern> ")
|
|
<< apply(dim, "Run tests matching the regex pattern") << "\n"
|
|
<< " " << apply(green, "--suite <name> ")
|
|
<< apply(dim, "Run only tests in the specified suite") << "\n"
|
|
<< " " << apply(green, "--tag <name> ")
|
|
<< apply(dim, "Run only tests with the specified tag") << "\n"
|
|
<< " " << apply(green, "--list ")
|
|
<< apply(dim, "List discovered tests without running them")
|
|
<< "\n"
|
|
<< " " << apply(green, "--shuffle ")
|
|
<< apply(dim, "Randomize the order of test execution") << "\n"
|
|
<< " " << apply(green, "--seed <n> ")
|
|
<< apply(dim, "Set the random seed (implies --shuffle)") << "\n"
|
|
<< " " << apply(green, "--repeat <n> ")
|
|
<< apply(dim, "Repeat the test run n times") << "\n"
|
|
<< " " << apply(green, "--reporter <type> ")
|
|
<< apply(dim, "Choose the output reporter: console, junit")
|
|
<< "\n"
|
|
<< " " << apply(green, "--output <file> ")
|
|
<< apply(dim, "Write reporter output to a file") << "\n"
|
|
<< " " << apply(green, "--log-file <file> ")
|
|
<< apply(dim,
|
|
"Mirror test events to <file> via Void::Logging")
|
|
<< "\n"
|
|
<< " " << apply(green, "--verbose ")
|
|
<< apply(dim, "Enable verbose output") << "\n"
|
|
<< " " << apply(green, "--no-color ")
|
|
<< apply(dim, "Disable colored console output") << "\n"
|
|
<< " " << apply(green, "--help ")
|
|
<< apply(dim, "Show this help message") << "\n"
|
|
<< "\n"
|
|
<< " " << apply(bold_white, "EXAMPLES:") << "\n"
|
|
<< " " << apply(cyan, "./run_tests") << " "
|
|
<< apply(yellow, "--filter") << " \"math_tests.*\"" << "\n"
|
|
<< " " << apply(cyan, "./run_tests") << " "
|
|
<< apply(yellow, "--tag") << " integration "
|
|
<< apply(yellow, "--shuffle") << "\n"
|
|
<< " " << apply(cyan, "./run_tests") << " "
|
|
<< apply(yellow, "--reporter") << " junit "
|
|
<< apply(yellow, "--output") << " results.xml" << "\n"
|
|
<< "\n";
|
|
|
|
return {};
|
|
}
|
|
auto all_tests = Detail::Registry::instance().tests();
|
|
auto tests = filter_tests(all_tests, config);
|
|
auto reporter = create_reporter(config);
|
|
|
|
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";
|
|
}
|
|
}
|
|
|
|
if (config.list_only) {
|
|
for (const auto& t : tests) {
|
|
std::cout << t.suite << "." << t.name
|
|
<< (t.disabled ? " [DISABLED]" : "") << "\n";
|
|
}
|
|
return {};
|
|
}
|
|
|
|
RunResult run_result;
|
|
reporter->on_run_start(config, static_cast<int>(tests.size()));
|
|
if (log_sink) {
|
|
log_sink->info(std::format("run start: {} test(s) discovered",
|
|
tests.size()));
|
|
}
|
|
if (config.shuffle) {
|
|
std::shuffle(tests.begin(), tests.end(),
|
|
std::mt19937{config.random_seed});
|
|
if (log_sink) {
|
|
log_sink->info(std::format("shuffle enabled (seed = {})",
|
|
config.random_seed));
|
|
}
|
|
}
|
|
|
|
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 start_time = std::chrono::high_resolution_clock::now();
|
|
|
|
for (unsigned rep = 0; rep < config.repeat; ++rep) {
|
|
for (auto& test : tests) {
|
|
auto& suite = suite_results[suite_index[test.suite]];
|
|
if (rep == 0) {
|
|
reporter->on_suite_start(
|
|
suite.name, std::count_if(tests.begin(), tests.end(),
|
|
[&](const auto& t) {
|
|
return t.suite ==
|
|
suite.name;
|
|
}));
|
|
if (log_sink) {
|
|
log_sink->info(
|
|
std::format("suite start: {}", suite.name));
|
|
}
|
|
}
|
|
reporter->on_test_start(test);
|
|
auto result = execute_test(test);
|
|
reporter->on_test_end(result);
|
|
if (log_sink) {
|
|
const auto qualified =
|
|
std::format("{}.{}", result.suite, result.name);
|
|
switch (result.state) {
|
|
case TestState::Passed:
|
|
log_sink->info(
|
|
std::format("test pass: {}", qualified));
|
|
break;
|
|
case TestState::Failed:
|
|
log_sink->error(std::format(
|
|
"test fail: {} — {}", qualified,
|
|
result.crash_info.empty() ? "assertion failed"
|
|
: result.crash_info));
|
|
break;
|
|
case TestState::Skipped:
|
|
log_sink->warning(std::format(
|
|
"test skip: {} ({})", qualified,
|
|
result.skip_reason.empty() ? "no reason"
|
|
: result.skip_reason));
|
|
break;
|
|
case TestState::Crashed:
|
|
log_sink->fatal(std::format(
|
|
"test crash: {} — {}", qualified,
|
|
result.crash_info));
|
|
break;
|
|
}
|
|
}
|
|
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);
|
|
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));
|
|
}
|
|
return run_result;
|
|
}
|
|
|
|
int Runner::main(int argc, char** argv)
|
|
{
|
|
auto config = parse_args(argc, argv);
|
|
auto result = run(config);
|
|
return (result.failed + result.crashed > 0) ? 1 : 0;
|
|
}
|
|
} // namespace Void::Testing
|