Skip to content

Commit

Permalink
testkit: add targetroot to classpath based on scalacOptions
Browse files Browse the repository at this point in the history
* client shouldn't have to make semanticdb information available in
  the inputClasspath
* rely args.validatedClasspath for proper Scala 3 support
  • Loading branch information
github-brice-jaglin committed May 16, 2021
1 parent d0f461c commit 20b3be8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
8 changes: 5 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ lazy val unit = project
}
put(
"inputClasspath",
(testsInput / Compile / fullClasspath).value.map(_.data) :+
(testsInput / Compile / semanticdbTargetRoot).value
(testsInput / Compile / fullClasspath).value.map(_.data)
)
put(
"inputSourceDirectories",
Expand Down Expand Up @@ -246,7 +245,10 @@ lazy val unit = project
"testsInputResources" ->
(testsInput / Compile / sourceDirectory).value / "resources",
"semanticClasspath" ->
(testsShared / Compile / semanticdbTargetRoot).value,
Seq(
(testsInput / Compile / semanticdbTargetRoot).value,
(testsShared / Compile / semanticdbTargetRoot).value
),
"sharedSourceroot" ->
(ThisBuild / baseDirectory).value /
"scalafix-tests" / "shared" / "src" / "main",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ object ClasspathOps {
new URLClassLoader(url +: getURLs(classLoader), classLoader)
}

def thisClassLoaderWith(urls: Seq[URL]): URLClassLoader = {
val classLoader = this.getClass.getClassLoader
new URLClassLoader(urls.toArray ++ getURLs(classLoader), classLoader)
}

def thisClasspath: Classpath = {
Classpath(
getURLs(this.getClass().getClassLoader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import org.scalatest.BeforeAndAfterAll
import org.scalatest.Suite
import org.scalatest.TestRegistration
import org.scalatest.exceptions.TestFailedException
import scalafix.internal.config.ScalaVersion
import scalafix.internal.patch.PatchInternals
import scalafix.internal.reflect.ClasspathOps
import scalafix.internal.testkit.AssertDiff
import scalafix.internal.testkit.CommentAssertion
import scalafix.internal.v1.Args

/**
* Construct a test suite for running semantic Scalafix rules.
Expand Down Expand Up @@ -98,8 +100,13 @@ abstract class AbstractSemanticRuleSuite(
}

lazy val testsToRun: List[RuleTest] = {
val args = Args.default.copy(
scalaVersion = ScalaVersion.from(props.scalaVersion).get,
scalacOptions = props.scalacOptions,
classpath = props.inputClasspath
)
val symtab = ClasspathOps.newSymbolTable(props.inputClasspath)
val classLoader = ClasspathOps.toClassLoader(props.inputClasspath)
val classLoader = ClasspathOps.toClassLoader(args.validatedClasspath)
val tests = TestkitPath.fromProperties(props)
tests.map { test =>
RuleTest.fromPath(props, test, classLoader, symtab)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import java.nio.file.SimpleFileVisitor
import java.nio.file.StandardCopyOption
import java.nio.file.attribute.BasicFileAttributes

import scala.collection.immutable.Seq

import scala.meta.internal.io.FileIO
import scala.meta.io.AbsolutePath
import scala.meta.io.RelativePath
Expand All @@ -23,6 +21,7 @@ import scalafix.test.StringFS
import scalafix.testkit.DiffAssertions
import scalafix.testkit.SemanticRuleSuite
import scalafix.testkit.TestkitProperties
import scalafix.tests.BuildInfo
import scalafix.tests.util.ScalaVersions
import scalafix.v1.Main

Expand Down Expand Up @@ -179,6 +178,8 @@ trait BaseCliSuite extends AnyFunSuite with DiffAssertions {
def checkSemantic(
name: String,
args: Array[String],
targetroots: Seq[String] =
BuildInfo.semanticClasspath.map(_.getAbsolutePath),
expectedExit: ExitStatus,
preprocess: AbsolutePath => Unit = _ => (),
outputAssert: String => Unit = _ => (),
Expand Down Expand Up @@ -207,11 +208,13 @@ trait BaseCliSuite extends AnyFunSuite with DiffAssertions {
val sourceroot =
if (args.contains("--sourceroot")) Array[String]()
else Array("--sourceroot", cwd.toString)
val targetroots0 =
targetroots.flatMap(Seq("--semanticdb-targetroots", _))
val scalaOption =
if (ScalaVersions.isScala213)
"-Wunused:imports"
else "-Ywarn-unused-import"
val allArguments = args ++ sourceroot ++ Seq(
val allArguments = args ++ sourceroot ++ targetroots0 ++ Seq(
"--scalac-options",
scalaOption,
"-r",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import scala.meta.io.Classpath
import scala.meta.testkit.StringFS

import scalafix.cli._
import scalafix.tests.BuildInfo
import scalafix.tests.core.Classpaths

class CliSemanticSuite extends BaseCliSuite {
Expand Down Expand Up @@ -50,6 +51,7 @@ class CliSemanticSuite extends BaseCliSuite {
checkSemantic(
name = "missing --classpath",
args = Array(),
targetroots = Seq.empty,
expectedExit = ExitStatus.MissingSemanticdbError
)

Expand Down Expand Up @@ -170,13 +172,13 @@ class CliSemanticSuite extends BaseCliSuite {
checkSemantic(
name = "-P:semanticdb:targetroot",
args = {
val (_ :: targetroot :: Nil, jars) =
props.inputClasspath.entries.partition(_.isDirectory)
val jars = props.inputClasspath.entries.filter(_.isDirectory)
val targetroot = BuildInfo.semanticClasspath.last.getAbsolutePath
Array(
s"--scalacOptions",
s"-P:semanticdb:targetroot:shouldBeIgnored",
s"--scalacOptions",
s"-P:semanticdb:targetroot:${targetroot.toString()}",
s"-P:semanticdb:targetroot:$targetroot",
"--classpath",
Classpath(jars).syntax
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ object BaseSemanticSuite {
SemanticDocument.fromPath(
doc,
relpath,
ClasspathOps.thisClassLoaderWith(BuildInfo.semanticClasspath.toURI.toURL),
ClasspathOps.thisClassLoaderWith(
BuildInfo.semanticClasspath.map(_.toURI.toURL)
),
symtab
)
}
Expand All @@ -50,9 +52,10 @@ abstract class BaseSemanticSuite(filename: String)
}

override def beforeAll(): Unit = {
val dir = AbsolutePath(scalafix.tests.BuildInfo.semanticClasspath)
val dirs =
scalafix.tests.BuildInfo.semanticClasspath.map(AbsolutePath.apply)
_db = LegacyInMemorySemanticdbIndex.load(
Classpaths.withDirectory(dir),
Classpaths.withDirectories(dirs.toList),
PathIO.workingDirectory
)
_input = _db.inputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class BasePrettyTypeSuite extends BaseSemanticSuite("TypeToTreeInput") {
super.beforeAll()
val classDir: m.AbsolutePath =
m.AbsolutePath(scalafix.tests.BuildInfo.sharedClasspath)
val semanticdbTargetRoot: AbsolutePath =
m.AbsolutePath(scalafix.tests.BuildInfo.semanticClasspath)
val semanticdbTargetRoots: List[AbsolutePath] =
scalafix.tests.BuildInfo.semanticClasspath.map(m.AbsolutePath.apply).toList

val classpath: Classpath =
Classpaths.withDirectories(List(semanticdbTargetRoot, classDir))
Classpaths.withDirectories(semanticdbTargetRoots :+ classDir)
val table: GlobalSymbolTable = GlobalSymbolTable(classpath, includeJdk = true)
}

Expand Down

0 comments on commit 20b3be8

Please sign in to comment.