Skip to content

Commit

Permalink
feat: print report to console when reporting is not specified
Browse files Browse the repository at this point in the history
close #363
close #172

BREAKING CHANGE: Behavior without report config has been changed
from XML to console
  • Loading branch information
KengoTODA committed Sep 17, 2021
1 parent 7f350c0 commit b78a1b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class CacheabilityFunctionalTest extends Specification {
|repositories {
| mavenCentral()
|}
|spotbugsMain {
| reports {
| text.enabled = true
| }
|}
|'''.stripMargin()

if (runScan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import spock.lang.Specification

import org.gradle.testkit.runner.GradleRunner

import static org.gradle.testkit.runner.TaskOutcome.FAILED
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
import static org.junit.jupiter.api.Assertions.assertEquals
import static org.junit.jupiter.api.Assertions.assertFalse
Expand Down Expand Up @@ -58,19 +59,26 @@ public class Foo {
"""
}

def "generate XML report by default"() {
def "generate console report by default"() {
given:
def badCode = new File(rootDir, 'src/main/java/Bar.java')
badCode << '''
|public class Bar {
| public int unreadField = 42; // warning: URF_UNREAD_FIELD
|}
|'''.stripMargin()
when:
def result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments('spotbugsMain')
.withPluginClasspath()
.withGradleVersion(version)
.build()
.buildAndFail()

then:
assertEquals(SUCCESS, result.task(":spotbugsMain").outcome)
File report = rootDir.toPath().resolve("build").resolve("reports").resolve("spotbugs").resolve("main.xml").toFile()
report.isFile()
assertEquals(FAILED, result.task(":spotbugsMain").outcome)
assertTrue(result.output.contains("M D UrF: Unread public/protected field: Bar.unreadField At Bar.java:[line 3]"))
assertFalse(rootDir.toPath().resolve("build").toFile().list().contains("reports"))
}

def "can generate spotbugs.txt"() {
Expand Down Expand Up @@ -468,4 +476,4 @@ spotbugsMain {
File report = rootDir.toPath().resolve("build").resolve("reports").resolve("spotbugs").resolve("main.sarif").toFile()
assertTrue(report.isFile())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,13 @@ public class SimpleTest {
@Unroll
def 'shows report path when failures are found (Worker API? #isWorkerApi)'() {
given:
buildFile << """
spotbugsMain {
reports {
xml.enabled = true
}
}"""
def badCode = new File(rootDir, 'src/main/java/Bar.java')
badCode << '''
|public class Bar {
Expand Down
6 changes: 1 addition & 5 deletions src/main/groovy/com/github/spotbugs/snom/SpotBugsTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,7 @@ class SpotBugsTask extends DefaultTask implements VerificationTask {
@Nested
SpotBugsReport getFirstEnabledReport() {
java.util.Optional<SpotBugsReport> report = reports.stream().filter({ report -> report.enabled}).findFirst()
if (isSpotBugsPluginApplied) {
return report.orElse(reports.create("xml"))
} else {
return report.orElse(null)
}
return report.orElse(null)
}

void setReportLevel(@Nullable String name) {
Expand Down

0 comments on commit b78a1b3

Please sign in to comment.