Skip to content

Commit

Permalink
SBT: refactor tests, move those for cli and sysops
Browse files Browse the repository at this point in the history
As a result, `tests` contains only `core` tests; the reason for it to
remain separate is due to special BuildInfo handling, to capture the
resource directories holding `.stat` files to test with.
  • Loading branch information
kitbellew committed Jan 25, 2025
1 parent 9b13e8d commit a7b4bf4
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 41 deletions.
60 changes: 27 additions & 33 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ commands += Command.command("ci-test-jvm") { s =>
case _ => scala213
}
val docsTest = if (scalaVersion == scala212) "docs/run" else "version"
s"++$scalaVersion" :: "tests/test" :: "dynamic/test" :: "publishLocal" ::
s"++$scalaVersion" :: "tests/test" :: "cli/test" :: "publishLocal" ::
docsTest :: s
}

Expand All @@ -69,15 +69,12 @@ lazy val dynamic = crossProject(JVMPlatform, NativePlatform)
.withoutSuffixFor(JVMPlatform).in(file("scalafmt-dynamic")).settings(
moduleName := "scalafmt-dynamic",
description := "Implementation of scalafmt-interfaces",
buildInfoSettings,
buildInfoPackage := "org.scalafmt.dynamic",
buildInfoObject := "BuildInfo",
buildInfoSettings("org.scalafmt.dynamic", "BuildInfo"),
libraryDependencies ++= List(
"io.get-coursier" % "interface" % "1.0.26",
"com.typesafe" % "config" % "1.4.3",
munit.value % Test,
scalametaTestkit.value % Test,
),
sharedTestSettings,
scalacOptions ++= scalacJvmOptions.value,
).dependsOn(interfaces, sysops).dependsOn(core % "test")
.enablePlugins(BuildInfoPlugin)
Expand Down Expand Up @@ -116,6 +113,7 @@ lazy val sysops = crossProject(JVMPlatform, NativePlatform)
case _ => Seq()
}
},
sharedTestSettings,
)

lazy val config = crossProject(JVMPlatform, NativePlatform)
Expand All @@ -135,7 +133,7 @@ lazy val config = crossProject(JVMPlatform, NativePlatform)
lazy val core = crossProject(JVMPlatform, NativePlatform)
.in(file("scalafmt-core")).settings(
moduleName := "scalafmt-core",
buildInfoSettings,
buildInfoSettings("org.scalafmt", "Versions"),
scalacOptions ++= scalacJvmOptions.value,
libraryDependencies ++= Seq("org.scalameta" %%% "mdoc-parser" % mdocV),
libraryDependencies ++= {
Expand All @@ -153,7 +151,7 @@ lazy val core = crossProject(JVMPlatform, NativePlatform)
// )
// )
.nativeSettings(libraryDependencies += "com.lihaoyi" %%% "fastparse" % "3.1.1")
.jvmSettings(Test / run / fork := true).dependsOn(sysops, config, macros)
.aggregate(sysops, config, macros).dependsOn(sysops, config, macros)
.enablePlugins(BuildInfoPlugin)
lazy val coreJVM = core.jvm
// lazy val coreJS = core.js
Expand Down Expand Up @@ -197,12 +195,13 @@ lazy val cli = crossProject(JVMPlatform, NativePlatform)
oldStrategy(x)
},
libraryDependencies ++= Seq(
"org.scalameta" %%% "munit-diff" % "1.0.3",
"org.scalameta" %%% "munit-diff" % munitV,
"com.martiansoftware" % "nailgun-server" % "0.9.1",
"com.github.scopt" %%% "scopt" % "4.1.0",
),
scalacOptions ++= scalacJvmOptions.value,
Compile / mainClass := Some("org.scalafmt.cli.Cli"),
sharedTestSettings,
).jvmSettings(
nativeImageInstalled := isCI,
nativeImageOptions += "-march=compatibility",
Expand All @@ -219,39 +218,34 @@ lazy val cli = crossProject(JVMPlatform, NativePlatform)
case _ => Nil
}
},
).nativeSettings(scalaNativeConfig).dependsOn(core, dynamic)
.jvmEnablePlugins(NativeImagePlugin)
).nativeSettings(scalaNativeConfig).jvmEnablePlugins(NativeImagePlugin)
.dependsOn(core, dynamic).aggregate(core, dynamic)

