Fix fancy uses of analysis

Clean up first analysis to make sure that test utilities using
information from analysis have correct information that does not collide
with the compilation analysis of the previous runs (batch tests).
This commit is contained in:
jvican 2017-05-02 13:08:50 +02:00
parent 927910aa72
commit dc048926c7
No known key found for this signature in database
GPG Key ID: 42DAFA0F112E8050
8 changed files with 59 additions and 12 deletions

View File

@ -1,12 +1,26 @@
import sbt.internal.inc.Analysis
import complete.DefaultParsers._
InputKey[Unit]("checkNumberOfCompilerIterations") := {
val a = (compile in Compile).value.asInstanceOf[Analysis]
val args = Def.spaceDelimited().parsed
assert(args.size == 1)
val expectedIterationsNumber = args(0).toInt
assert(a.compilations.allCompilations.size == expectedIterationsNumber,
"a.compilations.allCompilations.size = %d (expected %d)".format(
a.compilations.allCompilations.size, expectedIterationsNumber)
)
// Reset compiler iterations, necessary because tests run in batch mode
val recordPreviousIterations = taskKey[Unit]("Record previous iterations.")
recordPreviousIterations := {
CompileState.previousIterations = {
val previousAnalysis = (previousCompile in Compile).value.analysis
if (previousAnalysis.isEmpty) {
streams.value.log.info("No previous analysis detected")
0
} else {
previousAnalysis.get match {
case a: Analysis => a.compilations.allCompilations.size
}
}
}
}
val checkIterations = inputKey[Unit]("Verifies the accumulated number of iterations of incremental compilation.")
checkIterations := {
val expected: Int = (Space ~> NatBasic).parsed
val actual: Int = ((compile in Compile).value match { case a: Analysis => a.compilations.allCompilations.size }) - CompileState.previousIterations
assert(expected == actual, s"Expected $expected compilations, got $actual")
}

View File

@ -0,0 +1,4 @@
// This is necessary because tests are run in batch mode
object CompileState {
@volatile var previousIterations: Int = -1
}

View File

@ -4,6 +4,7 @@
# See https://github.com/sbt/sbt/issues/726 for details
# introduces first compile iteration
> recordPreviousIterations
> compile
# this change adds a comment and does not change api so introduces
# only one additional compile iteration
@ -11,4 +12,4 @@ $ copy-file changes/Bar1.scala src/main/scala/Bar.scala
# second iteration
#> compile
# check if there are only two compile iterations performed
> checkNumberOfCompilerIterations 2
> checkIterations 2

View File

@ -1,5 +1,4 @@
# Marked as pending, see https://github.com/sbt/sbt/issues/1543
#
# Tests if source dependencies are tracked properly
# for compile-time constants (like final vals in top-level objects)
# see https://issues.scala-lang.org/browse/SI-7173 for details

View File

@ -1,4 +1,14 @@
import sbt.internal.inc.Analysis
import xsbti.Maybe
import xsbti.compile.{PreviousResult, CompileAnalysis, MiniSetup}
previousCompile in Compile := {
if (!CompileState.isNew) {
val res = new PreviousResult(Maybe.nothing[CompileAnalysis], Maybe.nothing[MiniSetup])
CompileState.isNew = true
res
} else (previousCompile in Compile).value
}
/* Performs checks related to compilations:
* a) checks in which compilation given set of files was recompiled
@ -22,7 +32,7 @@ TaskKey[Unit]("checkCompilations") := {
val files = fileNames.map(new java.io.File(_))
assert(recompiledFiles(iteration) == files, "%s != %s".format(recompiledFiles(iteration), files))
}
assert(allCompilations.size == 2)
assert(allCompilations.size == 2, s"All compilations is ${allCompilations.size}")
// B.scala is just compiled at the beginning
recompiledFilesInIteration(0, Set("B.scala"))
// A.scala is changed and recompiled

View File

@ -0,0 +1,4 @@
// This is necessary because tests are run in batch mode
object CompileState {
@volatile var isNew: Boolean = false
}

View File

@ -1,7 +1,18 @@
import sbt.internal.inc.Analysis
import xsbti.Maybe
import xsbti.compile.{PreviousResult, CompileAnalysis, MiniSetup}
logLevel := Level.Debug
// Reset compile status because scripted tests are run in batch mode
previousCompile in Compile := {
if (!CompileState.isNew) {
val res = new PreviousResult(Maybe.nothing[CompileAnalysis], Maybe.nothing[MiniSetup])
CompileState.isNew = true
res
} else (previousCompile in Compile).value
}
// disable sbt's heuristic which recompiles everything in case
// some fraction (e.g. 50%) of files is scheduled to be recompiled
// in this test we want precise information about recompiled files

View File

@ -0,0 +1,4 @@
// This is necessary because tests are run in batch mode
object CompileState {
@volatile var isNew: Boolean = false
}