feat(test): write lcov coverage reports
VoidBuildSystem CI / signing-openssl-gcc16 (push) Successful in 1m40s
VoidBuildSystem CI / fuzz-clang (push) Failing after 1m47s
VoidBuildSystem CI / sanitizers-gcc16 (push) Successful in 3m0s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 6m44s

This commit is contained in:
faumaray committed 2026-12-05 14:50:44 +03:00
1 parent d420732016
commit 92c4be9273
9 files changed
+326 -1

No files matched your search

+31
View File
@@ -29,6 +29,7 @@
#include <Void/Build/Provider/Provider.hpp>
#include <Void/Build/Provider/Translator.hpp>
#include <Void/Build/Report/Cdash.hpp>
#include <Void/Build/Report/Coverage.hpp>
#include <Void/Build/SelfHost/TestRegistryGen.hpp>
#include <Void/Build/Snapshot/Snapshot.hpp>
#include <Void/Build/State/StateStore.hpp>
@@ -395,6 +396,7 @@ struct BuildCmdOptions {
std::optional<std::string> target_os_arg;
std::optional<std::string> target_arch_arg;
std::optional<std::string> junit_arg;
std::optional<std::string> coverage_arg;
std::optional<std::string> cdash_arg;
std::optional<std::string> cdash_project_arg;
std::optional<std::string> cdash_build_arg;
@@ -543,6 +545,12 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
std::cerr << "vbs: " << ex.what() << '\n';
return 2;
}
if (opts.coverage_arg) {
// Instrument every compile and link so gcov data is produced when tests run.
// (GCC and Clang both accept --coverage.)
ctx.compile_options.push_back("--coverage");
ctx.link_options.push_back("--coverage");
}
auto tc = Toolchain::discover_and_probe(ctx, build_root / ".vbs" / "probes");
if (!tc) {
@@ -1102,6 +1110,27 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
}
}
// lcov coverage report from the gcov data produced by the instrumented test binaries.
if (run_tests && opts.coverage_arg) {
const auto gcda = Report::collect_gcda_files(build_root);
std::error_code cov_ec;
const auto source_root = std::filesystem::weakly_canonical(project_root, cov_ec);
auto cov = Report::write_coverage_report(gcda, *opts.coverage_arg, source_root);
if (!cov) {
std::cerr << "vbs test: " << cov.error().message() << '\n';
all_ok = false;
} else {
const double percent =
cov->lines_found == 0
? 0.0
: 100.0 * static_cast<double>(cov->lines_hit) /
static_cast<double>(cov->lines_found);
Void::Log::info("Test", "Coverage {:.1f}% ({}/{} lines) across {} file(s) -> {}", percent,
cov->lines_hit, cov->lines_found, cov->files,
(std::filesystem::path{*opts.coverage_arg} / "coverage.info").string());
}
}
// JUnit XML report for CI systems (aggregated across selected profiles).
if (opts.junit_arg && !opts.junit_arg->empty() && !test_records.empty()) {
const auto xml_escape = [](std::string_view text) {
@@ -3636,6 +3665,8 @@ int main(int argc, char **argv) {
.option_default("--test-timeout", "", "Per-test timeout in seconds (1..86400)",
build_opts.test_timeout_arg, "300")
.option("--junit", "", "Write a JUnit XML report of test results", build_opts.junit_arg)
.option("--coverage", "", "Build with --coverage and write an lcov report to this directory",
build_opts.coverage_arg)
.option("--cdash", "", "Upload a CDash test report to this base URL",
build_opts.cdash_arg)
.option("--cdash-project", "", "CDash project name (default: project directory name)",