feat(test): write JUnit reports and honour a per-test timeout
This commit is contained in:
1 parent
ce47c23cda
commit
1e670cb96b
7 files changed
+147
-3
No files matched your search
+121
-1
@@ -356,6 +356,15 @@ combine_trusted_keys(const std::vector<std::string> &inline_keys,
|
||||
return combined;
|
||||
}
|
||||
|
||||
struct TestRunRecord {
|
||||
std::string profile;
|
||||
std::string name;
|
||||
bool passed{false};
|
||||
int exit_code{-1};
|
||||
double elapsed_ms{0};
|
||||
std::string output;
|
||||
};
|
||||
|
||||
struct BuildCmdOptions {
|
||||
std::string project_arg;
|
||||
std::optional<std::string> build_arg;
|
||||
@@ -377,6 +386,8 @@ struct BuildCmdOptions {
|
||||
std::vector<std::string> trusted_key_files;
|
||||
std::optional<std::string> target_os_arg;
|
||||
std::optional<std::string> target_arch_arg;
|
||||
std::optional<std::string> junit_arg;
|
||||
std::string test_timeout_arg{"300"};
|
||||
bool publish_editor_selection{true};
|
||||
bool no_cache{false};
|
||||
bool locked{false};
|
||||
@@ -386,6 +397,14 @@ struct BuildCmdOptions {
|
||||
|
||||
int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
const std::string cmd_name = run_tests ? "vbs test" : "vbs build";
|
||||
int test_timeout_seconds = 300;
|
||||
if (!Void::Str::to_int(opts.test_timeout_arg, test_timeout_seconds) ||
|
||||
test_timeout_seconds <= 0 || test_timeout_seconds > 86400) {
|
||||
std::cerr << cmd_name << ": invalid --test-timeout value '" << opts.test_timeout_arg
|
||||
<< "' (expected 1..86400 seconds)\n";
|
||||
return 2;
|
||||
}
|
||||
std::vector<TestRunRecord> test_records;
|
||||
const auto total_sw = Void::Time::Stopwatch::started();
|
||||
|
||||
std::optional<CompilerFamily> compiler_override;
|
||||
@@ -495,6 +514,7 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
const std::string target_os = opts.target_os_arg.value_or(Toolchain::host_os_name());
|
||||
const std::string target_arch =
|
||||
opts.target_arch_arg.value_or(Toolchain::host_architecture_name());
|
||||
@@ -834,8 +854,20 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
treq.executable = t.primary_outputs.front();
|
||||
}
|
||||
treq.working_directory = project_root;
|
||||
treq.timeout = std::chrono::seconds{300};
|
||||
treq.timeout = std::chrono::seconds{test_timeout_seconds};
|
||||
auto tres = Platform::run_supervised(treq);
|
||||
TestRunRecord record;
|
||||
record.profile = prof_name;
|
||||
record.name = t.name;
|
||||
record.passed = tres.has_value() && tres->succeeded();
|
||||
record.elapsed_ms = tsw.elapsed_ms();
|
||||
if (tres) {
|
||||
record.output = tres->stdout_data + tres->stderr_data;
|
||||
record.exit_code = tres->exit_code;
|
||||
} else {
|
||||
record.output = tres.error().message();
|
||||
}
|
||||
test_records.push_back(std::move(record));
|
||||
if (!tres || !tres->succeeded()) {
|
||||
Void::Log::error("Test", "[{}] FAILED '{}' ({:.0f} ms)", prof_name, t.name,
|
||||
tsw.elapsed_ms());
|
||||
@@ -856,6 +888,91 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
std::string out;
|
||||
out.reserve(text.size());
|
||||
for (const char c : text) {
|
||||
switch (c) {
|
||||
case '&':
|
||||
out += "&";
|
||||
break;
|
||||
case '<':
|
||||
out += "<";
|
||||
break;
|
||||
case '>':
|
||||
out += ">";
|
||||
break;
|
||||
case '"':
|
||||
out += """;
|
||||
break;
|
||||
case '\'':
|
||||
out += "'";
|
||||
break;
|
||||
default:
|
||||
if (static_cast<unsigned char>(c) >= 0x20 || c == '\n' || c == '\t') {
|
||||
out.push_back(c);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
};
|
||||
std::size_t failures = 0;
|
||||
for (const auto &record : test_records) {
|
||||
if (!record.passed) {
|
||||
++failures;
|
||||
}
|
||||
}
|
||||
std::string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||||
xml += std::format("<testsuites name=\"vbs\" tests=\"{}\" failures=\"{}\">\n",
|
||||
test_records.size(), failures);
|
||||
std::string current_profile;
|
||||
auto open_suite = [&] {
|
||||
if (!current_profile.empty()) {
|
||||
xml += " </testsuite>\n";
|
||||
}
|
||||
std::size_t suite_tests = 0;
|
||||
std::size_t suite_failures = 0;
|
||||
for (const auto &record : test_records) {
|
||||
if (record.profile == current_profile) {
|
||||
++suite_tests;
|
||||
if (!record.passed) {
|
||||
++suite_failures;
|
||||
}
|
||||
}
|
||||
}
|
||||
xml += std::format(" <testsuite name=\"{}\" tests=\"{}\" failures=\"{}\">\n",
|
||||
xml_escape(current_profile), suite_tests, suite_failures);
|
||||
};
|
||||
for (const auto &record : test_records) {
|
||||
if (record.profile != current_profile) {
|
||||
current_profile = record.profile;
|
||||
open_suite();
|
||||
}
|
||||
xml += std::format(" <testcase classname=\"{}\" name=\"{}\" time=\"{:.3f}\"",
|
||||
xml_escape(record.profile), xml_escape(record.name),
|
||||
record.elapsed_ms / 1000.0);
|
||||
if (record.passed) {
|
||||
xml += "/>\n";
|
||||
} else {
|
||||
xml += std::format(">\n <failure message=\"exited with status "
|
||||
"{}\">{}</failure>\n </testcase>\n",
|
||||
record.exit_code, xml_escape(record.output));
|
||||
}
|
||||
}
|
||||
if (!current_profile.empty()) {
|
||||
xml += " </testsuite>\n";
|
||||
}
|
||||
xml += "</testsuites>\n";
|
||||
if (auto written = Platform::atomic_write_file(*opts.junit_arg, xml, false); !written) {
|
||||
std::cerr << "vbs test: cannot write JUnit report: " << written.error().message()
|
||||
<< '\n';
|
||||
all_ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
Void::Log::debug("Build", "Total wall time: {:.2f}s", total_sw.elapsed_ms() / 1000.0);
|
||||
return all_ok ? 0 : 1;
|
||||
}
|
||||
@@ -2798,6 +2915,9 @@ int main(int argc, char **argv) {
|
||||
build_opts.target_os_arg)
|
||||
.option("--target-arch", "", "Target architecture (default: host, e.g. x86_64)",
|
||||
build_opts.target_arch_arg)
|
||||
.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("--editor-profile", "", "Editor profile for compile_commands.json",
|
||||
build_opts.editor_profile)
|
||||
.option_many("--build-source", "", "Additional build description source",
|
||||
|
||||
Reference in new issue
Block a user