lazy val tests = crossProject(JVMPlatform, NativePlatform)
.withoutSuffixFor(JVMPlatform).in(file("scalafmt-tests")).settings(
publish / skip := true,
libraryDependencies ++= Seq(
// Test dependencies
"com.lihaoyi" %%% "scalatags" % "0.13.1",
scalametaTestkit.value,
munit.value,
),
sharedTestSettings,
libraryDependencies += scalametaTestkit.value % Test,
libraryDependencies += "com.lihaoyi" %%% "scalatags" % "0.13.1" % Test,
scalacOptions ++= scalacJvmOptions.value,
buildInfoPackage := "org.scalafmt.tests",
buildInfoKeys := Seq[BuildInfoKey]("resourceDirectory" -> {
val sharedTests = (baseDirectory.value.getParentFile / "shared").toPath
(Test / resourceDirectories).value.find(_.toPath.startsWith(sharedTests))
.get
}),
).enablePlugins(BuildInfoPlugin)
.jvmSettings(javaOptions += "-Dfile.encoding=UTF8")
.dependsOn(core, dynamic, cli)
).enablePlugins(BuildInfoPlugin).jvmSettings(Test / run / fork := true)
.dependsOn(core)

lazy val sharedTestSettings = Seq(
libraryDependencies += munit.value % Test,
Test / javaOptions += "-Dfile.encoding=UTF8",
)

lazy val communityTestsCommon = project
.in(file("scalafmt-tests-community/common")).settings(
communityTestsSettings,
libraryDependencies ++= Seq(
// Test dependencies
"com.lihaoyi" %% "scalatags" % "0.13.1",
scalametaTestkit.value,
munit.value,
),
).enablePlugins(BuildInfoPlugin).dependsOn(coreJVM)
.in(file("scalafmt-tests-community/common"))
.settings(communityTestsSettings, sharedTestSettings)
.enablePlugins(BuildInfoPlugin).dependsOn(coreJVM)

lazy val communityTestsScala2 = project
.in(file("scalafmt-tests-community/scala2")).settings(communityTestsSettings)
Expand All @@ -276,7 +270,7 @@ lazy val communityTestsOther = project.in(file("scalafmt-tests-community/other")
lazy val benchmarks = project.in(file("scalafmt-benchmarks")).settings(
publish / skip := true,
moduleName := "scalafmt-benchmarks",
libraryDependencies ++= Seq(scalametaTestkit.value),
libraryDependencies += scalametaTestkit.value,
run / javaOptions ++= Seq(
"-Djava.net.preferIPv4Stack=true",
"-XX:+AggressiveOpts",
Expand Down Expand Up @@ -308,7 +302,7 @@ val Milestone = s"($V-M\\d+).*".r
lazy val stableVersion = Def
.setting((ThisBuild / version).value.replaceAll("\\+.*", ""))

lazy val buildInfoSettings: Seq[Def.Setting[_]] = Seq(
def buildInfoSettings(pkg: String, obj: String): Seq[Def.Setting[_]] = Seq(
buildInfoKeys := Seq[BuildInfoKey](
name,
version,
Expand All @@ -325,8 +319,8 @@ lazy val buildInfoSettings: Seq[Def.Setting[_]] = Seq(
scalaVersion,
sbtVersion,
),
buildInfoPackage := "org.scalafmt",
buildInfoObject := "Versions",
buildInfoPackage := pkg,
buildInfoObject := obj,
)

lazy val communityTestsSettings: Seq[Def.Setting[_]] = Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.scalafmt.util
package org.scalafmt.sysops

import java.io.IOException
import java.nio.file.FileVisitResult
Expand All @@ -8,7 +8,7 @@ import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributes

object DeleteTree {
def deleteTree(path: Path): Unit = {
def apply(path: Path): Unit = {
Files.walkFileTree(path, new DeleteTree)
()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.scalafmt.sysops

import org.scalafmt.util.DeleteTree.deleteTree

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
Expand All @@ -18,7 +16,7 @@ class FileOpsTest extends munit.FunSuite {
path = Files.createTempDirectory("FileOpsTestDir")

override def afterEach(context: AfterEach): Unit =
try deleteTree(path)
try DeleteTree(path)
catch {
case e: Throwable =>
println("Unable to delete test files")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.scalafmt.sysops

import org.scalafmt.util.DeleteTree.deleteTree

import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Files
Expand Down Expand Up @@ -36,7 +34,7 @@ class GitOpsTest extends FunSuite {
}

override def afterEach(context: AfterEach): Unit =
try deleteTree(path.path)
try DeleteTree(path.path)
catch {
case e: Throwable =>
println("Unable to delete test files")
Expand Down

0 comments on commit a7b4bf4

Please sign in to comment.