Skip to content

Commit b5ca9c5

Browse files
authored
Simplify gradle test task error reporting (#59760) (#59966)
* Simplify test error reporting - avoid using extra plugin - avoid extra task listener (should be avoided related to #57918 ) - keep all logic in the listener
1 parent aae08e5 commit b5ca9c5

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.gradle.api.artifacts.ModuleDependency;
3737
import org.gradle.api.artifacts.ProjectDependency;
3838
import org.gradle.api.artifacts.ResolutionStrategy;
39-
import org.gradle.api.execution.TaskActionListener;
4039
import org.gradle.api.file.FileCollection;
4140
import org.gradle.api.plugins.BasePlugin;
4241
import org.gradle.api.plugins.JavaLibraryPlugin;
@@ -72,8 +71,6 @@ public class ElasticsearchJavaPlugin implements Plugin<Project> {
7271
public void apply(Project project) {
7372
// make sure the global build info plugin is applied to the root project
7473
project.getRootProject().getPluginManager().apply(GlobalBuildInfoPlugin.class);
75-
// apply global test task failure listener
76-
project.getRootProject().getPluginManager().apply(TestFailureReportingPlugin.class);
7774
// common repositories setup
7875
project.getPluginManager().apply(RepositoriesSetupPlugin.class);
7976
project.getPluginManager().apply(JavaLibraryPlugin.class);
@@ -223,7 +220,7 @@ public static void configureTestTasks(Project project) {
223220
project.getTasks().withType(Test.class).configureEach(test -> {
224221
File testOutputDir = new File(test.getReports().getJunitXml().getDestination(), "output");
225222

226-
ErrorReportingTestListener listener = new ErrorReportingTestListener(test.getTestLogging(), testOutputDir);
223+
ErrorReportingTestListener listener = new ErrorReportingTestListener(test.getTestLogging(), test.getLogger(), testOutputDir);
227224
test.getExtensions().add("errorReportingTestListener", listener);
228225
test.addTestOutputListener(listener);
229226
test.addTestListener(listener);
@@ -473,31 +470,4 @@ private static void configureJavadoc(Project project) {
473470
// ensure javadoc task is run with 'check'
474471
project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME).configure(t -> t.dependsOn(javadoc));
475472
}
476-
477-
static class TestFailureReportingPlugin implements Plugin<Project> {
478-
@Override
479-
public void apply(Project project) {
480-
if (project != project.getRootProject()) {
481-
throw new IllegalStateException(this.getClass().getName() + " can only be applied to the root project.");
482-
}
483-
484-
project.getGradle().addListener(new TaskActionListener() {
485-
@Override
486-
public void beforeActions(Task task) {}
487-
488-
@Override
489-
public void afterActions(Task task) {
490-
if (task instanceof Test) {
491-
ErrorReportingTestListener listener = task.getExtensions().findByType(ErrorReportingTestListener.class);
492-
if (listener != null && listener.getFailedTests().size() > 0) {
493-
task.getLogger().lifecycle("\nTests with failures:");
494-
for (ErrorReportingTestListener.Descriptor failure : listener.getFailedTests()) {
495-
task.getLogger().lifecycle(" - " + failure.getFullName());
496-
}
497-
}
498-
}
499-
}
500-
});
501-
}
502-
}
503473
}

buildSrc/src/main/java/org/elasticsearch/gradle/test/ErrorReportingTestListener.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.gradle.api.internal.tasks.testing.logging.FullExceptionFormatter;
2222
import org.gradle.api.internal.tasks.testing.logging.TestExceptionFormatter;
2323
import org.gradle.api.logging.Logger;
24-
import org.gradle.api.logging.Logging;
2524
import org.gradle.api.tasks.testing.TestDescriptor;
2625
import org.gradle.api.tasks.testing.TestListener;
2726
import org.gradle.api.tasks.testing.TestOutputEvent;
@@ -49,17 +48,18 @@
4948
import java.util.concurrent.ConcurrentHashMap;
5049

5150
public class ErrorReportingTestListener implements TestOutputListener, TestListener {
52-
private static final Logger LOGGER = Logging.getLogger(ErrorReportingTestListener.class);
5351
private static final String REPRODUCE_WITH_PREFIX = "REPRODUCE WITH";
5452

5553
private final TestExceptionFormatter formatter;
5654
private final File outputDirectory;
55+
private final Logger taskLogger;
5756
private Map<Descriptor, EventWriter> eventWriters = new ConcurrentHashMap<>();
5857
private Map<Descriptor, Deque<String>> reproductionLines = new ConcurrentHashMap<>();
5958
private Set<Descriptor> failedTests = new LinkedHashSet<>();
6059

61-
public ErrorReportingTestListener(TestLogging testLogging, File outputDirectory) {
60+
public ErrorReportingTestListener(TestLogging testLogging, Logger taskLogger, File outputDirectory) {
6261
this.formatter = new FullExceptionFormatter(testLogging);
62+
this.taskLogger = taskLogger;
6363
this.outputDirectory = outputDirectory;
6464
}
6565

@@ -120,6 +120,15 @@ public void afterSuite(final TestDescriptor suite, TestResult result) {
120120
}
121121
}
122122
}
123+
if (suite.getParent() == null) {
124+
// per test task top level gradle test run suite finished
125+
if (getFailedTests().size() > 0) {
126+
taskLogger.lifecycle("\nTests with failures:");
127+
for (ErrorReportingTestListener.Descriptor failure : getFailedTests()) {
128+
taskLogger.lifecycle(" - " + failure.getFullName());
129+
}
130+
}
131+
}
123132
} catch (IOException e) {
124133
throw new UncheckedIOException("Error reading test suite output", e);
125134
} finally {
@@ -129,7 +138,7 @@ public void afterSuite(final TestDescriptor suite, TestResult result) {
129138
try {
130139
writer.close();
131140
} catch (IOException e) {
132-
LOGGER.error("Failed to close test suite output stream", e);
141+
taskLogger.error("Failed to close test suite output stream", e);
133142
}
134143
}
135144
}

0 commit comments

Comments
 (0)