Skip to content

Commit

Permalink
DynamicSuite: test nightly version via local build
Browse files Browse the repository at this point in the history
This will allow us to avoid the problem that we saw with 3.7.16, when
we introduced a bug with dynamic loading but only discovered it after
the release.
  • Loading branch information
kitbellew committed Nov 12, 2023
1 parent 75669c3 commit 5143812
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ lazy val dynamic = project
scalacOptions ++= scalacJvmOptions.value
)
.dependsOn(interfaces)
.dependsOn(core.jvm % "test")
.enablePlugins(BuildInfoPlugin)

lazy val interfaces = project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ final case class ScalafmtDynamic(
def this() = this(
ScalafmtProperties(),
new ScalafmtModuleLoader.CachedProxy(
new ScalafmtModuleLoader.WithDownloader(CoursierDependencyDownloader)
ScalafmtDynamic.defaultUncachedModuleLoader
),
new ScalafmtConfigLoader.CachedProxy(ScalafmtConfigLoader)
new ScalafmtConfigLoader.CachedProxy(
ScalafmtDynamic.defaultUncachedConfigLoader
)
)

override def clear(): Unit = {
Expand Down Expand Up @@ -59,3 +61,12 @@ final case class ScalafmtDynamic(
configLoader.load(configPath, properties, moduleLoader)

}

private[dynamic] object ScalafmtDynamic {

def defaultUncachedModuleLoader =
new ScalafmtModuleLoader.WithDownloader(CoursierDependencyDownloader)

def defaultUncachedConfigLoader = ScalafmtConfigLoader

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, Paths}
import java.nio.file.attribute.FileTime

import org.scalafmt.interfaces.{PositionException, Scalafmt, ScalafmtReporter}
import org.scalafmt.interfaces.{PositionException, ScalafmtReporter}
import PositionSyntax._

import scala.collection.mutable
Expand Down Expand Up @@ -66,12 +66,14 @@ class DynamicSuite extends FunSuite {
)
}
}
val dynamic: ScalafmtDynamic = cfgFunc(
Scalafmt
.create(this.getClass.getClassLoader)
.withReporter(reporter)
.asInstanceOf[ScalafmtDynamic]
)
val dynamic: ScalafmtDynamic = cfgFunc {
val configLoader = ScalafmtDynamic.defaultUncachedConfigLoader
new ScalafmtDynamic(
properties = ScalafmtProperties(reporter = reporter),
moduleLoader = new ScalafmtModuleLoader.CachedProxy(getModuleLoader),
configLoader = new ScalafmtConfigLoader.CachedProxy(configLoader)
)
}
val config = Files.createTempFile("scalafmt", ".scalafmt.conf")
val filename = Paths.get(name + ".scala")
var timestamps = 100L
Expand All @@ -83,7 +85,7 @@ class DynamicSuite extends FunSuite {
def setVersion(newVersion: String, dialect: String, rest: String*): Unit = {
val dialectLine = Option(dialect).fold("")(x => s"runner.dialect = $x")
setConfig(s"""
|version=$newVersion
|version="$newVersion"
|$dialectLine
|${rest.mkString("\n")}
|""".stripMargin)
Expand Down Expand Up @@ -171,6 +173,7 @@ class DynamicSuite extends FunSuite {
}

private val testedVersions = Seq(
nightly,
latest,
"3.1.2",
"2.7.5",
Expand Down Expand Up @@ -202,15 +205,14 @@ class DynamicSuite extends FunSuite {
}
}

def latest = BuildInfo.previousStable

def checkVersion(version: String, dialect: String): Unit = {
check(s"v$version") { f =>
f.setVersion(version, dialect)
f.assertFormat("object A { }", "object A {}\n")
}
}

checkVersion(nightly, "scala212")
checkVersion(latest, "scala212")
checkVersion("1.5.1", "scala211")
checkVersion("1.0.0", "scala211")
Expand Down Expand Up @@ -585,7 +587,24 @@ class DynamicSuite extends FunSuite {

private object DynamicSuite {

def nightly = BuildInfo.nightly
def latest = BuildInfo.previousStable

def getDialectError(version: String, dialect: String) =
if (version >= "3.1.0") s" [dialect $dialect]" else ""

// in tests, let's not try to download current version
def getModuleLoader = new ScalafmtModuleLoader {
private val downloader = ScalafmtDynamic.defaultUncachedModuleLoader
private val clsLoader = org.scalafmt.Scalafmt.getClass.getClassLoader
override def load(
cfg: Path,
ver: ScalafmtVersion,
props: ScalafmtProperties
): FormatEval[ScalafmtReflect] =
if (ver.toString == nightly) Right(ScalafmtReflect(clsLoader, ver))
else downloader.load(cfg, ver, props)
override def close(): Unit = downloader.close()
}

}

0 comments on commit 5143812

Please sign in to comment.