Skip to content

Re-enable PluginCoverageScalaJsTest, fix dangling UndefinedParam bug #464

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

Merged
merged 14 commits into from
Jun 3, 2022
Merged
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 @@ -146,7 +147,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)
Comment on lines -149 to +151
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, it was some trickiness actually. No regression.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this make sense. 👍🏼 good catch.

.settings(
name := "scalac-scoverage-plugin",
crossTarget := target.value / s"scala-${scalaVersion.value}",
Expand All @@ -158,7 +160,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 @@ -179,6 +181,16 @@ lazy val reporter =
)
.dependsOn(domain, serializer)

lazy val buildInfo =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we have a separate build target for buildInfo instead of just including the buildInfo stuff inside of the plugin module and enabling the BuildInfoPlugin there?

Copy link
Contributor Author

@armanbilge armanbilge Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, yes I suppose we could. I did this because the BuildInfo was only needed in the Test scope and there's currently no way to configure that (see sbt/sbt-buildinfo#186). But I guess here if we "leak" the BuildInfo as part of the shipped plugin, it's probably not a big deal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was only needed in the Test scope

Yea, that's sort of what I expected. No worries. Let's leave it as is.

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

lazy val domain =
project
.settings(
Expand Down
5 changes: 3 additions & 2 deletions 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 @@ -98,11 +99,11 @@ class ScoverageInstrumentationComponent(
private var options: ScoverageOptions = ScoverageOptions.default()
private var coverageFilter: CoverageFilter = AllCoverageFilter

private val isScalaJsEnabled: Boolean = {
private lazy val isScalaJsEnabled: Boolean = {
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"