Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Allow other plugins to register tasks to produce junit reports #1764

Merged
merged 9 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.palantir.gradle.junit.JunitReportsExtension;
import com.palantir.gradle.junit.JunitReportsPlugin;
import com.palantir.gradle.junit.JunitReportsRootPlugin;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -39,7 +39,7 @@ public final class BaselineCircleCi implements Plugin<Project> {

@Override
public void apply(Project project) {
project.getPluginManager().apply(JunitReportsPlugin.class);
project.getPluginManager().apply(JunitReportsRootPlugin.class);

configurePluginsForReports(project);
configurePluginsForArtifacts(project);
Expand Down
3 changes: 3 additions & 0 deletions gradle-junit-reports/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ dependencies {
compile gradleApi()
compile 'com.google.guava:guava'

annotationProcessor 'org.immutables:value'
annotationProcessor 'org.inferred:freebuilder'

compileOnly 'org.immutables:value::annotations'
compileOnly 'org.inferred:freebuilder'

testCompile 'com.google.guava:guava'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@
import org.gradle.api.file.DirectoryProperty;

public class JunitReportsExtension {
private static final String EXT_JUNIT_REPORTS = "junitReports";

private final DirectoryProperty reportsDirectory;
private final TaskTimer timer;

public JunitReportsExtension(Project project) {
this.reportsDirectory = project.getObjects().directoryProperty();
reportsDirectory.set(project.getLayout().getBuildDirectory().dir("junit-reports"));
static JunitReportsExtension register(Project project) {
TaskTimer timer = new StyleTaskTimer();
project.getGradle().addListener(timer);
return project.getExtensions().create(EXT_JUNIT_REPORTS, JunitReportsExtension.class, project, timer);
}

public JunitReportsExtension(Project project, TaskTimer timer) {
this.reportsDirectory = project.getObjects()
.directoryProperty()
.value(project.getLayout().getBuildDirectory().dir("junit-reports"));
this.timer = timer;
}

public final DirectoryProperty getReportsDirectory() {
return reportsDirectory;
}

public final TaskTimer getTimer() {
return this.timer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,38 @@
import javax.inject.Inject;
import javax.xml.transform.TransformerException;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.util.GUtil;
import org.w3c.dom.Document;

public class JunitReportsFinalizer extends DefaultTask {

public static void registerFinalizer(
Task task, TaskTimer timer, FailuresSupplier failuresSupplier, Provider<Directory> reportDir) {
JunitReportsFinalizer finalizer = Tasks.createTask(
task.getProject().getTasks(), task.getName() + "CircleFinalizer", JunitReportsFinalizer.class);
if (finalizer == null) {
// Already registered (happens if the user applies us to the root project and subprojects)
return;
}
finalizer.setStyleTask(task);
finalizer.setTaskTimer(timer);
finalizer.setFailuresSupplier(failuresSupplier);
finalizer
.getTargetFile()
.set(reportDir.map(dir -> dir.file(task.getProject().getName() + "-" + task.getName() + ".xml")));
finalizer.getReportDir().set(reportDir);

task.finalizedBy(finalizer);
Project project,
String taskName,
TaskTimer timer,
FailuresSupplier failuresSupplier,
Provider<Directory> reportDir) {
TaskProvider<JunitReportsFinalizer> finalizer = project.getTasks()
.register(GUtil.toLowerCamelCase(taskName + " CircleFinalizer"), JunitReportsFinalizer.class, task -> {
ferozco marked this conversation as resolved.
Show resolved Hide resolved
task.setTaskTimer(timer);
task.setFailuresSupplier(failuresSupplier);
task.getTargetFile()
.set(reportDir.map(dir -> dir.file(project.getName() + "-" + taskName + ".xml")));
task.getReportDir().set(reportDir);
});

project.getTasks().named(taskName, task -> {
task.finalizedBy(finalizer);
});
}

private Task styleTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

import com.google.common.base.Splitter;
import java.io.File;
import java.nio.file.Path;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFile;
import org.gradle.api.plugins.quality.Checkstyle;
Expand All @@ -31,50 +29,33 @@

public final class JunitReportsPlugin implements Plugin<Project> {

public static final String EXT_JUNIT_REPORTS = "junitReports";

@Override
@SuppressWarnings("Slf4jLogsafeArgs")
public void apply(Project project) {
if (project != project.getRootProject()) {
project.getLogger()
.warn(
"com.palantir.junit-reports should be applied to the root project only, not '{}'",
project.getName());
}

JunitReportsExtension reportsExtension =
project.getExtensions().create(EXT_JUNIT_REPORTS, JunitReportsExtension.class, project);
JunitReportsExtension rootExt = project.getRootProject().getExtensions().getByType(JunitReportsExtension.class);
JunitTaskResultExtension ext = JunitTaskResultExtension.register(project);

configureBuildFailureFinalizer(project.getRootProject(), reportsExtension.getReportsDirectory());
project.getTasks().withType(Test.class, test -> {
test.getReports().getJunitXml().setEnabled(true);
test.getReports().getJunitXml().setDestination(junitPath(rootExt.getReportsDirectory(), test.getPath()));
});

TaskTimer timer = new StyleTaskTimer();
project.getRootProject().getGradle().addListener(timer);
project.getTasks().withType(Checkstyle.class, checkstyle -> {
ext.registerTask(
checkstyle.getName(), XmlReportFailuresSupplier.create(checkstyle, new CheckstyleReportHandler()));
});

project.getRootProject().allprojects(proj -> {
proj.getTasks().withType(Test.class, test -> {
test.getReports().getJunitXml().setEnabled(true);
test.getReports()
.getJunitXml()
.setDestination(junitPath(reportsExtension.getReportsDirectory(), test.getPath()));
});
proj.getTasks()
.withType(
Checkstyle.class,
checkstyle -> JunitReportsFinalizer.registerFinalizer(
checkstyle,
timer,
XmlReportFailuresSupplier.create(checkstyle, new CheckstyleReportHandler()),
reportsExtension.getReportsDirectory().map(dir -> dir.dir("checkstyle"))));
proj.getTasks()
.withType(
JavaCompile.class,
javac -> JunitReportsFinalizer.registerFinalizer(
javac,
timer,
JavacFailuresSupplier.create(javac),
reportsExtension.getReportsDirectory().map(dir -> dir.dir("javac"))));
project.getTasks().withType(JavaCompile.class, javac -> {
ext.registerTask(javac.getName(), JavacFailuresSupplier.create(javac));
});

ext.getTaskEntries()
.configureEach(entry -> JunitReportsFinalizer.registerFinalizer(
project,
entry.taskName(),
rootExt.getTimer(),
entry.failuresSupplier(),
rootExt.getReportsDirectory()));
}

private static Provider<File> junitPath(DirectoryProperty basePath, String testPath) {
Expand All @@ -83,20 +64,4 @@ private static Provider<File> junitPath(DirectoryProperty basePath, String testP
dir.file(String.join(File.separator, Splitter.on(':').splitToList(testPath.substring(1)))))
.map(RegularFile::getAsFile);
}

private static void configureBuildFailureFinalizer(Project rootProject, Provider<Directory> reportsDir) {
Provider<RegularFile> targetFileProvider = reportsDir.map(dir -> {
int attemptNumber = 1;
Path targetFile = dir.getAsFile().toPath().resolve("gradle").resolve("build.xml");
while (targetFile.toFile().exists()) {
targetFile = dir.getAsFile().toPath().resolve("gradle").resolve("build" + ++attemptNumber + ".xml");
}
return dir.file(targetFile.toAbsolutePath().toString());
});

BuildFailureListener listener = new BuildFailureListener();
BuildFinishedAction action = new BuildFinishedAction(targetFileProvider, listener);
rootProject.getGradle().addListener(listener);
rootProject.getGradle().buildFinished(action);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.gradle.junit;

import com.google.common.base.Preconditions;
import java.nio.file.Path;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.file.Directory;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;

public final class JunitReportsRootPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
Preconditions.checkState(
project.getRootProject().equals(project), "Plugin must only be applied to root project");

JunitReportsExtension reportsExtension = JunitReportsExtension.register(project);
configureBuildFailureFinalizer(project.getRootProject(), reportsExtension.getReportsDirectory());
}

private static void configureBuildFailureFinalizer(Project rootProject, Provider<Directory> reportsDir) {
Provider<RegularFile> targetFileProvider = reportsDir.map(dir -> {
int attemptNumber = 1;
Path targetFile = dir.getAsFile().toPath().resolve("gradle").resolve("build.xml");
while (targetFile.toFile().exists()) {
targetFile = dir.getAsFile().toPath().resolve("gradle").resolve("build" + ++attemptNumber + ".xml");
}
return dir.file(targetFile.toAbsolutePath().toString());
});

BuildFailureListener listener = new BuildFailureListener();
BuildFinishedAction action = new BuildFinishedAction(targetFileProvider, listener);
rootProject.getGradle().addListener(listener);
rootProject.getGradle().buildFinished(action);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.gradle.junit;

import org.gradle.api.NamedDomainObjectSet;
import org.gradle.api.Project;
import org.gradle.api.model.ObjectFactory;
import org.immutables.value.Value.Immutable;

public class JunitTaskResultExtension {
static final String NAME = "junitTaskResults";
private final NamedDomainObjectSet<TaskEntry> taskEntries;

static JunitTaskResultExtension register(Project project) {
return project.getExtensions().create(NAME, JunitTaskResultExtension.class);
}

public JunitTaskResultExtension(ObjectFactory objects) {
this.taskEntries = objects.namedDomainObjectSet(TaskEntry.class);
}

public final NamedDomainObjectSet<TaskEntry> getTaskEntries() {
return taskEntries;
}

public final void registerTask(String taskName, FailuresSupplier failuresSupplier) {
ferozco marked this conversation as resolved.
Show resolved Hide resolved
taskEntries.add(TaskEntry.of(taskName, failuresSupplier));
}

@Immutable
interface TaskEntry {

String taskName();

FailuresSupplier failuresSupplier();

static TaskEntry of(String taskName, FailuresSupplier failuresSupplier) {
return ImmutableTaskEntry.builder()
.taskName(taskName)
.failuresSupplier(failuresSupplier)
.build();
}
}
}