-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improvements for HTML reporting (#127)
* 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
Showing
7 changed files
with
200 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/github/spotbugs/internal/SpotBugsHtmlReportImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
} |