Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SBT: add JSPlatform to tests #4817

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,6 +1,7 @@
package org.scalafmt.sysops

private[scalafmt] object PlatformCompat {
def isJS = true
def isJVM = false
def isScalaNative = false
def isNativeOnWindows = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scalafmt.sysops

private[scalafmt] object PlatformCompat {
def isJS = false
def isJVM = true
def isScalaNative = false
def isNativeOnWindows = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.scalafmt.sysops
import scala.scalanative.runtime.Platform

private[scalafmt] object PlatformCompat {
def isJS = false
def isJVM = false
def isScalaNative = true
def isNativeOnWindows = Platform.isWindows()
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,9 +1,15 @@
package org.scalafmt.config

import org.scalafmt.sysops._

import munit.FunSuite

class ScalafmtConfigTest extends FunSuite {

override def munitIgnore: Boolean =
// TODO: remove when scala.meta.internal.io.NodeNIOPath works on Windows
PlatformCompat.isJS && OsSpecific.isWindows

test("project.matcher") {
val config = ScalafmtConfig.fromHoconString(
"""|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.scalafmt.config

import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops._

import scala.meta.dialects

class StandardProjectLayoutTest extends munit.FunSuite {

override def munitIgnore: Boolean =
// TODO: remove when scala.meta.internal.io.NodeNIOPath works on Windows
PlatformCompat.isJS && OsSpecific.isWindows

import ProjectFiles.Layout.StandardConvention._

Seq(
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
}