Skip to content

Commit

Permalink
Re-enable PluginCoverageScalaJsTest, fix dangling UndefinedParam
Browse files Browse the repository at this point in the history
…bug (#464)

* Try re-enabling PluginCoverageScalaJsTest

* ScoverageCompiler refactoring to support SJS

* Scala.js test is working again

* Remove unneeded dep

* Try adding buildInfo to root

* Set crossScalaVersions for buildinfo project

* Revert "Remove unneeded dep"

This reverts commit 8da1f01.

* Add another sjs test dep

* plugin also depends on runtimeJS for tests

* Actually enable the sjs compiler plugin :)

* Fix expected measured statements 4 -> 2

* Only catch non-fatals

* Make isScalaJsEnabled a lazy val

* I think these deps are unneeded now
  • Loading branch information
armanbilge authored Jun 3, 2022
1 parent ba4838e commit 7eba8e1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
18 changes: 15 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ lazy val root = Project("scalac-scoverage", file("."))
runtimeJSDOMTest,
reporter,
domain,
serializer
serializer,
buildInfo
)

lazy val runtime = CrossProject(
Expand Down Expand Up @@ -136,7 +137,8 @@ lazy val runtimeJSDOMTest =

lazy val plugin =
project
.dependsOn(runtimeJVM % Test)
// we need both runtimes compiled prior to running tests
.dependsOn(runtimeJVM % Test, runtimeJS % Test)
.settings(
name := "scalac-scoverage-plugin",
crossTarget := target.value / s"scala-${scalaVersion.value}",
Expand All @@ -148,7 +150,7 @@ lazy val plugin =
.settings(
Test / unmanagedSourceDirectories += (Test / sourceDirectory).value / "scala-2.12+"
)
.dependsOn(domain, reporter % "test->compile", serializer)
.dependsOn(domain, reporter % "test->compile", serializer, buildInfo % Test)

lazy val reporter =
project
Expand All @@ -169,6 +171,16 @@ lazy val reporter =
)
.dependsOn(domain, serializer)

lazy val buildInfo =
project
.settings(
crossScalaVersions := bin212 ++ bin213,
buildInfoKeys += BuildInfoKey("scalaJSVersion", scalaJSVersion),
publishArtifact := false,
publishLocal := {}
)
.enablePlugins(BuildInfoPlugin)

lazy val domain =
project
.settings(
Expand Down
3 changes: 2 additions & 1 deletion plugin/src/main/scala/scoverage/ScoveragePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scala.tools.nsc.plugins.Plugin
import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.transform.Transform
import scala.tools.nsc.transform.TypingTransformers
import scala.util.control.NonFatal

import scoverage.domain.Coverage
import scoverage.domain.Statement
Expand Down Expand Up @@ -102,7 +103,7 @@ class ScoverageInstrumentationComponent(
try {
rootMirror.getClassIfDefined("scala.scalajs.js.Any") != NoSymbol
} catch {
case _: Throwable => false
case NonFatal(_) => false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import munit.FunSuite
*/
class PluginCoverageScalaJsTest extends FunSuite with MacroSupport {

test("scoverage should ignore default undefined parameter".ignore) {
val compiler = ScoverageCompiler.default
test("scoverage should ignore default undefined parameter") {
val compiler = ScoverageCompiler.defaultJS
compiler.compileCodeSnippet(
"""import scala.scalajs.js
|
Expand All @@ -16,6 +16,6 @@ class PluginCoverageScalaJsTest extends FunSuite with MacroSupport {
|}""".stripMargin
)
assert(!compiler.reporter.hasErrors)
compiler.assertNMeasuredStatements(4)
compiler.assertNMeasuredStatements(2)
}
}
61 changes: 51 additions & 10 deletions plugin/src/test/scala/scoverage/ScoverageCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.transform.Transform
import scala.tools.nsc.transform.TypingTransformers

import buildinfo.BuildInfo
import scoverage.reporter.IOUtils

private[scoverage] object ScoverageCompiler {
Expand All @@ -24,9 +25,22 @@ private[scoverage] object ScoverageCompiler {
def classPath: Seq[String] =
getScalaJars.map(
_.getAbsolutePath
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses.getAbsolutePath
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses("jvm").getAbsolutePath

def settings: Settings = {
def jsClassPath: Seq[String] =
getScalaJsJars.map(
_.getAbsolutePath
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses("js").getAbsolutePath

def settings: Settings = settings(classPath)

def jsSettings: Settings = {
val s = settings(jsClassPath)
s.plugin.value = List(getScalaJsCompilerJar.getAbsolutePath)
s
}

def settings(classPath: Seq[String]): Settings = {
val s = new scala.tools.nsc.Settings
s.Xprint.value = List("all", "_")
s.deprecation.value = true
Expand All @@ -46,6 +60,11 @@ private[scoverage] object ScoverageCompiler {
new ScoverageCompiler(settings, reporter)
}

def defaultJS: ScoverageCompiler = {
val reporter = new scala.tools.nsc.reporters.ConsoleReporter(jsSettings)
new ScoverageCompiler(jsSettings, reporter)
}

def locationCompiler: LocationCompiler = {
val reporter = new scala.tools.nsc.reporters.ConsoleReporter(settings)
new LocationCompiler(settings, reporter)
Expand All @@ -56,6 +75,19 @@ private[scoverage] object ScoverageCompiler {
scalaJars.map(findScalaJar)
}

private def getScalaJsJars: List[File] =
findJar(
"org.scala-js",
s"scalajs-library_$ShortScalaVersion",
BuildInfo.scalaJSVersion
) :: getScalaJars

private def getScalaJsCompilerJar: File = findJar(
"org.scala-js",
s"scalajs-compiler_$ScalaVersion",
BuildInfo.scalaJSVersion
)

private def sbtCompileDir: File = {
val dir = new File(
s"./plugin/target/scala-$ScalaVersion/classes"
Expand All @@ -67,20 +99,28 @@ private[scoverage] object ScoverageCompiler {
dir
}

private def runtimeClasses: File = new File(
s"./runtime/jvm/target/scala-$ScalaVersion/classes"
private def runtimeClasses(platform: String): File = new File(
s"./runtime/$platform/target/scala-$ScalaVersion/classes"
)

private def findScalaJar(artifactId: String): File =
findIvyJar("org.scala-lang", artifactId, ScalaVersion)
.orElse(findCoursierJar(artifactId, ScalaVersion))
findJar("org.scala-lang", artifactId, ScalaVersion)

private def findJar(
groupId: String,
artifactId: String,
version: String
): File =
findIvyJar(groupId, artifactId, version)
.orElse(findCoursierJar(groupId, artifactId, version))
.getOrElse {
throw new FileNotFoundException(
s"Could not locate $artifactId/$ScalaVersion"
s"Could not locate $groupId:$artifactId:$version"
)
}

private def findCoursierJar(
groupId: String,
artifactId: String,
version: String
): Option[File] = {
Expand All @@ -89,9 +129,10 @@ private[scoverage] object ScoverageCompiler {
".cache/coursier", // Linux
"Library/Caches/Coursier", // MacOSX
"AppData/Local/Coursier/cache" // Windows
).map(loc =>
s"$userHome/$loc/v1/https/repo1.maven.org/maven2/org/scala-lang/$artifactId/$version/$artifactId-$version.jar"
)
).map { loc =>
val gid = groupId.replace('.', '/')
s"$userHome/$loc/v1/https/repo1.maven.org/maven2/$gid/$artifactId/$version/$artifactId-$version.jar"
}
jarPaths.map(new File(_)).find(_.exists())
}

Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.33")

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")

libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0"

0 comments on commit 7eba8e1

Please sign in to comment.