From 64d09c76b6976ca653e8dcfc8358267c4fb531f8 Mon Sep 17 00:00:00 2001 From: swarren12 <33803336+swarren12@users.noreply.github.com> Date: Wed, 19 May 2021 12:30:24 +0100 Subject: [PATCH] Use original JUnit test class name instead of suite class in test XML report --- src/com/facebook/buck/jvm/java/JavaTest.java | 26 +++++++++++++++---- .../facebook/buck/test/TestCaseSummary.java | 14 ++++++++++ .../buck/test/XmlTestResultParser.java | 12 +++++++-- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/com/facebook/buck/jvm/java/JavaTest.java b/src/com/facebook/buck/jvm/java/JavaTest.java index e9f81f2ff74..26225445939 100644 --- a/src/com/facebook/buck/jvm/java/JavaTest.java +++ b/src/com/facebook/buck/jvm/java/JavaTest.java @@ -85,6 +85,7 @@ import java.util.SortedSet; import java.util.concurrent.Callable; import java.util.logging.Level; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nullable; @@ -422,10 +423,11 @@ public Path getPathToTestOutputDirectory() { /** @return a test case result, named "main", signifying a failure of the entire test class. */ private TestCaseSummary getTestClassFailedSummary(String testClass, String message, long time) { return new TestCaseSummary( - testClass, - ImmutableList.of( - new TestResultSummary( - testClass, "main", ResultType.FAILURE, time, message, "", "", ""))); + testClass, + false, + ImmutableList.of( + new TestResultSummary( + testClass, "main", ResultType.FAILURE, time, message, "", "", ""))); } @Override @@ -473,7 +475,12 @@ public Callable interpretTestResults( // definitively say whether or not a class should be run. It's not possible, for example, // to filter testClassNames here at the buck end. } else if (Files.isRegularFile(testResultFile)) { - summaries.add(XmlTestResultParser.parse(testResultFile)); + TestCaseSummary summary = XmlTestResultParser.parse(testResultFile); + if (summary.isTestSuite()) { + summaries.addAll(unrollTestSuiteSummary(summary)); + } else { + summaries.add(summary); + } } } @@ -685,6 +692,15 @@ protected ImmutableSet getExtraRequiredPaths( .build(); } + private List unrollTestSuiteSummary(TestCaseSummary summary) { + Map> summariesByActualTestCase = summary.getTestResults() + .stream().collect(Collectors.groupingBy(TestResultSummary::getTestCaseName)); + + return summariesByActualTestCase.entrySet().stream() + .map(entry -> new TestCaseSummary(entry.getKey(), false, entry.getValue())) + .collect(Collectors.toList()); + } + public interface AdditionalClasspathEntriesProvider { ImmutableList getAdditionalClasspathEntries(SourcePathResolverAdapter resolver); } diff --git a/src/com/facebook/buck/test/TestCaseSummary.java b/src/com/facebook/buck/test/TestCaseSummary.java index 9d3494c99ac..cd0f2f1abdb 100644 --- a/src/com/facebook/buck/test/TestCaseSummary.java +++ b/src/com/facebook/buck/test/TestCaseSummary.java @@ -29,6 +29,7 @@ public class TestCaseSummary implements TestCaseSummaryExternalInterface testResults; private final boolean isDryRun; private final boolean hasAssumptionViolations; @@ -38,7 +39,15 @@ public class TestCaseSummary implements TestCaseSummaryExternalInterface testResults) { + this(testCaseName, false, testResults); + } + + public TestCaseSummary( + String testCaseName, + boolean testSuite, + List testResults) { this.testCaseName = testCaseName; + this.testSuite = testSuite; this.testResults = ImmutableList.copyOf(testResults); boolean isDryRun = false; @@ -110,6 +119,11 @@ public long getTotalTime() { return totalTime; } + /** @return whether this summary represents a suite of tests or a single class */ + public boolean isTestSuite() { + return testSuite; + } + /** @return a one-line, printable summary */ public String getOneLineSummary(Locale locale, boolean hasPassingDependencies, Ansi ansi) { String statusText; diff --git a/src/com/facebook/buck/test/XmlTestResultParser.java b/src/com/facebook/buck/test/XmlTestResultParser.java index 027305fca41..1fb756e1704 100644 --- a/src/com/facebook/buck/test/XmlTestResultParser.java +++ b/src/com/facebook/buck/test/XmlTestResultParser.java @@ -139,6 +139,7 @@ private static TestCaseSummary doParse(String xml) throws IOException, SAXExcept Element root = doc.getDocumentElement(); Preconditions.checkState("testcase".equals(root.getTagName())); String testCaseName = root.getAttribute("name"); + boolean testSuite = false; NodeList testElements = doc.getElementsByTagName("test"); List testResults = Lists.newArrayListWithCapacity(testElements.getLength()); @@ -176,13 +177,20 @@ private static TestCaseSummary doParse(String xml) throws IOException, SAXExcept stdErr = null; } + String actualTestCaseName = node.getAttribute("suite"); + if (actualTestCaseName != null && !actualTestCaseName.isEmpty()) { + testSuite |= !actualTestCaseName.equals(testCaseName); + } else { + actualTestCaseName = testCaseName; + } + TestResultSummary testResult = new TestResultSummary( - testCaseName, testName, type, time, message, stacktrace, stdOut, stdErr); + actualTestCaseName, testName, type, time, message, stacktrace, stdOut, stdErr); testResults.add(testResult); } - return new TestCaseSummary(testCaseName, testResults); + return new TestCaseSummary(testCaseName, testSuite, testResults); } private static String createDetailedExceptionMessage(Path xmlFile, String xmlFileContents) {