Skip to content

Commit

Permalink
test progress with late compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Oct 24, 2023
1 parent c0190c2 commit 84f5cdf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
24 changes: 24 additions & 0 deletions sbt-bridge/test/xsbt/CompileProgressSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ import org.junit.Assert._
*/
class CompileProgressSpecification {

@Test
def totalIsMoreWhenSourcePath = {
val srcA = """class A"""
val srcB = """class B"""
val extraC = """trait C""" // will only exist in the `-sourcepath`, causing a late compile
val extraD = """trait D""" // will only exist in the `-sourcepath`, causing a late compile
val srcE = """class E extends C""" // depends on class in the sourcepath
val srcF = """class F extends C, D""" // depends on classes in the sourcepath

val compilerForTesting = new ScalaCompilerForUnitTesting

val totalA = compilerForTesting.extractTotal(srcA)()
assertTrue("expected more than 1 unit of work for a single file", totalA > 1)

val totalB = compilerForTesting.extractTotal(srcA, srcB)()
assertEquals("expected twice the work for two sources", totalA * 2, totalB)

val totalC = compilerForTesting.extractTotal(srcA, srcE)(extraC)
assertEquals("expected 2x+1 the work for two sources, and 1 late compile", totalA * 2 + 1, totalC)

val totalD = compilerForTesting.extractTotal(srcA, srcF)(extraC, extraD)
assertEquals("expected 2x+2 the work for two sources, and 2 late compiles", totalA * 2 + 2, totalD)
}

@Test
def multipleFilesVisitSamePhases = {
val srcA = """class A"""
Expand Down
17 changes: 15 additions & 2 deletions sbt-bridge/test/xsbt/ScalaCompilerForUnitTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class ScalaCompilerForUnitTesting {
tempSrcFiles.map(src => run.unitPhases(src.id))
}

def extractTotal(srcs: String*)(extraSourcePath: String*): Int = {
val (tempSrcFiles, Callbacks(_, testProgress)) = compileSrcs(List(srcs.toList), extraSourcePath.toList)
val run = testProgress.runs.head
run.total
}

def extractProgressPhases(srcs: String*): List[String] = {
val (_, Callbacks(_, testProgress)) = compileSrcs(srcs: _*)
testProgress.runs.head.phases
Expand Down Expand Up @@ -136,7 +142,7 @@ class ScalaCompilerForUnitTesting {
* The sequence of temporary files corresponding to passed snippets and analysis
* callback is returned as a result.
*/
def compileSrcs(groupedSrcs: List[List[String]]): (Seq[VirtualFile], Callbacks) = {
def compileSrcs(groupedSrcs: List[List[String]], sourcePath: List[String] = Nil): (Seq[VirtualFile], Callbacks) = {
val temp = IO.createTemporaryDirectory
val analysisCallback = new TestCallback
val testProgress = new TestCompileProgress
Expand All @@ -146,6 +152,11 @@ class ScalaCompilerForUnitTesting {
val bridge = new CompilerBridge

val files = for ((compilationUnits, unitId) <- groupedSrcs.zipWithIndex) yield {
val extraFiles = sourcePath.toSeq.zipWithIndex.map {
case (src, i) =>
val fileName = s"Extra-$unitId-$i.scala"
prepareSrcFile(temp, fileName, src)
}
val srcFiles = compilationUnits.toSeq.zipWithIndex.map {
(src, i) =>
val fileName = s"Test-$unitId-$i.scala"
Expand All @@ -157,10 +168,12 @@ class ScalaCompilerForUnitTesting {
val output = new SingleOutput:
def getOutputDirectory() = classesDir

val maybeSourcePath = if extraFiles.isEmpty then Nil else List("-sourcepath", temp.getAbsolutePath.toString)

bridge.run(
virtualSrcFiles,
new TestDependencyChanges,
Array("-Yforce-sbt-phases", "-classpath", classesDirPath, "-usejavacp", "-d", classesDirPath),
Array("-Yforce-sbt-phases", "-classpath", classesDirPath, "-usejavacp", "-d", classesDirPath) ++ maybeSourcePath,
output,
analysisCallback,
new TestReporter,
Expand Down
3 changes: 3 additions & 0 deletions sbt-bridge/test/xsbti/TestCompileProgress.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ class TestCompileProgress extends CompileProgress:
class Run:
private[TestCompileProgress] val _phases: mutable.Set[String] = mutable.LinkedHashSet.empty
private[TestCompileProgress] val _unitPhases: mutable.Map[String, mutable.Set[String]] = mutable.LinkedHashMap.empty
private[TestCompileProgress] var _latestTotal: Int = 0

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

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

0 comments on commit 84f5cdf

Please sign in to comment.