Skip to content

Commit

Permalink
run more tests against scala 3 targets
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Apr 10, 2022
1 parent 26e0b55 commit 7cfbad5
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 58 deletions.
17 changes: 16 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,22 @@ lazy val unit = projectMatrix
),
Test / test := (Test / test)
.dependsOn(cli.projectRefs.map(_ / publishLocalTransitive): _*)
.value
.value,
Test / unmanagedSourceDirectories ++= {
val sourceDir = (Test / sourceDirectory).value
val maybeTargetScalaVersion =
TargetAxis
.targetScalaVersion(virtualAxes.value)
.flatMap(CrossVersion.partialVersion(_))
maybeTargetScalaVersion match {
case Some((n, m)) =>
Seq(
sourceDir / s"scala-target$n",
sourceDir / s"scala-target$n.$m"
)
case _ => Seq()
}
}
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(
Expand Down
8 changes: 4 additions & 4 deletions project/TargetAxis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ case class TargetAxis(scalaVersion: String) extends VirtualAxis.WeakAxis {

object TargetAxis {

private def targetScalaVersion(virtualAxes: Seq[VirtualAxis]): String =
virtualAxes.collectFirst { case a: TargetAxis => a.scalaVersion }.get
def targetScalaVersion(virtualAxes: Seq[VirtualAxis]): Option[String] =
virtualAxes.collectFirst { case a: TargetAxis => a.scalaVersion }

/**
* When invoked on a ProjectMatrix with a TargetAxis, lookup the project
Expand All @@ -27,7 +27,7 @@ object TargetAxis {
key: TaskKey[T]
): Def.Initialize[Task[T]] =
Def.taskDyn {
val sv = targetScalaVersion(virtualAxes.value)
val sv = targetScalaVersion(virtualAxes.value).get
val project = matrix.finder().apply(sv)
Def.task((project / key).value)
}
Expand All @@ -42,7 +42,7 @@ object TargetAxis {
key: SettingKey[T]
): Def.Initialize[T] =
Def.settingDyn {
val sv = targetScalaVersion(virtualAxes.value)
val sv = targetScalaVersion(virtualAxes.value).get
val project = matrix.finder().apply(sv)
Def.setting((project / key).value)
}
Expand Down
44 changes: 44 additions & 0 deletions scalafix-tests/unit/src/main/scala/scalafix/test/cli/Rules.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package scalafix.tests.cli

import metaconfig._
import scalafix.v1
import scalafix.v1._

class CrashingRule extends SyntacticRule("CrashingRule") {
override def fix(implicit doc: SyntacticDocument): _root_.scalafix.v1.Patch =
throw new MissingSymbolException(Symbol("local63"))
}

class NoOpRule extends SyntacticRule("NoOpRule") {
override def fix(implicit doc: SyntacticDocument): _root_.scalafix.v1.Patch =
Patch.empty
}

class DeprecatedName
extends SyntacticRule(
RuleName("DeprecatedName").withDeprecatedName(
"OldDeprecatedName",
"Use DeprecatedName instead",
"1.0"
)
) {
override def fix(implicit doc: SyntacticDocument): _root_.scalafix.v1.Patch =
Patch.empty
}

class Scala2_9 extends SyntacticRule("Scala2_9") {
override def withConfiguration(config: Configuration): Configured[Rule] =
if (!config.scalaVersion.startsWith("2.9")) {
Configured.error("scalaVersion must start with 2.9")
} else if (!config.scalacOptions.contains("-Ysource:2.9")) {
Configured.error("scalacOptions must contain -Ysource:2.9")
} else {
Configured.ok(this)
}
}

class AvailableRule
extends v1.SemanticRule(
v1.RuleName("AvailableRule")
.withDeprecatedName("DeprecatedAvailableRule", "", "")
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scalafix.tests.v0
import scala.meta._
import scalafix.tests.core.BaseSemanticSuite

// LegacySyntheticsTest cannot be compiled with Scala 2.13 or Scala 3
class LegacySyntheticsSuite extends BaseSemanticSuite("LegacySyntheticsTest") {

test("text") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,16 @@ trait BaseCliSuite extends AnyFunSuite with DiffAssertions {
)
}

// The defaults of this helper expect to find test input which is currently
// built only on scala 2.x since building them for Scala 3 would trigger
// test failures as the rules they exercise are not supported on Scala 3.
// TODO: switch defaults to rely on much more simple test input/rules so
// that we don't need to hardcode a scala version here.
val sourceDir = "scalafix-tests/input/src/main/scala-2"

def sourceDirectory: AbsolutePath =
props.inputSourceDirectories
// TODO: This test won't work for scala 3. The path is hardcoded
.find(dir => dir.toNIO.endsWith("scala-2")) // Skip scala-2.12
.find(dir => dir.toNIO.endsWith(sourceDir))
.getOrElse {
throw new IllegalArgumentException(
props.inputSourceDirectories.toString()
Expand Down Expand Up @@ -195,8 +201,7 @@ trait BaseCliSuite extends AnyFunSuite with DiffAssertions {
test(name, SkipWindows) {
val fileIsFixed = expectedExit.isOk
val cwd = Files.createTempDirectory("scalafix")
val inputSourceDirectory =
cwd.resolve("scalafix-tests/input/src/main/scala-2/")
val inputSourceDirectory = cwd.resolve(sourceDir)
Files.createDirectories(inputSourceDirectory)
val root = AbsolutePath(inputSourceDirectory)
val out = new ByteArrayOutputStream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
package scalafix.tests.cli

import metaconfig.Configured
import scalafix.cli._
import scalafix.patch.Patch
import scalafix.rule.RuleName
import scalafix.v1
import scalafix.v1.Configuration
import scalafix.v1.MissingSymbolException
import scalafix.v1.Rule
import scalafix.v1.Symbol
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule

class CliSyntacticSuite extends BaseCliSuite {

Expand Down Expand Up @@ -477,42 +467,3 @@ class CliSyntacticSuite extends BaseCliSuite {
}
)
}

class CrashingRule extends SyntacticRule("CrashingRule") {
override def fix(implicit doc: SyntacticDocument): _root_.scalafix.v1.Patch =
throw new MissingSymbolException(Symbol("local63"))
}

class NoOpRule extends SyntacticRule("NoOpRule") {
override def fix(implicit doc: SyntacticDocument): _root_.scalafix.v1.Patch =
Patch.empty
}

class DeprecatedName
extends SyntacticRule(
RuleName("DeprecatedName").withDeprecatedName(
"OldDeprecatedName",
"Use DeprecatedName instead",
"1.0"
)
) {
override def fix(implicit doc: SyntacticDocument): _root_.scalafix.v1.Patch =
Patch.empty
}

class Scala2_9 extends SyntacticRule("Scala2_9") {
override def withConfiguration(config: Configuration): Configured[Rule] =
if (!config.scalaVersion.startsWith("2.9")) {
Configured.error("scalaVersion must start with 2.9")
} else if (!config.scalacOptions.contains("-Ysource:2.9")) {
Configured.error("scalacOptions must contain -Ysource:2.9")
} else {
Configured.ok(this)
}
}

class AvailableRule
extends v1.SemanticRule(
v1.RuleName("AvailableRule")
.withDeprecatedName("DeprecatedAvailableRule", "", "")
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class BasePrettyTypeSuite extends BaseSemanticSuite("TypeToTreeInput") {

val classpath: Classpath =
Classpaths.withDirectories(semanticdbTargetRoots :+ classDir)

// As of scalameta 4.5.3, this relies on scalap (and not on TASTy), so it
// cannot work against classes compiled with Scala 3
val table: GlobalSymbolTable = GlobalSymbolTable(classpath, includeJdk = true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class SymbolSuite extends munit.FunSuite {
case Term.ApplyInfix(_, Term.Name("shouldBe"), _, arg :: Nil) => arg
}

// The symbol lookup fails against Scala 3.1.1 SemanticDB as the position
// there excludes surrounding parentheses while 2.x (scalac-semanticdb) and
// the parser include them
assertNotEquals(arg.symbol, Symbol.None)
}

Expand Down

0 comments on commit 7cfbad5

Please sign in to comment.