Skip to content

Commit fff9705

Browse files
committed
test progress with late compilation
1 parent f196558 commit fff9705

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

Diff for: sbt-bridge/test/xsbt/CompileProgressSpecification.scala

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ import org.junit.Assert._
88
*/
99
class CompileProgressSpecification {
1010

11+
@Test
12+
def totalIsMoreWhenSourcePath = {
13+
val srcA = """class A"""
14+
val srcB = """class B"""
15+
val extraC = """trait C""" // will only exist in the `-sourcepath`, causing a late compile
16+
val extraD = """trait D""" // will only exist in the `-sourcepath`, causing a late compile
17+
val srcE = """class E extends C""" // depends on class in the sourcepath
18+
val srcF = """class F extends C, D""" // depends on classes in the sourcepath
19+
20+
val compilerForTesting = new ScalaCompilerForUnitTesting
21+
22+
val totalA = compilerForTesting.extractTotal(srcA)()
23+
assertTrue("expected more than 1 unit of work for a single file", totalA > 1)
24+
25+
val totalB = compilerForTesting.extractTotal(srcA, srcB)()
26+
assertEquals("expected twice the work for two sources", totalA * 2, totalB)
27+
28+
val totalC = compilerForTesting.extractTotal(srcA, srcE)(extraC)
29+
assertEquals("expected 2x+1 the work for two sources, and 1 late compile", totalA * 2 + 1, totalC)
30+
31+
val totalD = compilerForTesting.extractTotal(srcA, srcF)(extraC, extraD)
32+
assertEquals("expected 2x+2 the work for two sources, and 2 late compiles", totalA * 2 + 2, totalD)
33+
}
34+
1135
@Test
1236
def multipleFilesVisitSamePhases = {
1337
val srcA = """class A"""

Diff for: sbt-bridge/test/xsbt/ScalaCompilerForUnitTesting.scala

+15-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class ScalaCompilerForUnitTesting {
3030
tempSrcFiles.map(src => run.unitPhases(src.id))
3131
}
3232

33+
def extractTotal(srcs: String*)(extraSourcePath: String*): Int = {
34+
val (tempSrcFiles, Callbacks(_, testProgress)) = compileSrcs(List(srcs.toList), extraSourcePath.toList)
35+
val run = testProgress.runs.head
36+
run.total
37+
}
38+
3339
def extractProgressPhases(srcs: String*): List[String] = {
3440
val (_, Callbacks(_, testProgress)) = compileSrcs(srcs: _*)
3541
testProgress.runs.head.phases
@@ -136,7 +142,7 @@ class ScalaCompilerForUnitTesting {
136142
* The sequence of temporary files corresponding to passed snippets and analysis
137143
* callback is returned as a result.
138144
*/
139-
def compileSrcs(groupedSrcs: List[List[String]]): (Seq[VirtualFile], Callbacks) = {
145+
def compileSrcs(groupedSrcs: List[List[String]], sourcePath: List[String] = Nil): (Seq[VirtualFile], Callbacks) = {
140146
val temp = IO.createTemporaryDirectory
141147
val analysisCallback = new TestCallback
142148
val testProgress = new TestCompileProgress
@@ -146,6 +152,11 @@ class ScalaCompilerForUnitTesting {
146152
val bridge = new CompilerBridge
147153

148154
val files = for ((compilationUnits, unitId) <- groupedSrcs.zipWithIndex) yield {
155+
val extraFiles = sourcePath.toSeq.zipWithIndex.map {
156+
case (src, i) =>
157+
val fileName = s"Extra-$unitId-$i.scala"
158+
prepareSrcFile(temp, fileName, src)
159+
}
149160
val srcFiles = compilationUnits.toSeq.zipWithIndex.map {
150161
(src, i) =>
151162
val fileName = s"Test-$unitId-$i.scala"
@@ -157,10 +168,12 @@ class ScalaCompilerForUnitTesting {
157168
val output = new SingleOutput:
158169
def getOutputDirectory() = classesDir
159170

171+
val maybeSourcePath = if extraFiles.isEmpty then Nil else List("-sourcepath", temp.getAbsolutePath.toString)
172+
160173
bridge.run(
161174
virtualSrcFiles,
162175
new TestDependencyChanges,
163-
Array("-Yforce-sbt-phases", "-classpath", classesDirPath, "-usejavacp", "-d", classesDirPath),
176+
Array("-Yforce-sbt-phases", "-classpath", classesDirPath, "-usejavacp", "-d", classesDirPath) ++ maybeSourcePath,
164177
output,
165178
analysisCallback,
166179
new TestReporter,

Diff for: sbt-bridge/test/xsbti/TestCompileProgress.scala

+3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ class TestCompileProgress extends CompileProgress:
88
class Run:
99
private[TestCompileProgress] val _phases: mutable.Set[String] = mutable.LinkedHashSet.empty
1010
private[TestCompileProgress] val _unitPhases: mutable.Map[String, mutable.Set[String]] = mutable.LinkedHashMap.empty
11+
private[TestCompileProgress] var _latestTotal: Int = 0
1112

1213
def phases: List[String] = _phases.toList
1314
def unitPhases: collection.MapView[String, List[String]] = _unitPhases.view.mapValues(_.toList)
15+
def total: Int = _latestTotal
1416

1517
private val _runs: mutable.ListBuffer[Run] = mutable.ListBuffer.empty
1618
private var _currentRun: Run = new Run
@@ -27,4 +29,5 @@ class TestCompileProgress extends CompileProgress:
2729
override def advance(current: Int, total: Int, prevPhase: String, nextPhase: String): Boolean =
2830
_currentRun._phases += prevPhase
2931
_currentRun._phases += nextPhase
32+
_currentRun._latestTotal = total
3033
true

0 commit comments

Comments
 (0)