Skip to content

Commit

Permalink
Fix ScalafixImplSuite test and add a test for configuration path (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlachkar authored Aug 28, 2020
1 parent ab96028 commit be5dcc8
Showing 1 changed file with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import java.nio.file.Files
import java.nio.file.Paths
import java.util.Collections
import java.util.Optional

import coursier._
import org.scalatest.funsuite.AnyFunSuite

import scala.collection.JavaConverters._
import buildinfo.RulesBuildInfo

import scala.meta.io.AbsolutePath
import scala.meta.io.Classpath
import scalafix.Versions
Expand All @@ -21,9 +25,12 @@ import scalafix.interfaces.ScalafixMainCallback
import scalafix.interfaces.ScalafixMainMode
import scalafix.internal.reflect.ClasspathOps
import scalafix.internal.reflect.RuleCompiler
import scalafix.internal.rule.ProcedureSyntax
import scalafix.test.StringFS
import scalafix.testkit.DiffAssertions
import scalafix.tests.util.ScalaVersions
import scalafix.{interfaces => i}

import scala.util.Properties

class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
Expand Down Expand Up @@ -97,6 +104,47 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
assert(e.isPresent)
assert(e.get().getMessage.contains("-Ywarn-unused"))
}
test("rulesThatWillRun") {
val api = i.Scalafix.classloadInstance(this.getClass.getClassLoader)

val charset = StandardCharsets.US_ASCII
val cwd = StringFS
.string2dir(
"""|/src/Semicolon.scala
|
|object Semicolon {
| def main { println(42) }
|}
|/.scalafix.conf
|rules = ["DisableSyntax"]
""".stripMargin,
charset
)
val args = api
.newArguments()
.withConfig(Optional.empty())
.withWorkingDirectory(cwd.toNIO)
args.validate()
assert(
args.rulesThatWillRun().asScala.toList.map(_.toString) == List(
"ScalafixRule(DisableSyntax)"
)
)

//if a non empty list of rules is provided, rules from config file are ignored
val args2 = api
.newArguments()
.withRules(List("ProcedureSyntax").asJava)
.withConfig(Optional.empty())
.withWorkingDirectory(cwd.toNIO)
args2.validate()
assert(
args2.rulesThatWillRun().asScala.toList.map(_.name()) == List(
"ProcedureSyntax"
)
)

}

test("runMain") {
// This is a full integration test that stresses the full breadth of the scalafix-interfaces API
Expand Down Expand Up @@ -126,9 +174,19 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
Files.createDirectories(d)
val semicolon = src.resolve("Semicolon.scala")
val excluded = src.resolve("Excluded.scala")
val scalaBinaryVersion =
RulesBuildInfo.scalaVersion.split('.').take(2).mkString(".")
// This rule is published to Maven Central to simplify testing --tool-classpath.
val dep =
Dependency(
Module(
Organization("ch.epfl.scala"),
ModuleName(s"example-scalafix-rule_$scalaBinaryVersion")
),
"1.6.0"
)
val toolClasspathJars = Fetch()
.addDependencies(dep"com.geirsson:example-scalafix-rule_2.12:1.1.0")
.addDependencies(dep)
.run()
.toList
val toolClasspath = ClasspathOps.toClassLoader(
Expand All @@ -147,7 +205,6 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
excluded.toString
)
val compileSucceeded = scala.tools.nsc.Main.process(scalacOptions)
assert(compileSucceeded)
val buf = List.newBuilder[ScalafixDiagnostic]
val callback = new ScalafixMainCallback {
override def reportDiagnostic(diagnostic: ScalafixDiagnostic): Unit = {
Expand All @@ -156,6 +213,10 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
}
val out = new ByteArrayOutputStream()
val relativePath = cwd.relativize(semicolon)
val warnRemoveUnused =
if (ScalaVersions.isScala213)
"-Wunused:imports"
else "-Ywarn-unused-import"
val args = api
.newArguments()
.withParsedArguments(
Expand Down Expand Up @@ -183,7 +244,7 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
.withPrintStream(new PrintStream(out))
.withMode(ScalafixMainMode.CHECK)
.withToolClasspath(toolClasspath)
.withScalacOptions(Collections.singletonList("-Ywarn-unused-import"))
.withScalacOptions(Collections.singletonList(warnRemoveUnused))
.withScalaVersion(Properties.versionNumberString)
.withConfig(Optional.empty())
val expectedRulesToRun = List(
Expand Down Expand Up @@ -243,5 +304,4 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
|""".stripMargin
)
}

}

0 comments on commit be5dcc8

Please sign in to comment.