Skip to content

Commit

Permalink
feat: support baseline file. Fixes #348
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Rocher authored and KengoTODA committed Nov 5, 2020
1 parent 50851dc commit 816fc7e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ spotbugs {
assertTrue(result.getOutput().contains(filter.getAbsolutePath()))
}

def "can use baselineFile"() {
setup:
File baseline = new File(rootDir, "baseline.xml")
buildFile << """
spotbugs {
baselineFile = file('baseline.xml')
}"""
baseline << """
<BugCollection></BugCollection>
"""
when:
def result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments('spotbugsMain', '--debug')
.withPluginClasspath()
.withGradleVersion(version)
.build()

then:
assertEquals(SUCCESS, result.task(":spotbugsMain").outcome)
assertTrue(result.getOutput().contains("-excludeBugs"))
assertTrue(result.getOutput().contains(baseline.getAbsolutePath()))
}

def "can use visitors"() {
setup:
buildFile << """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,73 @@ public class SimpleTest {
where:
isWorkerApi << [true, false]
}
@Unroll
def 'ignore bugs from baseline file (Worker API? #isWorkerApi)'() {
given:
def badCode = new File(rootDir, 'src/main/java/Bar.java')
badCode << '''
|public class Bar {
| public int unreadField = 42; // warning: URF_UNREAD_FIELD
|}
|'''.stripMargin()
def baseline = new File(rootDir, 'baseline.xml')
baseline << '''
<BugCollection version="4.1.1" sequence="0" timestamp="1602489053934" analysisTimestamp="1602489053968" release="1.0">
<BugInstance type="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD" priority="2" rank="18" abbrev="UrF" category="STYLE" instanceHash="94edf310851e6a92f2c3f91d60450ae9" instanceOccurrenceNum="0" instanceOccurrenceMax="0">
<ShortMessage>Unread public/protected field</ShortMessage>
<LongMessage>Unread public/protected field: Bar.unreadField</LongMessage>
<Class classname="Bar" primary="true">
<SourceLine classname="Bar" start="2" end="3" sourcefile="Bar.java" sourcepath="Bar.java" relSourcepath="java/Bar.java">
<Message>At Bar.java:[lines 2-3]</Message>
</SourceLine>
<Message>In class Bar</Message>
</Class>
<Field classname="Bar" name="unreadField" signature="I" isStatic="false" primary="true">
<SourceLine classname="Bar" sourcefile="Bar.java" sourcepath="Bar.java" relSourcepath="java/Bar.java">
<Message>In Bar.java</Message>
</SourceLine>
<Message>Field Bar.unreadField</Message>
</Field>
<SourceLine classname="Bar" primary="true" start="3" end="3" startBytecode="7" endBytecode="7" sourcefile="Bar.java" sourcepath="Bar.java" relSourcepath="java/Bar.java">
<Message>At Bar.java:[line 3]</Message>
</SourceLine>
</BugInstance>
<BugCategory category="STYLE">
<Description>Dodgy code</Description>
</BugCategory>
<BugPattern type="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD" abbrev="UrF" category="STYLE">
<ShortDescription>Unread public/protected field</ShortDescription>
</BugPattern>
<BugCode abbrev="UrF">
<Description>Champ non lu</Description>
</BugCode>
<Errors errors="0" missingClasses="0"></Errors>
</BugCollection>'''.stripMargin()
buildFile << """
spotbugs {
baselineFile = file('baseline.xml')
}"""
when:
def arguments = [':spotbugsMain']
if(!isWorkerApi) {
arguments.add('-Pcom.github.spotbugs.snom.worker=false')
}
def runner = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(arguments)
.withPluginClasspath()
.forwardOutput()
.withGradleVersion(version)
.withDebug(true)
def result = runner.build()
then:
result.task(':spotbugsMain').outcome == TaskOutcome.SUCCESS
where:
isWorkerApi << [true, false]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ class SpotBugsExtension {
*/
@NonNull
final RegularFileProperty excludeFilter;
/**
* Property to set the baseline file. This file is a Spotbugs result file, and all bugs reported in this file will not be
* reported in the final output.
*/
@NonNull
final RegularFileProperty baselineFile;
/**
* Property to specify the target classes for analysis. Default value is empty that means all classes are analyzed.
*/
Expand Down Expand Up @@ -162,6 +168,7 @@ class SpotBugsExtension {
reportsDir = objects.directoryProperty()
includeFilter = objects.fileProperty()
excludeFilter = objects.fileProperty()
baselineFile = objects.fileProperty()
onlyAnalyze = objects.listProperty(String);
projectName = objects.property(String);
release = objects.property(String);
Expand Down
11 changes: 11 additions & 0 deletions src/main/groovy/com/github/spotbugs/snom/SpotBugsTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ class SpotBugsTask extends DefaultTask implements VerificationTask {
@PathSensitive(PathSensitivity.RELATIVE)
@NonNull
final RegularFileProperty excludeFilter;
/**
* Property to set the baseline file. This file is a Spotbugs result file, and all bugs reported in this file will not be
* reported in the final output.
*/
@Optional
@InputFile
@PathSensitive(PathSensitivity.RELATIVE)
@NonNull
final RegularFileProperty baselineFile;
/**
* Property to specify the target classes for analysis. Default value is empty that means all classes are analyzed.
*/
Expand Down Expand Up @@ -306,6 +315,7 @@ class SpotBugsTask extends DefaultTask implements VerificationTask {
});
includeFilter = objects.fileProperty()
excludeFilter = objects.fileProperty()
baselineFile = objects.fileProperty()
onlyAnalyze = objects.listProperty(String);
projectName = objects.property(String);
release = objects.property(String);
Expand Down Expand Up @@ -333,6 +343,7 @@ class SpotBugsTask extends DefaultTask implements VerificationTask {
reportsDir.convention(extension.reportsDir)
includeFilter.convention(extension.includeFilter)
excludeFilter.convention(extension.excludeFilter)
baselineFile.convention(extension.baselineFile)
onlyAnalyze.convention(extension.onlyAnalyze)
projectName.convention(extension.projectName.map({p -> String.format("%s (%s)", p, getName())}))
release.convention(extension.release)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ protected List<String> buildArguments(SpotBugsTask task) {
args.add("-exclude");
args.add(task.getExcludeFilter().get().getAsFile().getAbsolutePath());
}
if (task.getBaselineFile().isPresent() && task.getBaselineFile().get() != null) {
args.add("-excludeBugs");
args.add(task.getBaselineFile().get().getAsFile().getAbsolutePath());
}
if (task.getOnlyAnalyze().isPresent() && !task.getOnlyAnalyze().get().isEmpty()) {
args.add("-onlyAnalyze");
args.add(task.getOnlyAnalyze().get().stream().collect(Collectors.joining(",")));
Expand Down

0 comments on commit 816fc7e

Please sign in to comment.