Skip to content

Commit d5aa4f0

Browse files
author
Jaden Peterson
committed
fixup! f96fe2e Don't consider frameworks used if we found no tests with them
1 parent 078f10c commit d5aa4f0

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/main/scala/higherkindness/rules_scala/common/sbt-testing/Test.scala

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object TestDefinition {
1313
}
1414

1515
class TestFrameworkLoader(loader: ClassLoader) {
16-
private val loadedJars = mutable.ArrayBuffer[Path]()
16+
private val loadedJarsByFramework = mutable.Map.empty[String, mutable.ArrayBuffer[Path]]
1717
private def getClassJar(`class`: Class[?]): Option[Path] = for {
1818
codeSource <- Option(`class`.getProtectionDomain().getCodeSource())
1919
codeSourceUri = codeSource.getLocation().toURI()
@@ -25,22 +25,27 @@ class TestFrameworkLoader(loader: ClassLoader) {
2525
}
2626
} yield path
2727

28-
def getLoadedJars(): Array[Path] = loadedJars.toArray
28+
def getLoadedJarsByFramework(): Map[String, Array[Path]] =
29+
loadedJarsByFramework.view.map { case (frameworkName, loadedJars) => frameworkName -> loadedJars.toArray }.toMap
30+
2931
def load(className: String) = {
30-
val framework =
32+
val (framework, loadedJar) =
3133
try {
3234
val `class` = Class.forName(className, true, loader)
35+
val loadedJar = getClassJar(`class`)
3336

34-
getClassJar(`class`).foreach(loadedJars += _)
35-
36-
Some(`class`.getDeclaredConstructor().newInstance())
37+
(Some(`class`.getDeclaredConstructor().newInstance()), loadedJar)
3738
} catch {
38-
case _: ClassNotFoundException => None
39+
case _: ClassNotFoundException => (None, None)
3940
case NonFatal(e) => throw new Exception(s"Failed to load framework $className", e)
4041
}
4142
framework.map {
42-
case framework: Framework => framework
43-
case _ => throw new Exception(s"$className does not implement ${classOf[Framework].getName}")
43+
case framework: Framework =>
44+
loadedJar.foreach(loadedJarsByFramework.getOrElseUpdate(className, mutable.ArrayBuffer.empty) += _)
45+
46+
framework
47+
48+
case _ => throw new Exception(s"$className does not implement ${classOf[Framework].getName}")
4449
}
4550

4651
}

src/main/scala/higherkindness/rules_scala/workers/zinc/compile/ZincRunner.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,17 @@ object ZincRunner extends WorkerMain[Unit] {
225225
.toMap,
226226
)
227227

228+
val loadedJarsByFramework = frameworkLoader.getLoadedJarsByFramework()
229+
val loadedJars = testsFileData.testsByFramework.view
230+
.filter { case (_, tests) => tests.nonEmpty }
231+
.keys
232+
.flatMap(loadedJarsByFramework.get)
233+
.flatten
234+
.toArray
235+
228236
Files.write(path, Json.stringify(Json.toJson(testsFileData)).getBytes(StandardCharsets.UTF_8))
229237

230-
frameworkLoader.getLoadedJars()
238+
loadedJars
231239
}
232240
.getOrElse(Array.empty)
233241

@@ -304,6 +312,15 @@ object ZincRunner extends WorkerMain[Unit] {
304312

305313
InterruptUtil.throwIfInterrupted(task.isCancelled)
306314

315+
/*
316+
* JARs on the classpath used during test discovery, but not used by compiler won't show up in `analysis.usedJars`, so
317+
* we need to consider them in addition to those used by the compiler.
318+
*
319+
* Test discovery works by iterating through the list of test frameworks provided via `workRequest.testFrameworks`,
320+
* attempting to classload each framework, and then using that framework to discover tests. If a given framework
321+
* successfully loads and we found a test with it we take note of the JAR it was loaded from and consider that JAR
322+
* (and therefore the target to which it belongs) used.
323+
*/
307324
val usedDepsDuringTestDiscovery = maybeWriteTestsFile(workRequest, analysis)
308325

309326
InterruptUtil.throwIfInterrupted(task.isCancelled)

0 commit comments

Comments
 (0)