From 15efde12c1b4a662f4a626238813199f12eb91d2 Mon Sep 17 00:00:00 2001 From: Kengo TODA Date: Tue, 23 Jun 2020 10:06:24 +0800 Subject: [PATCH] fix: Print report location as file uri and improve warning messages these changes came from #287 --- .../spotbugs/snom/ReportFunctionalTest.groovy | 2 +- .../snom/StandardFunctionalTest.groovy | 2 +- .../internal/SpotBugsRunnerForJavaExec.java | 8 ++- .../internal/SpotBugsRunnerForWorker.java | 55 ++++++++++--------- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/functionalTest/groovy/com/github/spotbugs/snom/ReportFunctionalTest.groovy b/src/functionalTest/groovy/com/github/spotbugs/snom/ReportFunctionalTest.groovy index 31d38e9a..fa7dcd9c 100644 --- a/src/functionalTest/groovy/com/github/spotbugs/snom/ReportFunctionalTest.groovy +++ b/src/functionalTest/groovy/com/github/spotbugs/snom/ReportFunctionalTest.groovy @@ -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"() { diff --git a/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy b/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy index 8997b706..455f6930 100644 --- a/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy +++ b/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy @@ -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) diff --git a/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForJavaExec.java b/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForJavaExec.java index d532586a..9a2e2e48 100644 --- a/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForJavaExec.java +++ b/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForJavaExec.java @@ -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; @@ -41,10 +43,12 @@ public void run(@NonNull SpotBugsTask task) { String errorMessage = "Verification failed: SpotBugs execution thrown exception."; List 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); } diff --git a/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForWorker.java b/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForWorker.java index ebd5aba3..62439fc3 100644 --- a/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForWorker.java +++ b/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForWorker.java @@ -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; @@ -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 arguments = getParameters().getArguments().get();