Skip to content

Commit

Permalink
feat: Improvements for HTML reporting (#127)
Browse files Browse the repository at this point in the history
* refs #111: add -release arg for better reporting

* refs #111: add -projectName arg for better reporting

* issue #107: support relative path for stylesheet (XSL)

* add CHANGELOG entry

* make each field transient

* reorder modifiers
  • Loading branch information
KengoTODA authored Mar 14, 2019
1 parent 3db2de4 commit 1e7ef13
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Currently the versioning policy of this project follows [Semantic Versioning](ht

## Unreleased - 2019-??-??

### Added

* Add `-projectName` option ([#111](https://github.com/spotbugs/spotbugs-gradle-plugin/issues/111))
* Add `-release` option ([#111](https://github.com/spotbugs/spotbugs-gradle-plugin/issues/111))
* Add `setStylesheet(String)` that accepts relative path from the root of `spotbugs.jar` ([#107](https://github.com/spotbugs/spotbugs-gradle-plugin/issues/107))

## 1.6.11 - 2019-03-10

### Changed
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/github/spotbugs/SpotBugsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.github.spotbugs.internal.spotbugs.SpotBugsSpecBuilder;
import com.github.spotbugs.internal.spotbugs.SpotBugsWorkerManager;

import edu.umd.cs.findbugs.annotations.NonNull;
import groovy.lang.Closure;

/**
Expand Down Expand Up @@ -90,6 +91,12 @@ public class SpotBugsTask extends SourceTask implements VerificationTask, Report

private Collection<String> jvmArgs = new ArrayList<>();

private String release;

private String projectName;

private SourceSet sourceSet;

@Nested
private final SpotBugsReportsInternal reports;

Expand Down Expand Up @@ -279,6 +286,8 @@ SpotBugsSpec generateSpec() {
.withExcludeFilter(getExcludeFilter())
.withIncludeFilter(getIncludeFilter())
.withExcludeBugsFilter(getExcludeBugsFilter())
.withRelease(getRelease())
.withProjectName(getProjectName())
.withExtraArgs(getExtraArgs())
.withJvmArgs(getJvmArgs())
.configureReports(getReports());
Expand Down Expand Up @@ -341,6 +350,7 @@ public SpotBugsTask jvmArgs(String... arguments) {

SpotBugsTask setSourceSet(SourceSet sourceSet) {
this.sourceDirs = sourceSet.getAllJava().getSrcDirs();
this.sourceSet = sourceSet;
setSource(sourceDirs);
return this;
}
Expand Down Expand Up @@ -667,4 +677,32 @@ public Collection<String> getJvmArgs() {
public void setJvmArgs(Collection<String> jvmArgs) {
this.jvmArgs = jvmArgs;
}

@NonNull
public String getRelease() {
if (release != null) {
return release;
}
return String.valueOf(getProject().getVersion());
}

public void setRelease(String release) {
this.release = release;
}

@NonNull
public String getProjectName() {
if (projectName != null) {
return projectName;
}
if (sourceSet == null) {
return String.valueOf(getProject().getDisplayName());
} else {
return String.format("%s (%s)", getProject().getDisplayName(), sourceSet.getName());
}
}

public void setProjectName(String projectName) {
this.projectName = projectName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.spotbugs.internal;

import java.io.File;
import java.util.Optional;

import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.logging.Logger;
import org.gradle.api.reporting.internal.CustomizableHtmlReportImpl;
import org.gradle.api.resources.ResourceHandler;
import org.gradle.api.resources.TextResource;
import org.gradle.api.resources.TextResourceFactory;
import org.gradle.api.tasks.Input;

public class SpotBugsHtmlReportImpl extends CustomizableHtmlReportImpl {
private static final long serialVersionUID = 6474874842199703745L;
private final transient ResourceHandler handler;
private final transient Configuration configuration;
private final transient Logger logger;

/**
* Null-able string representing relative file path of XSL packaged in spotbugs.jar.
*/
private String stylesheet;

public SpotBugsHtmlReportImpl(String name, Task task) {
super(name, task);
handler = task.getProject().getResources();
configuration = task.getProject().getConfigurations().getAt("spotbugs");
logger = task.getLogger();
}

@Input
public void setStylesheet(String fileName) {
this.stylesheet = fileName;
}

@Override
public TextResource getStylesheet() {
if (stylesheet == null) {
return super.getStylesheet();
}

TextResourceFactory factory = handler.getText();
Optional<File> spotbugs = configuration.files(this::find).stream().findFirst();
if (spotbugs.isPresent()) {
File jar = spotbugs.get();
logger.debug("Specified stylesheet ({}) found in spotbugs configuration: {}", stylesheet, jar.getAbsolutePath());
return factory.fromArchiveEntry(jar, stylesheet);
} else {
throw new InvalidUserDataException("Specified stylesheet (" + stylesheet + ") does not found in spotbugs configuration");
}
}

private boolean find(Dependency d) {
return "com.github.spotbugs".equals(d.getGroup()) && "spotbugs".equals(d.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.gradle.api.Task;
import org.gradle.api.reporting.SingleFileReport;
import org.gradle.api.reporting.internal.CustomizableHtmlReportImpl;
import org.gradle.api.reporting.internal.TaskGeneratedSingleFileReport;
import org.gradle.api.reporting.internal.TaskReportContainer;

Expand All @@ -11,11 +10,11 @@

public class SpotBugsReportsImpl extends TaskReportContainer<SingleFileReport> implements SpotBugsReportsInternal {

public SpotBugsReportsImpl(Task task) {
public SpotBugsReportsImpl(Task task) {
super(SingleFileReport.class, task);

add(SpotBugsXmlReportImpl.class, "xml", task);
add(CustomizableHtmlReportImpl.class, "html", task);
add(SpotBugsHtmlReportImpl.class, "html", task);
add(TaskGeneratedSingleFileReport.class, "text", task);
add(TaskGeneratedSingleFileReport.class, "emacs", task);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class SpotBugsSpecBuilder {
private Collection<String> jvmArgs;
private boolean showProgress;
private boolean debugEnabled;
private String release;
private String projectName;

public SpotBugsSpecBuilder(FileCollection classes) {
if (classes == null || classes.isEmpty()) {
Expand Down Expand Up @@ -151,6 +153,16 @@ public SpotBugsSpecBuilder withJvmArgs(@Nullable Collection<String> jvmArgs) {
return this;
}

public SpotBugsSpecBuilder withRelease(@Nullable String release) {
this.release = release;
return this;
}

public SpotBugsSpecBuilder withProjectName(@Nullable String projectName) {
this.projectName = projectName;
return this;
}

public SpotBugsSpec build() {
ArrayList<String> args = new ArrayList<>();
args.add("-pluginList");
Expand Down Expand Up @@ -234,6 +246,16 @@ public SpotBugsSpec build() {
args.add(excludeBugsFilter.getPath());
}

if (has(release)) {
args.add("-release");
args.add(release);
}

if (has(projectName)) {
args.add("-projectName");
args.add(projectName);
}

if (has(extraArgs)) {
args.addAll(extraArgs);
}
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/com/github/spotbugs/HtmlReportTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.spotbugs;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.stream.Collectors;

import org.gradle.testkit.runner.GradleRunner;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class HtmlReportTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Before
public void createProject() throws IOException {
Files.copy(Paths.get("src/test/resources/HtmlReportTest.gradle"), folder.getRoot().toPath().resolve("build.gradle"),
StandardCopyOption.COPY_ATTRIBUTES);

File sourceDir = folder.newFolder("src", "main", "java");
File to = new File(sourceDir, "Foo.java");
File from = new File("src/test/java/com/github/spotbugs/Foo.java");
Files.copy(from.toPath(), to.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
Files.write(folder.newFile("settings.gradle").toPath(), "rootProject.name = 'my project name'".getBytes());
}

@Test
public void testReportContainsVersion() throws Exception {
GradleRunner.create().withProjectDir(folder.getRoot())
.withArguments(Arrays.asList("spotbugsMain")).withPluginClasspath().build();
Path report = folder.getRoot().toPath().resolve("build").resolve("reports").resolve("spotbugs").resolve("main.html");
String html = Files.readAllLines(report).stream().collect(Collectors.joining("\n"));
assertThat(html, containsString("\"1.2.3\""));
}

@Test
public void testReportContainsProjectName() throws Exception {
GradleRunner.create().withProjectDir(folder.getRoot())
.withArguments(Arrays.asList("spotbugsMain")).withPluginClasspath().build();
Path report = folder.getRoot().toPath().resolve("build").resolve("reports").resolve("spotbugs").resolve("main.html");
String html = Files.readAllLines(report).stream().collect(Collectors.joining("\n"));
assertThat(html, containsString("'my project name' (main)"));
}

}
17 changes: 17 additions & 0 deletions src/test/resources/HtmlReportTest.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
id 'java'
id 'com.github.spotbugs'
}
version = '1.2.3'
repositories {
mavenCentral()
}
spotbugsMain {
reports {
xml.enabled false
html {
enabled true
stylesheet 'fancy-hist.xsl'
}
}
}

0 comments on commit 1e7ef13

Please sign in to comment.