Skip to content

Commit

Permalink
Only report warnings via the reporter (#326)
Browse files Browse the repository at this point in the history
* Only report warnings via the reporter

* Fix tests

* Enable reports during compilation by default
  • Loading branch information
avdv authored Mar 29, 2020
1 parent e947418 commit 0e80c05
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
18 changes: 9 additions & 9 deletions src/main/scala/com/sksamuel/scapegoat/Feedback.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ class Feedback(
inspection = inspection.getClass.getCanonicalName
)
warningsBuffer.append(warning)

if (shouldPrint(warning)) {
println(s"[${warning.level.toString.toLowerCase}] $sourceFileNormalized:${warning.line}: $text")
println(s" $explanation")
snippet.foreach(s => println(s" $s"))
println()
}
val snippetText = snippet.fold("")("\n " + _ + "\n")
val report = s"""|[scapegoat] $text
| $explanation$snippetText""".stripMargin

adjustedLevel match {
case Levels.Error => reporter.error(pos, text)
case Levels.Warning => reporter.warning(pos, text)
case Levels.Info => reporter.info(pos, text, force = false)
adjustedLevel match {
case Levels.Error => reporter.error(pos, report)
case Levels.Warning => reporter.warning(pos, report)
case Levels.Info => reporter.info(pos, report, force = false)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/sksamuel/scapegoat/plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class ScapegoatComponent(val global: Global, inspections: Seq[Inspection])
var disabledInspections: List[String] = Nil
var enabledInspections: List[String] = Nil
var ignoredFiles: List[String] = Nil
var consoleOutput: Boolean = false
var consoleOutput: Boolean = true
var verbose: Boolean = false
val debug: Boolean = false
var summary: Boolean = true
Expand Down
21 changes: 14 additions & 7 deletions src/test/scala/com/sksamuel/scapegoat/FeedbackTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class FeedbackTest extends AnyFreeSpec with Matchers with OneInstancePerTest wit
"This is explanation."
)
val reporter = new StoreReporter
val feedback = new Feedback(false, reporter, defaultSourcePrefix)
val feedback = new Feedback(true, reporter, defaultSourcePrefix)
feedback.warn(position, inspection)
reporter.infos should contain(reporter.Info(position, "My default is Error", reporter.ERROR))
reporter.infos should contain(
reporter.Info(position, "[scapegoat] My default is Error\n This is explanation.", reporter.ERROR)
)
}
"for warning" in {
val inspection = new DummyInspection(
Expand All @@ -44,9 +46,12 @@ class FeedbackTest extends AnyFreeSpec with Matchers with OneInstancePerTest wit
"This is explanation."
)
val reporter = new StoreReporter
val feedback = new Feedback(false, reporter, defaultSourcePrefix)
val feedback = new Feedback(true, reporter, defaultSourcePrefix)
feedback.warn(position, inspection)
reporter.infos should contain(reporter.Info(position, "My default is Warning", reporter.WARNING))
reporter.infos should contain(
reporter
.Info(position, "[scapegoat] My default is Warning\n This is explanation.", reporter.WARNING)
)
}
"for info" in {
val inspection = new DummyInspection(
Expand All @@ -56,9 +61,11 @@ class FeedbackTest extends AnyFreeSpec with Matchers with OneInstancePerTest wit
"This is explanation."
)
val reporter = new StoreReporter
val feedback = new Feedback(false, reporter, defaultSourcePrefix)
val feedback = new Feedback(true, reporter, defaultSourcePrefix)
feedback.warn(position, inspection)
reporter.infos should contain(reporter.Info(position, "My default is Info", reporter.INFO))
reporter.infos should contain(
reporter.Info(position, "[scapegoat] My default is Info\n This is explanation.", reporter.INFO)
)
}
}
"should use proper paths" - {
Expand Down Expand Up @@ -111,7 +118,7 @@ class FeedbackTest extends AnyFreeSpec with Matchers with OneInstancePerTest wit
)
val inspections = Seq(inspectionError, inspectionWarning, inspectionInfo)
val reporter = new StoreReporter
val feedback = new Feedback(false, reporter, defaultSourcePrefix, Levels.Info)
val feedback = new Feedback(true, reporter, defaultSourcePrefix, Levels.Info)
inspections.foreach(inspection => feedback.warn(position, inspection))
feedback.warningsWithMinimalLevel.length should be(3)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.sksamuel.scapegoat.inspections.collections

import com.sksamuel.scapegoat.PluginRunner
import org.scalatest.OneInstancePerTest
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

class MapGetAndGetOrElseTest extends AnyFreeSpec with Matchers with PluginRunner {
class MapGetAndGetOrElseTest extends AnyFreeSpec with Matchers with PluginRunner with OneInstancePerTest {

override val inspections = Seq(new MapGetAndGetOrElse)

Expand Down

0 comments on commit 0e80c05

Please sign in to comment.