Skip to content

Commit

Permalink
Fix ScalafixImplSuite test and add a test for configuration path
Browse files Browse the repository at this point in the history
  • Loading branch information
mlachkar committed Aug 12, 2020
1 parent 8174093 commit 2647d71
Showing 1 changed file with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 @@ -23,6 +24,7 @@ import scalafix.internal.reflect.ClasspathOps
import scalafix.internal.reflect.RuleCompiler
import scalafix.test.StringFS
import scalafix.testkit.DiffAssertions
import scalafix.tests.util.ScalaVersions
import scalafix.{interfaces => i}
import scala.util.Properties

Expand Down Expand Up @@ -97,6 +99,47 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
assert(e.isPresent)
assert(e.get().getMessage.contains("-Ywarn-unused"))
}
test("configure scalafix path") {
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 not read.
val args2 = api
.newArguments()
.withRules(List("ProcedureSyntax").asJava)
.withConfig(Optional.empty())
.withWorkingDirectory(cwd.toNIO)
args2.validate()
assert(
args2.rulesThatWillRun().asScala.toList.map(_.toString) == List(
"ScalafixRule(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 +169,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 +200,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 +208,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 +239,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 +299,4 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
|""".stripMargin
)
}

}

0 comments on commit 2647d71

Please sign in to comment.