From a7b4bf4de234f1222e1160f52f1a2c093e478ba1 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:37:08 -0800 Subject: [PATCH] SBT: refactor tests, move those for cli and sysops 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. --- build.sbt | 60 +++++++++---------- .../org/scalafmt/cli/CliOptionsJVMTest.scala | 0 .../org/scalafmt/cli/CliOptionsTest.scala | 0 .../test/scala/org/scalafmt/cli/CliTest.scala | 0 .../scala/org/scalafmt/cli/FakeGitOps.scala | 0 .../scala/org/scalafmt/cli/FileTestOps.scala | 0 .../org/scalafmt/sysops}/DeleteTree.scala | 4 +- .../org/scalafmt/sysops/FileOpsTest.scala | 4 +- .../org/scalafmt/sysops/GitOpsTest.scala | 4 +- 9 files changed, 31 insertions(+), 41 deletions(-) rename {scalafmt-tests => scalafmt-cli}/jvm/src/test/scala/org/scalafmt/cli/CliOptionsJVMTest.scala (100%) rename {scalafmt-tests => scalafmt-cli}/shared/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala (100%) rename {scalafmt-tests => scalafmt-cli}/shared/src/test/scala/org/scalafmt/cli/CliTest.scala (100%) rename {scalafmt-tests => scalafmt-cli}/shared/src/test/scala/org/scalafmt/cli/FakeGitOps.scala (100%) rename {scalafmt-tests => scalafmt-cli}/shared/src/test/scala/org/scalafmt/cli/FileTestOps.scala (100%) rename {scalafmt-tests/shared/src/test/scala/org/scalafmt/util => scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops}/DeleteTree.scala (93%) rename {scalafmt-tests => scalafmt-sysops}/shared/src/test/scala/org/scalafmt/sysops/FileOpsTest.scala (95%) rename {scalafmt-tests => scalafmt-sysops}/shared/src/test/scala/org/scalafmt/sysops/GitOpsTest.scala (99%) diff --git a/build.sbt b/build.sbt index 3796ab1082..fb1c10fe9e 100644 --- a/build.sbt +++ b/build.sbt @@ -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 } @@ -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) @@ -116,6 +113,7 @@ lazy val sysops = crossProject(JVMPlatform, NativePlatform) case _ => Seq() } }, + sharedTestSettings, ) lazy val config = crossProject(JVMPlatform, NativePlatform) @@ -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 ++= { @@ -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 @@ -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", @@ -219,18 +218,15 @@ 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" -> { @@ -238,20 +234,18 @@ lazy val tests = crossProject(JVMPlatform, NativePlatform) (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) @@ -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", @@ -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, @@ -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( diff --git a/scalafmt-tests/jvm/src/test/scala/org/scalafmt/cli/CliOptionsJVMTest.scala b/scalafmt-cli/jvm/src/test/scala/org/scalafmt/cli/CliOptionsJVMTest.scala similarity index 100% rename from scalafmt-tests/jvm/src/test/scala/org/scalafmt/cli/CliOptionsJVMTest.scala rename to scalafmt-cli/jvm/src/test/scala/org/scalafmt/cli/CliOptionsJVMTest.scala diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala b/scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala similarity index 100% rename from scalafmt-tests/shared/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala rename to scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/cli/CliTest.scala b/scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/CliTest.scala similarity index 100% rename from scalafmt-tests/shared/src/test/scala/org/scalafmt/cli/CliTest.scala rename to scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/CliTest.scala diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/cli/FakeGitOps.scala b/scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/FakeGitOps.scala similarity index 100% rename from scalafmt-tests/shared/src/test/scala/org/scalafmt/cli/FakeGitOps.scala rename to scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/FakeGitOps.scala diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/cli/FileTestOps.scala b/scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/FileTestOps.scala similarity index 100% rename from scalafmt-tests/shared/src/test/scala/org/scalafmt/cli/FileTestOps.scala rename to scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/FileTestOps.scala diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/util/DeleteTree.scala b/scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/DeleteTree.scala similarity index 93% rename from scalafmt-tests/shared/src/test/scala/org/scalafmt/util/DeleteTree.scala rename to scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/DeleteTree.scala index 3ec783bff9..665a02d726 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/util/DeleteTree.scala +++ b/scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/DeleteTree.scala @@ -1,4 +1,4 @@ -package org.scalafmt.util +package org.scalafmt.sysops import java.io.IOException import java.nio.file.FileVisitResult @@ -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) () } diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/sysops/FileOpsTest.scala b/scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/FileOpsTest.scala similarity index 95% rename from scalafmt-tests/shared/src/test/scala/org/scalafmt/sysops/FileOpsTest.scala rename to scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/FileOpsTest.scala index 08108c0a9d..da818dad27 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/sysops/FileOpsTest.scala +++ b/scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/FileOpsTest.scala @@ -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 @@ -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") diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/sysops/GitOpsTest.scala b/scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/GitOpsTest.scala similarity index 99% rename from scalafmt-tests/shared/src/test/scala/org/scalafmt/sysops/GitOpsTest.scala rename to scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/GitOpsTest.scala index db04cc8278..0b92bba7c4 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/sysops/GitOpsTest.scala +++ b/scalafmt-sysops/shared/src/test/scala/org/scalafmt/sysops/GitOpsTest.scala @@ -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 @@ -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")