Skip to content

Commit 7eba8e1

Browse files
authored
Re-enable PluginCoverageScalaJsTest, fix dangling UndefinedParam 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
1 parent ba4838e commit 7eba8e1

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

build.sbt

+15-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ lazy val root = Project("scalac-scoverage", file("."))
101101
runtimeJSDOMTest,
102102
reporter,
103103
domain,
104-
serializer
104+
serializer,
105+
buildInfo
105106
)
106107

107108
lazy val runtime = CrossProject(
@@ -136,7 +137,8 @@ lazy val runtimeJSDOMTest =
136137

137138
lazy val plugin =
138139
project
139-
.dependsOn(runtimeJVM % Test)
140+
// we need both runtimes compiled prior to running tests
141+
.dependsOn(runtimeJVM % Test, runtimeJS % Test)
140142
.settings(
141143
name := "scalac-scoverage-plugin",
142144
crossTarget := target.value / s"scala-${scalaVersion.value}",
@@ -148,7 +150,7 @@ lazy val plugin =
148150
.settings(
149151
Test / unmanagedSourceDirectories += (Test / sourceDirectory).value / "scala-2.12+"
150152
)
151-
.dependsOn(domain, reporter % "test->compile", serializer)
153+
.dependsOn(domain, reporter % "test->compile", serializer, buildInfo % Test)
152154

153155
lazy val reporter =
154156
project
@@ -169,6 +171,16 @@ lazy val reporter =
169171
)
170172
.dependsOn(domain, serializer)
171173

174+
lazy val buildInfo =
175+
project
176+
.settings(
177+
crossScalaVersions := bin212 ++ bin213,
178+
buildInfoKeys += BuildInfoKey("scalaJSVersion", scalaJSVersion),
179+
publishArtifact := false,
180+
publishLocal := {}
181+
)
182+
.enablePlugins(BuildInfoPlugin)
183+
172184
lazy val domain =
173185
project
174186
.settings(

plugin/src/main/scala/scoverage/ScoveragePlugin.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import scala.tools.nsc.plugins.Plugin
1010
import scala.tools.nsc.plugins.PluginComponent
1111
import scala.tools.nsc.transform.Transform
1212
import scala.tools.nsc.transform.TypingTransformers
13+
import scala.util.control.NonFatal
1314

1415
import scoverage.domain.Coverage
1516
import scoverage.domain.Statement
@@ -102,7 +103,7 @@ class ScoverageInstrumentationComponent(
102103
try {
103104
rootMirror.getClassIfDefined("scala.scalajs.js.Any") != NoSymbol
104105
} catch {
105-
case _: Throwable => false
106+
case NonFatal(_) => false
106107
}
107108
}
108109

plugin/src/test/scala/scoverage/PluginCoverageScalaJsTest.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import munit.FunSuite
66
*/
77
class PluginCoverageScalaJsTest extends FunSuite with MacroSupport {
88

9-
test("scoverage should ignore default undefined parameter".ignore) {
10-
val compiler = ScoverageCompiler.default
9+
test("scoverage should ignore default undefined parameter") {
10+
val compiler = ScoverageCompiler.defaultJS
1111
compiler.compileCodeSnippet(
1212
"""import scala.scalajs.js
1313
|
@@ -16,6 +16,6 @@ class PluginCoverageScalaJsTest extends FunSuite with MacroSupport {
1616
|}""".stripMargin
1717
)
1818
assert(!compiler.reporter.hasErrors)
19-
compiler.assertNMeasuredStatements(4)
19+
compiler.assertNMeasuredStatements(2)
2020
}
2121
}

plugin/src/test/scala/scoverage/ScoverageCompiler.scala

+51-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import scala.tools.nsc.plugins.PluginComponent
1111
import scala.tools.nsc.transform.Transform
1212
import scala.tools.nsc.transform.TypingTransformers
1313

14+
import buildinfo.BuildInfo
1415
import scoverage.reporter.IOUtils
1516

1617
private[scoverage] object ScoverageCompiler {
@@ -24,9 +25,22 @@ private[scoverage] object ScoverageCompiler {
2425
def classPath: Seq[String] =
2526
getScalaJars.map(
2627
_.getAbsolutePath
27-
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses.getAbsolutePath
28+
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses("jvm").getAbsolutePath
2829

29-
def settings: Settings = {
30+
def jsClassPath: Seq[String] =
31+
getScalaJsJars.map(
32+
_.getAbsolutePath
33+
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses("js").getAbsolutePath
34+
35+
def settings: Settings = settings(classPath)
36+
37+
def jsSettings: Settings = {
38+
val s = settings(jsClassPath)
39+
s.plugin.value = List(getScalaJsCompilerJar.getAbsolutePath)
40+
s
41+
}
42+
43+
def settings(classPath: Seq[String]): Settings = {
3044
val s = new scala.tools.nsc.Settings
3145
s.Xprint.value = List("all", "_")
3246
s.deprecation.value = true
@@ -46,6 +60,11 @@ private[scoverage] object ScoverageCompiler {
4660
new ScoverageCompiler(settings, reporter)
4761
}
4862

63+
def defaultJS: ScoverageCompiler = {
64+
val reporter = new scala.tools.nsc.reporters.ConsoleReporter(jsSettings)
65+
new ScoverageCompiler(jsSettings, reporter)
66+
}
67+
4968
def locationCompiler: LocationCompiler = {
5069
val reporter = new scala.tools.nsc.reporters.ConsoleReporter(settings)
5170
new LocationCompiler(settings, reporter)
@@ -56,6 +75,19 @@ private[scoverage] object ScoverageCompiler {
5675
scalaJars.map(findScalaJar)
5776
}
5877

78+
private def getScalaJsJars: List[File] =
79+
findJar(
80+
"org.scala-js",
81+
s"scalajs-library_$ShortScalaVersion",
82+
BuildInfo.scalaJSVersion
83+
) :: getScalaJars
84+
85+
private def getScalaJsCompilerJar: File = findJar(
86+
"org.scala-js",
87+
s"scalajs-compiler_$ScalaVersion",
88+
BuildInfo.scalaJSVersion
89+
)
90+
5991
private def sbtCompileDir: File = {
6092
val dir = new File(
6193
s"./plugin/target/scala-$ScalaVersion/classes"
@@ -67,20 +99,28 @@ private[scoverage] object ScoverageCompiler {
6799
dir
68100
}
69101

70-
private def runtimeClasses: File = new File(
71-
s"./runtime/jvm/target/scala-$ScalaVersion/classes"
102+
private def runtimeClasses(platform: String): File = new File(
103+
s"./runtime/$platform/target/scala-$ScalaVersion/classes"
72104
)
73105

74106
private def findScalaJar(artifactId: String): File =
75-
findIvyJar("org.scala-lang", artifactId, ScalaVersion)
76-
.orElse(findCoursierJar(artifactId, ScalaVersion))
107+
findJar("org.scala-lang", artifactId, ScalaVersion)
108+
109+
private def findJar(
110+
groupId: String,
111+
artifactId: String,
112+
version: String
113+
): File =
114+
findIvyJar(groupId, artifactId, version)
115+
.orElse(findCoursierJar(groupId, artifactId, version))
77116
.getOrElse {
78117
throw new FileNotFoundException(
79-
s"Could not locate $artifactId/$ScalaVersion"
118+
s"Could not locate $groupId:$artifactId:$version"
80119
)
81120
}
82121

83122
private def findCoursierJar(
123+
groupId: String,
84124
artifactId: String,
85125
version: String
86126
): Option[File] = {
@@ -89,9 +129,10 @@ private[scoverage] object ScoverageCompiler {
89129
".cache/coursier", // Linux
90130
"Library/Caches/Coursier", // MacOSX
91131
"AppData/Local/Coursier/cache" // Windows
92-
).map(loc =>
93-
s"$userHome/$loc/v1/https/repo1.maven.org/maven2/org/scala-lang/$artifactId/$version/$artifactId-$version.jar"
94-
)
132+
).map { loc =>
133+
val gid = groupId.replace('.', '/')
134+
s"$userHome/$loc/v1/https/repo1.maven.org/maven2/$gid/$artifactId/$version/$artifactId-$version.jar"
135+
}
95136
jarPaths.map(new File(_)).find(_.exists())
96137
}
97138

project/plugins.sbt

+2
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")
66

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

9+
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")
10+
911
libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0"

0 commit comments

Comments
 (0)