Skip to content

Commit

Permalink
Move test error reporting to java plugin (#57259)
Browse files Browse the repository at this point in the history
This commit moves the global hook for reporting failed test cases to the
ElasticsearchJavaPlugin. It should always be applied for all java
projects since the Test class is what emits the failures logged.
  • Loading branch information
rjernst committed May 28, 2020
1 parent f5bbf10 commit ef48257
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ class BuildPlugin implements Plugin<Project> {
project.pluginManager.apply('elasticsearch.publish')
project.pluginManager.apply(DependenciesInfoPlugin)

// apply global test task failure listener
project.rootProject.pluginManager.apply(TestFailureReportingPlugin)

project.getTasks().register("buildResources", ExportElasticsearchBuildResourcesTask)

project.extensions.getByType(ExtraPropertiesExtension).set('versions', VersionProperties.versions)
Expand Down Expand Up @@ -141,36 +138,6 @@ class BuildPlugin implements Plugin<Project> {
}
}


private static class TestFailureReportingPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
if (project != project.rootProject) {
throw new IllegalStateException("${this.class.getName()} can only be applied to the root project.")
}

project.gradle.addListener(new TaskActionListener() {
@Override
void beforeActions(Task task) {

}

@Override
void afterActions(Task task) {
if (task instanceof Test) {
ErrorReportingTestListener listener = task.extensions.findByType(ErrorReportingTestListener)
if (listener != null && listener.getFailedTests().size() > 0) {
task.logger.lifecycle("\nTests with failures:")
listener.getFailedTests().each {
task.logger.lifecycle(" - ${it.getFullName()}")
}
}
}
}
})
}
}

private static inFipsJvm(){
return Boolean.parseBoolean(System.getProperty("tests.fips.enabled"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.gradle.api.artifacts.dsl.RepositoryHandler;
import org.gradle.api.artifacts.repositories.IvyArtifactRepository;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.execution.TaskActionListener;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaPlugin;
Expand Down Expand Up @@ -78,8 +79,11 @@ public class ElasticsearchJavaPlugin implements Plugin<Project> {
public void apply(Project project) {
// make sure the global build info plugin is applied to the root project
project.getRootProject().getPluginManager().apply(GlobalBuildInfoPlugin.class);
// apply global test task failure listener
project.getRootProject().getPluginManager().apply(TestFailureReportingPlugin.class);

project.getPluginManager().apply(JavaPlugin.class);

configureConfigurations(project);
configureRepositories(project);
configureCompile(project);
Expand Down Expand Up @@ -545,4 +549,31 @@ private static void configureJavadoc(Project project) {
.named(LifecycleBasePlugin.CHECK_TASK_NAME)
.configure(t -> t.dependsOn(project.getTasks().withType(Javadoc.class)));
}

static class TestFailureReportingPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
if (project != project.getRootProject()) {
throw new IllegalStateException(this.getClass().getName() + " can only be applied to the root project.");
}

project.getGradle().addListener(new TaskActionListener() {
@Override
public void beforeActions(Task task) {}

@Override
public void afterActions(Task task) {
if (task instanceof Test) {
ErrorReportingTestListener listener = task.getExtensions().findByType(ErrorReportingTestListener.class);
if (listener != null && listener.getFailedTests().size() > 0) {
task.getLogger().lifecycle("\nTests with failures:");
for (ErrorReportingTestListener.Descriptor failure : listener.getFailedTests()) {
task.getLogger().lifecycle(" - " + failure.getFullName());
}
}
}
}
});
}
}
}

0 comments on commit ef48257

Please sign in to comment.