Skip to content

Commit

Permalink
fix: Print report location as file uri and improve warning messages
Browse files Browse the repository at this point in the history
these changes came from #287
  • Loading branch information
KengoTODA committed Jun 23, 2020
1 parent 4666f4f commit 15efde1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ buildDir = 'new-build-dir'

then:
//issue 284 - information on where the report should still be printed even if suppressing stack traces.
result.output.contains('SpotBugs report can be found in')
result.output.contains('See the report at')
}

def "can generate spotbugs.html in configured buildDir"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ public class SimpleTest {
then:
result.task(':spotbugsMain').outcome == TaskOutcome.FAILED
result.output.contains('SpotBugs report can be found in')
result.output.contains('See the report at')
def expectedOutput = File.separator + "build" + File.separator + "reports" + File.separator + "spotbugs" + File.separator + "main.xml"
result.output.contains(expectedOutput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.github.spotbugs.snom.SpotBugsTask;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -41,10 +43,12 @@ public void run(@NonNull SpotBugsTask task) {
String errorMessage = "Verification failed: SpotBugs execution thrown exception.";
List<String> reportPaths =
task.getReportsDir().getAsFileTree().getFiles().stream()
.map(File::getAbsolutePath)
.map(File::toPath)
.map(Path::toUri)
.map(URI::toString)
.collect(Collectors.toList());
if (!reportPaths.isEmpty()) {
errorMessage += "SpotBugs report can be found in " + String.join(",", reportPaths);
errorMessage += "See the report at: " + String.join(",", reportPaths);
}
throw new GradleException(errorMessage, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import edu.umd.cs.findbugs.TextUICommandLine;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import org.gradle.api.Action;
Expand Down Expand Up @@ -94,42 +95,42 @@ public void execute() {
TextUICommandLine commandLine = new TextUICommandLine();
FindBugs.processCommandLine(commandLine, args, findBugs2);
findBugs2.execute();

StringBuilder message = new StringBuilder();
if (findBugs2.getErrorCount() > 0) {
throw new GradleException(
"Verification failed: SpotBugs error found: "
+ findBugs2.getErrorCount()
+ ". "
+ buildMessageAboutReport());
} else if (findBugs2.getBugCount() > 0) {
throw new GradleException(
"Verification failed: SpotBugs violation found: "
+ findBugs2.getBugCount()
+ ". "
+ buildMessageAboutReport());
message.append(findBugs2.getErrorCount()).append(" SpotBugs errors were found.");
}
if (findBugs2.getBugCount() > 0) {
if (message.length() > 0) {
message.append(' ');
}
message.append(findBugs2.getBugCount()).append(" SpotBugs violations were found.");
}
if (message.length() > 0) {
String reportPath = findReportPath();
if (reportPath != null) {
message.append(" See the report at: ").append(Paths.get(reportPath).toUri());
}

GradleException e = new GradleException(message.toString());

if (params.getIgnoreFailures().getOrElse(Boolean.FALSE).booleanValue()) {
log.warn(message.toString());
if (params.getShowStackTraces().getOrElse(Boolean.TRUE).booleanValue()) {
log.warn("", e);
}
} else {
throw e;
}
}
}
} catch (GradleException e) {
if (params.getIgnoreFailures().getOrElse(Boolean.FALSE).booleanValue()) {
final boolean showStackTraces =
params.getShowStackTraces().getOrElse(Boolean.TRUE).booleanValue();
log.warn("SpotBugs reported failures", showStackTraces ? e : e.getMessage());
} else {
throw e;
}
throw e;
} catch (Exception e) {
throw new GradleException("Verification failed: SpotBugs execution thrown exception", e);
}
}

private String buildMessageAboutReport() {
String reportPath = findReportPath();
if (reportPath != null) {
return "SpotBugs report can be found in " + reportPath;
} else {
return "";
}
}

@CheckForNull
private String findReportPath() {
List<String> arguments = getParameters().getArguments().get();
Expand Down

0 comments on commit 15efde1

Please sign in to comment.