Skip to content

Commit 9927611

Browse files
rpalcoleawreulicke
andauthored
feat: show spotbugs report full path when spotbugs execution fails (#246)
* feat: show spotbugs report full path when spotbugs execution fails * Update src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForWorker.java Co-authored-by: wreulicke <[email protected]> * Update src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForWorker.java Co-authored-by: wreulicke <[email protected]> Co-authored-by: wreulicke <[email protected]>
1 parent 712b005 commit 9927611

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy

+34
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,38 @@ public class FooTest {
451451
result.output.contains("Applying com.h3xstream.findsecbugs.PredictableRandomDetector to FooTest")
452452
!result.output.contains("Trying to add already registered factory")
453453
}
454+
455+
@Unroll
456+
def 'shows report path when failures are found (Worker API? #isWorkerApi)'() {
457+
given:
458+
def badCode = new File(rootDir, 'src/main/java/Bar.java')
459+
badCode << '''
460+
|public class Bar {
461+
| public int unreadField = 42; // warning: URF_UNREAD_FIELD
462+
|}
463+
|'''.stripMargin()
464+
465+
when:
466+
def arguments = [':spotbugsMain']
467+
if(!isWorkerApi) {
468+
arguments.add('-Pcom.github.spotbugs.snom.worker=false')
469+
}
470+
def runner = GradleRunner.create()
471+
.withProjectDir(rootDir)
472+
.withArguments(arguments)
473+
.withPluginClasspath()
474+
.forwardOutput()
475+
.withGradleVersion(version)
476+
.withDebug(true)
477+
478+
def result = runner.buildAndFail()
479+
480+
then:
481+
result.task(':spotbugsMain').outcome == TaskOutcome.FAILED
482+
result.output.contains('SpotBugs report can be found in')
483+
result.output.contains('build/reports/spotbugs/main.xml')
484+
485+
where:
486+
isWorkerApi << [true, false]
487+
}
454488
}

src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForJavaExec.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
import com.github.spotbugs.snom.SpotBugsTask;
1717
import edu.umd.cs.findbugs.annotations.NonNull;
18+
import java.io.File;
1819
import java.util.ArrayList;
1920
import java.util.List;
21+
import java.util.stream.Collectors;
2022
import org.gradle.api.Action;
2123
import org.gradle.api.GradleException;
2224
import org.gradle.process.JavaExecSpec;
@@ -36,7 +38,15 @@ public void run(@NonNull SpotBugsTask task) {
3638
if (task.getIgnoreFailures()) {
3739
log.warn("SpotBugs reported failures", e);
3840
} else {
39-
throw new GradleException("Verification failed: SpotBugs execution thrown exception", e);
41+
String errorMessage = "Verification failed: SpotBugs execution thrown exception.";
42+
List<String> reportPaths =
43+
task.getReportsDir().getAsFileTree().getFiles().stream()
44+
.map(File::getAbsolutePath)
45+
.collect(Collectors.toList());
46+
if (!reportPaths.isEmpty()) {
47+
errorMessage += "SpotBugs report can be found in " + String.join(",", reportPaths);
48+
}
49+
throw new GradleException(errorMessage, e);
4050
}
4151
}
4252
}

src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunnerForWorker.java

+23-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import edu.umd.cs.findbugs.FindBugs2;
2020
import edu.umd.cs.findbugs.TextUICommandLine;
2121
import edu.umd.cs.findbugs.annotations.NonNull;
22+
import java.util.List;
2223
import java.util.Objects;
2324
import org.gradle.api.Action;
2425
import org.gradle.api.GradleException;
@@ -91,18 +92,36 @@ public void execute() {
9192
findBugs2.execute();
9293
if (findBugs2.getErrorCount() > 0) {
9394
throw new GradleException(
94-
"Verification failed: SpotBugs error found: " + findBugs2.getErrorCount());
95+
"Verification failed: SpotBugs error found: "
96+
+ findBugs2.getErrorCount()
97+
+ ". SpotBugs report can be found in "
98+
+ findReportPath());
9599
} else if (findBugs2.getBugCount() > 0) {
96100
throw new GradleException(
97-
"Verification failed: SpotBugs violation found: " + findBugs2.getBugCount());
101+
"Verification failed: SpotBugs violation found: "
102+
+ findBugs2.getBugCount()
103+
+ ". SpotBugs report can be found in "
104+
+ findReportPath());
98105
}
99106
}
100-
} catch (Exception e) {
107+
} catch (GradleException e) {
101108
if (params.getIgnoreFailures().getOrElse(Boolean.FALSE).booleanValue()) {
102109
log.warn("SpotBugs reported failures", e);
103110
} else {
104-
throw new GradleException("Verification failed: SpotBugs execution thrown exception", e);
111+
throw e;
105112
}
113+
} catch (Exception e) {
114+
throw new GradleException("Verification failed: SpotBugs execution thrown exception", e);
115+
}
116+
}
117+
118+
private String findReportPath() {
119+
List<String> arguments = getParameters().getArguments().get();
120+
int outputFileParameterIndex = arguments.indexOf("-outputFile");
121+
if (outputFileParameterIndex > 0) {
122+
return arguments.get(outputFileParameterIndex + 1);
123+
} else {
124+
return null;
106125
}
107126
}
108127
}

0 commit comments

Comments
 (0)