Aggregate junit report into one file (#1056)

Some systems require junit report to be in a single file. For example `bazel` https://bazel.build/reference/test-encyclopedia needs single file to be available in `XML_OUTPUT_FILE` path.

To avoid implementing junit aggregation in pkl wrappers in different places this PR instead adds a `--junit-aggregate-reports` flag to return all junit reports as a single file.

Additional flag `--junit-aggregate-suite-name` is added to allow overriding global test suite name from default `pkl-tests`
This commit is contained in:
Artem Yarmoliuk
2025-06-07 01:33:13 +01:00
committed by GitHub
parent 0b0f3b131d
commit 3bd8a88506
11 changed files with 353 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,9 @@ package org.pkl.core.stdlib.test.report;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.graalvm.collections.EconomicMap;
import org.pkl.core.TestResults;
import org.pkl.core.TestResults.Error;
@@ -38,11 +40,45 @@ import org.pkl.core.util.EconomicMaps;
public final class JUnitReport implements TestReport {
private final String aggregateSuiteName;
public JUnitReport(String aggregateSuiteName) {
this.aggregateSuiteName = aggregateSuiteName;
}
public JUnitReport() {
this("");
}
@Override
public void report(TestResults results, Writer writer) throws IOException {
writer.append(renderXML(" ", "1.0", buildSuite(results)));
}
@Override
public void summarize(List<TestResults> allTestResults, Writer writer) throws IOException {
var totalTests = allTestResults.stream().collect(Collectors.summingLong(r -> r.totalTests()));
var totalFailures =
allTestResults.stream().collect(Collectors.summingLong(r -> r.totalFailures()));
assert aggregateSuiteName != null;
var attrs =
buildAttributes(
"name", aggregateSuiteName,
"tests", totalTests,
"failures", totalFailures);
var tests =
allTestResults.stream()
.map(r -> buildSuite(r))
.collect(Collectors.toCollection(ArrayList::new));
var suite = buildXmlElement("testsuites", attrs, tests.toArray(new VmDynamic[0]));
writer.append(renderXML(" ", "1.0", suite));
}
private VmDynamic buildSuite(TestResults results) {
if (results.error() != null) {
var testCase = rootTestCase(results, results.error());

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,6 +57,7 @@ public final class SimpleReport implements TestReport {
writer.append(builder.toString());
}
@Override
public void summarize(List<TestResults> allTestResults, Writer writer) throws IOException {
var totalTests = 0;
var totalFailedTests = 0;

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import org.pkl.core.PklBugException;
import org.pkl.core.TestResults;
import org.pkl.core.util.StringBuilderWriter;
@@ -28,7 +29,9 @@ public interface TestReport {
void report(TestResults results, Writer writer) throws IOException;
default String report(TestResults results) {
void summarize(List<TestResults> allTestResults, Writer writer) throws IOException;
default String report(TestResults results) throws IOException {
try {
var builder = new StringBuilder();
var writer = new StringBuilderWriter(builder);
@@ -44,4 +47,10 @@ public interface TestReport {
report(results, writer);
}
}
default void summarizeToPath(List<TestResults> allTestResults, Path path) throws IOException {
try (var writer = new FileWriter(path.toFile(), StandardCharsets.UTF_8)) {
summarize(allTestResults, writer);
}
}
}