Skip to content

Commit

Permalink
SBT: add JSPlatform to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 13, 2025
1 parent 760f347 commit 3851a7d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 36 deletions.
7 changes: 4 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ commands ++= Seq(
// jvm tests
"tests/test" :: "cli/test" ::
// js tests
"sysopsJS/test" ::
"testsJS/test" :: "sysopsJS/test" ::
// other
"publishLocal" :: docsTest :: s
},
Expand Down Expand Up @@ -229,7 +229,7 @@ lazy val cli = crossProject(JVMPlatform, NativePlatform)
.jvmEnablePlugins(NativeImagePlugin)
.jvmConfigure(_.dependsOn(dynamic.jvm).aggregate(dynamic.jvm, core.jvm))

lazy val tests = crossProject(JVMPlatform, NativePlatform)
lazy val tests = crossProject(JVMPlatform, NativePlatform, JSPlatform)
.withoutSuffixFor(JVMPlatform).in(file("scalafmt-tests")).settings(
publish / skip := true,
sharedTestSettings,
Expand All @@ -243,7 +243,8 @@ lazy val tests = crossProject(JVMPlatform, NativePlatform)
.get
}),
).enablePlugins(BuildInfoPlugin)
.jvmSettings(javaOptions += "-Dfile.encoding=UTF8").dependsOn(core, cli)
.jvmSettings(javaOptions += "-Dfile.encoding=UTF8").dependsOn(core)
.jsSettings(scalaJsSettings).jsEnablePlugins(ScalaJSPlugin)

lazy val sharedTestSettings = Seq(libraryDependencies += munit.value % Test)

Expand Down
9 changes: 4 additions & 5 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// scalafmt: { maxColumn = 100, align.preset = more, align.allowOverflow = true }

resolvers ++= Seq(
Classpaths.sbtPluginReleases,
Resolver.sonatypeRepo("releases"),
Resolver.bintrayIvyRepo("jetbrains", "sbt-plugins"),
)
resolvers ++= Resolver.sonatypeOssRepos("releases")
resolvers ++= Resolver.sonatypeOssRepos("snapshots")

resolvers ++= Seq(Classpaths.sbtPluginReleases, Resolver.bintrayIvyRepo("jetbrains", "sbt-plugins"))

val crossProjectV = "1.3.2"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.scalafmt

import scala.concurrent._

object TestPlatformCompat {
def executeAndWait(body: => Unit)(timeoutSeconds: Option[Int]): Unit =
// we don't wait in scala.js
Future(body)(scala.scalajs.concurrent.JSExecutionContext.queue)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.scalafmt

import scala.concurrent._

object TestPlatformCompat {
def executeAndWait(body: => Unit)(timeoutSeconds: Option[Int]): Unit = {
val future = Future(body)(ExecutionContext.Implicits.global)
timeoutSeconds
.foreach(x => Await.ready(future, duration.Duration(x, duration.SECONDS)))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.scalafmt

import scala.concurrent._

object TestPlatformCompat {
def executeAndWait(body: => Unit)(timeoutSeconds: Option[Int]): Unit = {
val future = Future(body)(ExecutionContext.Implicits.global)
timeoutSeconds
.foreach(x => Await.ready(future, duration.Duration(x, duration.SECONDS)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import org.scalafmt.util._

import scala.meta.parsers.ParseException

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration._

import munit.FunSuite
import munit.diff.Diff

Expand Down Expand Up @@ -155,14 +150,12 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
if (!onlyUnit && !onlyManual)
assertEquals(explored, 2520110, "total explored")
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
val k = for {
_ <- Future(PlatformFileOps.writeFile(
FileOps.getPath("target", "index.html"),
Report.heatmap(debugResults.result()),
))
} yield ()
// Travis exits right after running tests.
if (sys.env.contains("TRAVIS")) Await.ready(k, 20.seconds)
TestPlatformCompat.executeAndWait(PlatformFileOps.writeFile(
FileOps.getPath("target", "index.html"),
Report.heatmap(debugResults.result()),
))(
// Travis exits right after running tests.
if (sys.env.contains("TRAVIS")) Some(20) else None,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.scalafmt.config
import scala.meta.Dialect
import scala.meta.dialects.Scala213

import metaconfig.ConfError
import metaconfig.Configured
import munit.FunSuite

class ConfigDialectOverrideTest extends FunSuite {
Expand All @@ -19,16 +21,21 @@ class ConfigDialectOverrideTest extends FunSuite {
).get,
)

test("throws on an incorrect type of setting")(
intercept[java.util.NoSuchElementException](
ScalafmtConfig.fromHoconString(
"""|
|runner.dialectOverride.toplevelSeparator = true
|runner.dialect = scala213
|""".stripMargin,
).get,
),
)
test("throws on an incorrect type of setting") {
val res =
try ScalafmtConfig.fromHoconString(
"""|
|runner.dialectOverride.toplevelSeparator = true
|runner.dialect = scala213
|""".stripMargin,
)
catch { case ex: Throwable => Configured.NotOk(ConfError.exception(ex)) }
res match {
case Configured.NotOk(err: ConfError) =>
assert(err.msg.contains("cannot be cast to"), err.msg)
case _ => fail("should have failed")
}
}

def testBooleanFlag(
methodName: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.scalafmt.util

import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops.PlatformCompat
import org.scalafmt.tests.BuildInfo

import java.nio.file.Paths

import munit.Location

case class DiffTest(
Expand All @@ -19,10 +19,11 @@ case class DiffTest(
stateVisits: Option[Int] = None,
stateVisits2: Option[Int] = None,
) {
val file = DiffTest.testDir.relativize(Paths.get(loc.path)).toString
val file = PlatformCompat
.relativize(AbsoluteFile(DiffTest.testDir), AbsoluteFile(loc.path))
val fullName = s"$file:${loc.line}: $name"
}

object DiffTest {
val testDir = BuildInfo.resourceDirectory.toPath
val testDir = BuildInfo.resourceDirectory
}

0 comments on commit 3851a7d

Please sign in to comment.