Skip to content
New issue

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

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

Already on GitHub? # to your account

Add check for right Tests traits in ScalaJS and Native #2874

Merged
merged 11 commits into from
Nov 15, 2023
16 changes: 16 additions & 0 deletions scalajslib/test/src/mill/scalajslib/HelloJSWorldTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ object HelloJSWorldTests extends TestSuite {
}
}

object scalaTestsError extends ScalaJSModule {
val (scala, scalaJS) = matrix.head
def scalaVersion = scala
def scalaJSVersion = scalaJS
object test extends ScalaTests with TestModule.Utest
}

object buildScalaTest extends Cross[BuildModuleScalaTest](matrix)
trait BuildModuleScalaTest extends RootModule {
object test extends ScalaJSTests with TestModule.ScalaTest {
Expand Down Expand Up @@ -272,6 +279,15 @@ object HelloJSWorldTests extends TestSuite {
)
}

test("extends-ScalaTests") {
val error = intercept[ExceptionInInitializerError] {
HelloJSWorld.scalaTestsError.test
}
assert(
error.getCause.getMessage == s"scalaTestsError is a `ScalaJSModule`. scalaTestsError.test needs to extend `ScalaJSTests` instead of `ScalaTests`"
)
}

def checkRun(scalaVersion: String, scalaJSVersion: String): Unit = {
val task = HelloJSWorld.helloJsWorld(scalaVersion, scalaJSVersion).run()

Expand Down
23 changes: 23 additions & 0 deletions scalalib/src/mill/scalalib/ScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ trait ScalaModule extends JavaModule with TestModule.ScalaModuleBase { outer =>
type ScalaModuleTests = ScalaTests

trait ScalaTests extends JavaModuleTests with ScalaModule {
try {
if (
Class.forName("mill.scalajslib.ScalaJSModule").isInstance(outer) && !Class.forName(
"mill.scalajslib.ScalaJSModule$ScalaJSTests"
).isInstance(this)
) throw new Exception(
lolgab marked this conversation as resolved.
Show resolved Hide resolved
s"$outer is a `ScalaJSModule`. $this needs to extend `ScalaJSTests` instead of `ScalaTests`"
lolgab marked this conversation as resolved.
Show resolved Hide resolved
)
} catch {
case _: ClassNotFoundException => // if we can't find the classes, we certainly are not in a ScalaJSModule
}
try {
if (
Class.forName("mill.scalanativelib.ScalaNativeModule").isInstance(outer) && !Class.forName(
"mill.scalanativelib.ScalaNativeModule$ScalaNativeTests"
).isInstance(this)
) throw new Exception(
lolgab marked this conversation as resolved.
Show resolved Hide resolved
s"$outer is a `ScalaNativeModule`. $this needs to extend `ScalaNativeTests` instead of `ScalaTests`"
lolgab marked this conversation as resolved.
Show resolved Hide resolved
)
} catch {
case _: ClassNotFoundException => // if we can't find the classes, we certainly are not in a ScalaNativeModule
}

override def scalaOrganization: Target[String] = outer.scalaOrganization()
override def scalaVersion: Target[String] = outer.scalaVersion()
override def scalacPluginIvyDeps: Target[Agg[Dep]] = outer.scalacPluginIvyDeps()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ object HelloNativeWorldTests extends TestSuite {
)
}
}

object scalaTestsError extends ScalaNativeModule {
val (scala, scalaNative, _) = matrix.head
def scalaVersion = scala
def scalaNativeVersion = scalaNative
object test extends ScalaTests with TestModule.Utest
}

object inherited extends ScalaNativeModule {
val (scala, scalaNative, _) = matrix.head
def scalacOptions = Seq("-deprecation")
Expand Down Expand Up @@ -230,6 +238,15 @@ object HelloNativeWorldTests extends TestSuite {
testAllMatrix((scala, scalaNative, releaseMode) => checkRun(scala, scalaNative, releaseMode))
}

test("extends-ScalaTests") {
val error = intercept[ExceptionInInitializerError] {
HelloNativeWorld.scalaTestsError.test
}
assert(
error.getCause.getMessage == s"scalaTestsError is a `ScalaNativeModule`. scalaTestsError.test needs to extend `ScalaNativeTests` instead of `ScalaTests`"
)
}

def checkInheritedTargets[A](target: ScalaNativeModule => T[A], expected: A) = {
val Right((mainResult, _)) = helloWorldEvaluator(target(HelloNativeWorld.inherited))
val Right((testResult, _)) = helloWorldEvaluator(target(HelloNativeWorld.inherited.test))
Expand Down
Loading