feat(test): write lcov coverage reports
This commit is contained in:
1 parent
d420732016
commit
92c4be9273
9 files changed
+326
-1
No files matched your search
@@ -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)",
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
#include <Void/Build/Report/Coverage.hpp>
|
||||
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace Void::Build::Report {
|
||||
namespace {
|
||||
|
||||
using JsonVal = Void::Json::Strict::Value;
|
||||
|
||||
Void::Result::Error make_error(std::string msg) {
|
||||
return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
std::move(msg));
|
||||
}
|
||||
|
||||
const JsonVal *find_key(const JsonVal::Object &object, std::string_view key) {
|
||||
const auto it = object.find(std::string{key});
|
||||
return it == object.end() ? nullptr : &it->second;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<std::filesystem::path> collect_gcda_files(const std::filesystem::path &root) {
|
||||
std::vector<std::filesystem::path> files;
|
||||
std::error_code ec;
|
||||
if (!std::filesystem::is_directory(root, ec)) {
|
||||
return files;
|
||||
}
|
||||
// Per-entry errors (dangling symlinks, unreadable dirs) must not abort the walk: a fresh
|
||||
// build tree can contain them while it is still being populated.
|
||||
try {
|
||||
for (const auto &entry : std::filesystem::recursive_directory_iterator(
|
||||
root, std::filesystem::directory_options::skip_permission_denied)) {
|
||||
std::error_code entry_ec;
|
||||
if (entry.is_regular_file(entry_ec) && !entry_ec &&
|
||||
entry.path().extension() == ".gcda") {
|
||||
files.push_back(entry.path());
|
||||
}
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error &) {
|
||||
// Keep whatever was collected before the unreadable subtree.
|
||||
}
|
||||
std::ranges::sort(files);
|
||||
return files;
|
||||
}
|
||||
|
||||
Void::Result::Result<CoverageSummary, Void::Result::Error>
|
||||
write_coverage_report(const std::vector<std::filesystem::path> &gcda_files,
|
||||
const std::filesystem::path &output_dir,
|
||||
const std::filesystem::path &source_root,
|
||||
const std::string &gcov_executable) {
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(output_dir, ec);
|
||||
if (ec) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
std::format("cannot create coverage directory '{}': {}", output_dir.string(),
|
||||
ec.message()),
|
||||
ec.value())};
|
||||
}
|
||||
if (gcda_files.empty()) {
|
||||
return std::unexpected{make_error("no .gcda coverage data was produced")};
|
||||
}
|
||||
|
||||
// Source path -> line -> execution count (max across translation units that include it).
|
||||
std::map<std::string, std::map<std::int64_t, std::int64_t>, std::less<>> coverage;
|
||||
for (const auto &gcda : gcda_files) {
|
||||
Platform::ProcessRequest req;
|
||||
req.executable = gcov_executable;
|
||||
req.arguments = {"--json-format", "--stdout", gcda.filename().string()};
|
||||
req.working_directory = gcda.parent_path();
|
||||
req.timeout = std::chrono::seconds{60};
|
||||
auto res = Platform::run_supervised(req);
|
||||
if (!res || !res->succeeded()) {
|
||||
return std::unexpected{make_error(std::format(
|
||||
"gcov failed for '{}': {}", gcda.string(),
|
||||
res ? res->stderr_data : res.error().message()))};
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(res->stdout_data);
|
||||
if (!parsed) {
|
||||
return std::unexpected{make_error(
|
||||
std::format("gcov produced invalid JSON for '{}'", gcda.string()))};
|
||||
}
|
||||
const auto *root = std::get_if<JsonVal::Object>(&parsed->value);
|
||||
if (root == nullptr) {
|
||||
continue;
|
||||
}
|
||||
std::filesystem::path cwd;
|
||||
if (const auto *cwd_v = find_key(*root, "current_working_directory")) {
|
||||
if (const auto *text = std::get_if<std::string>(&cwd_v->value)) {
|
||||
cwd = *text;
|
||||
}
|
||||
}
|
||||
const auto *files_v = find_key(*root, "files");
|
||||
const auto *files = files_v ? std::get_if<JsonVal::Array>(&files_v->value) : nullptr;
|
||||
if (files == nullptr) {
|
||||
continue;
|
||||
}
|
||||
for (const auto &file_entry : *files) {
|
||||
const auto *file_obj = std::get_if<JsonVal::Object>(&file_entry.value);
|
||||
if (file_obj == nullptr) {
|
||||
continue;
|
||||
}
|
||||
const auto *path_v = find_key(*file_obj, "file");
|
||||
const auto *path_text = path_v ? std::get_if<std::string>(&path_v->value) : nullptr;
|
||||
if (path_text == nullptr) {
|
||||
continue;
|
||||
}
|
||||
std::filesystem::path source{*path_text};
|
||||
if (source.is_relative()) {
|
||||
source = (cwd / source).lexically_normal();
|
||||
}
|
||||
if (!source_root.empty() && !Platform::path_is_within(source_root, source)) {
|
||||
continue;
|
||||
}
|
||||
const auto *lines_v = find_key(*file_obj, "lines");
|
||||
const auto *lines = lines_v ? std::get_if<JsonVal::Array>(&lines_v->value) : nullptr;
|
||||
if (lines == nullptr) {
|
||||
continue;
|
||||
}
|
||||
auto &entry = coverage[source.string()];
|
||||
for (const auto &line_entry : *lines) {
|
||||
const auto *line_obj = std::get_if<JsonVal::Object>(&line_entry.value);
|
||||
if (line_obj == nullptr) {
|
||||
continue;
|
||||
}
|
||||
std::int64_t number = 0;
|
||||
std::int64_t count = 0;
|
||||
if (const auto *number_v = find_key(*line_obj, "line_number")) {
|
||||
if (const auto *value = std::get_if<std::int64_t>(&number_v->value)) {
|
||||
number = *value;
|
||||
}
|
||||
}
|
||||
if (const auto *count_v = find_key(*line_obj, "count")) {
|
||||
if (const auto *value = std::get_if<std::int64_t>(&count_v->value)) {
|
||||
count = *value;
|
||||
}
|
||||
}
|
||||
if (number <= 0) {
|
||||
continue;
|
||||
}
|
||||
auto &slot = entry[number];
|
||||
slot = std::max(slot, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CoverageSummary summary;
|
||||
std::string lcov;
|
||||
for (auto &[path, lines] : coverage) {
|
||||
if (lines.empty()) {
|
||||
continue;
|
||||
}
|
||||
++summary.files;
|
||||
std::size_t hit = 0;
|
||||
lcov += "TN:\nSF:" + path + '\n';
|
||||
for (const auto &[number, count] : lines) {
|
||||
lcov += std::format("DA:{},{}\n", number, count);
|
||||
if (count > 0) {
|
||||
++hit;
|
||||
}
|
||||
}
|
||||
lcov += std::format("LF:{}\nLH:{}\nend_of_record\n", lines.size(), hit);
|
||||
summary.lines_found += lines.size();
|
||||
summary.lines_hit += hit;
|
||||
}
|
||||
|
||||
if (auto written = Platform::atomic_write_file(output_dir / "coverage.info", lcov, true);
|
||||
!written) {
|
||||
return std::unexpected{written.error()};
|
||||
}
|
||||
const double percent =
|
||||
summary.lines_found == 0
|
||||
? 0.0
|
||||
: 100.0 * static_cast<double>(summary.lines_hit) /
|
||||
static_cast<double>(summary.lines_found);
|
||||
const std::string summary_text =
|
||||
std::format("Coverage: {:.1f}% ({}/{} lines) across {} file(s)\n", percent,
|
||||
summary.lines_hit, summary.lines_found, summary.files);
|
||||
if (auto written = Platform::atomic_write_file(output_dir / "summary.txt", summary_text, true);
|
||||
!written) {
|
||||
return std::unexpected{written.error()};
|
||||
}
|
||||
return summary;
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Report
|
||||
Reference in new issue
Block a user