diff --git a/src/test/scala-3/com/sksamuel/scapegoat/DottyRunner.scala b/src/test/scala-3/com/sksamuel/scapegoat/DottyRunner.scala index 2357035d..6beabe17 100644 --- a/src/test/scala-3/com/sksamuel/scapegoat/DottyRunner.scala +++ b/src/test/scala-3/com/sksamuel/scapegoat/DottyRunner.scala @@ -5,6 +5,7 @@ import java.io.FileNotFoundException import java.nio.charset.StandardCharsets import java.nio.file.Files +import com.sksamuel.scapegoat.TestingScapegoatPlugin.emptyFeedback import dotty.tools.dotc.Driver import dotty.tools.dotc.core.Contexts import dotty.tools.dotc.core.Contexts.ContextBase @@ -22,10 +23,28 @@ class TestingScapegoatPlugin extends ScapegoatPlugin { phases } - def feedback: FeedbackDotty = scapegoatPhase.get.feedback.get + def feedback: FeedbackDotty = scapegoatPhase.get.feedback.getOrElse(emptyFeedback) } +object TestingScapegoatPlugin { + final val emptyFeedback: FeedbackDotty = FeedbackDotty( + Configuration( + dataDir = None, + disabledInspections = Nil, + enabledInspections = Nil, + ignoredFiles = Nil, + consoleOutput = false, + verbose = false, + reports = Reports(true, true, true, true, true), + customInspectors = Seq.empty, + sourcePrefix = "src/main/scala", + minimalLevel = Levels.Info, + overrideLevels = Map.empty + ) + )(using Contexts.NoContext) +} + class TestingScapegoat extends ContextBase with Plugins { private val scapegoat: TestingScapegoatPlugin = new TestingScapegoatPlugin @@ -36,7 +55,12 @@ class TestingScapegoat extends ContextBase with Plugins { def feedback: FeedbackDotty = scapegoat.feedback } -class DottyRunner(val inspection: Class[? <: Inspection]) extends Driver { +class DottyRunner( + val inspection: Class[? <: Inspection], + reports: String = "none", + disabledInspections: List[String] = List("none"), + createDataDir: () => File = DottyRunner.createTempDataDir +) extends Driver { private val dottyVersion: String = dotty.tools.dotc.config.Properties.versionNumberString private val scalaVersion: String = util.Properties.versionNumberString @@ -48,18 +72,19 @@ class DottyRunner(val inspection: Class[? <: Inspection]) extends Driver { override protected def initCtx: Contexts.Context = testingContext.initialCtx def compileCodeSnippet(source: String): FeedbackDotty = { - val targetDir = Files.createTempDirectory("scapegoat").toFile + val targetDir = createDataDir() val sourceFile = Files .write(Files.createTempFile("scapegoat_snippet", ".scala"), source.getBytes(StandardCharsets.UTF_8)) .toFile sourceFile.deleteOnExit() - targetDir.deleteOnExit() val _ = process( Array[String]( "-Xplugin-require:scapegoat", "-P:scapegoat:enabledInspections:" + inspection.getSimpleName, + "-P:scapegoat:disabledInspections:" + disabledInspections.mkString(":"), "-P:scapegoat:verbose:true", - "-P:scapegoat:reports:none", + s"-P:scapegoat:reports:$reports", + s"-P:scapegoat:dataDir:${targetDir.getAbsolutePath}", "-d", targetDir.getAbsolutePath, "-classpath", @@ -96,3 +121,13 @@ class DottyRunner(val inspection: Class[? <: Inspection]) extends Driver { } } + +object DottyRunner { + + def createTempDataDir(): File = { + val targetDir = Files.createTempDirectory("scapegoat").toFile + targetDir.deleteOnExit() + targetDir + } + +} diff --git a/src/test/scala-3/com/sksamuel/scapegoat/ScapegoatPhaseTest.scala b/src/test/scala-3/com/sksamuel/scapegoat/ScapegoatPhaseTest.scala index f575606c..aed1f6e5 100644 --- a/src/test/scala-3/com/sksamuel/scapegoat/ScapegoatPhaseTest.scala +++ b/src/test/scala-3/com/sksamuel/scapegoat/ScapegoatPhaseTest.scala @@ -1,99 +1,71 @@ package com.sksamuel.scapegoat -import java.io.File -import java.nio.file.Files +import java.io.{File, FilenameFilter} -import dotty.tools.dotc.core.Contexts.Context -import dotty.tools.dotc.core.Contexts.ContextBase +import com.sksamuel.scapegoat.inspections.option.OptionGet import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should class ScapegoatPhaseTest extends AnyFreeSpec with should.Matchers { - private def configuration(reports: Reports): (File, Configuration) = { - val tmpDir = Files.createTempDirectory("scapegoat").toFile - tmpDir.deleteOnExit() - val configuration = Configuration( - dataDir = Some(tmpDir), - disabledInspections = Nil, - enabledInspections = Nil, - ignoredFiles = Nil, - consoleOutput = false, - verbose = true, - reports = reports, - customInspectors = Nil, - sourcePrefix = "", - minimalLevel = Levels.Info, - overrideLevels = Map.empty + private def runDotty(report: String, disabledAll: Boolean = false): File = { + val targetFolder = DottyRunner.createTempDataDir() + val dotty = new DottyRunner( + classOf[OptionGet], + report, + if (disabledAll) List("all") else List("none"), + () => targetFolder ) - (tmpDir, configuration) + val _ = dotty.compileCodeSnippet("class Test {}") + targetFolder } - private val noReports = Reports( - disableXML = true, - disableHTML = true, - disableScalastyleXML = true, - disableMarkdown = true, - disableGitlabCodeQuality = true - ) + private val reportFilter = new FilenameFilter { + private val reportExtensions = "html" :: "xml" :: "md" :: "json" :: Nil + override def accept(dir: File, name: String): Boolean = reportExtensions.exists(name.endsWith) + } "ScapegoatPhase" - { "be disablable" in { - val (outputDir, config) = configuration(reports = noReports.copy(disableHTML = false)) - val phase = new ScapegoatPhase(config.copy(disabledInspections = List("all")), Nil) - implicit val ctx: Context = (new ContextBase).initialCtx - - val _ = phase.runOn(Nil) - - assert(outputDir.listFiles().size === 0) + val outputDir = runDotty("html", true) + outputDir.listFiles(reportFilter) should contain theSameElementsAs (Array.empty[File]) } "generate reports" - { "should generate html report" in { - val (outputDir, config) = configuration(reports = noReports.copy(disableHTML = false)) - val phase = new ScapegoatPhase(config, Nil) - implicit val ctx: Context = (new ContextBase).initialCtx - - val _ = phase.runOn(Nil) - - assert(new File(outputDir, "scapegoat.html").exists() === true) + val outputDir = runDotty("html") + outputDir.listFiles(reportFilter) should contain theSameElementsAs (Array( + new File(outputDir, "scapegoat.html") + )) } - "should generate xml report" in { - val (outputDir, config) = configuration(reports = noReports.copy(disableXML = false)) - val phase = new ScapegoatPhase(config, Nil) - implicit val ctx: Context = (new ContextBase).initialCtx - - val _ = phase.runOn(Nil) - assert(new File(outputDir, "scapegoat.xml").exists() === true) + "should generate xml report" in { + val outputDir = runDotty("xml") + outputDir.listFiles(reportFilter) should contain theSameElementsAs (Array( + new File(outputDir, "scapegoat.xml") + )) } - "should generate scalastyle report" in { - val (outputDir, config) = configuration(reports = noReports.copy(disableScalastyleXML = false)) - val phase = new ScapegoatPhase(config, Nil) - implicit val ctx: Context = (new ContextBase).initialCtx - val _ = phase.runOn(Nil) - - assert(new File(outputDir, "scapegoat-scalastyle.xml").exists() === true) + "should generate scalastyle report" in { + val outputDir = runDotty("scalastyle") + outputDir.listFiles(reportFilter) should contain theSameElementsAs (Array( + new File(outputDir, "scapegoat-scalastyle.xml") + )) } - "should generate markdown report" in { - val (outputDir, config) = configuration(reports = noReports.copy(disableMarkdown = false)) - val phase = new ScapegoatPhase(config, Nil) - implicit val ctx: Context = (new ContextBase).initialCtx - - val _ = phase.runOn(Nil) - assert(new File(outputDir, "scapegoat.md").exists() === true) + "should generate markdown report" in { + val outputDir = runDotty("markdown") + outputDir.listFiles(reportFilter) should contain theSameElementsAs (Array( + new File(outputDir, "scapegoat.md") + )) } - "should generate gitlab code quality report" in { - val (outputDir, config) = configuration(reports = noReports.copy(disableGitlabCodeQuality = false)) - val phase = new ScapegoatPhase(config, Nil) - implicit val ctx: Context = (new ContextBase).initialCtx - val _ = phase.runOn(Nil) - - assert(new File(outputDir, "scapegoat-gitlab.json").exists() === true) + "should generate gitlab code quality report" in { + val outputDir = runDotty("gitlab-codequality") + outputDir.listFiles(reportFilter) should contain theSameElementsAs (Array( + new File(outputDir, "scapegoat-gitlab.json") + )) } } }