2009-08-24 04:21:15 +02:00
|
|
|
package xsbt
|
|
|
|
|
|
2010-01-16 01:05:23 +01:00
|
|
|
import sbt.{ComponentManager, TestIvyLogger}
|
|
|
|
|
|
2009-08-24 04:21:15 +02:00
|
|
|
import java.io.File
|
|
|
|
|
import FileUtilities.withTemporaryDirectory
|
|
|
|
|
import org.specs._
|
|
|
|
|
|
|
|
|
|
object CompileTest extends Specification
|
|
|
|
|
{
|
2010-03-29 02:20:17 +02:00
|
|
|
"Analysis compiler" should {
|
2009-08-24 04:21:15 +02:00
|
|
|
"compile basic sources" in {
|
2009-08-31 16:46:22 +02:00
|
|
|
WithCompiler( "2.7.2" )(testCompileAnalysis)
|
2009-09-06 22:05:31 +02:00
|
|
|
WithCompiler( "2.7.3" )(testCompileAnalysis)
|
|
|
|
|
WithCompiler( "2.7.4" )(testCompileAnalysis)
|
|
|
|
|
WithCompiler( "2.7.5" )(testCompileAnalysis)
|
2010-01-31 03:40:25 +01:00
|
|
|
WithCompiler( "2.7.7" )(testCompileAnalysis)
|
|
|
|
|
WithCompiler( "2.8.0.Beta1" )(testCompileAnalysis)
|
2010-04-06 00:47:06 +02:00
|
|
|
WithCompiler( "2.8.0-SNAPSHOT" )(testCompileAnalysis)
|
2009-08-24 04:21:15 +02:00
|
|
|
}
|
2010-03-29 02:20:17 +02:00
|
|
|
}
|
2010-03-23 01:42:59 +01:00
|
|
|
|
|
|
|
|
"Raw compiler" should {
|
|
|
|
|
"Properly handle classpaths" in {
|
|
|
|
|
testClasspath("2.7.2")
|
|
|
|
|
testClasspath("2.7.7")
|
|
|
|
|
testClasspath("2.8.0.Beta1")
|
|
|
|
|
}
|
2009-08-24 04:21:15 +02:00
|
|
|
}
|
2010-03-23 01:42:59 +01:00
|
|
|
|
2009-09-05 21:01:04 +02:00
|
|
|
private def testCompileAnalysis(compiler: AnalyzingCompiler, log: CompileLogger)
|
2009-08-24 04:21:15 +02:00
|
|
|
{
|
|
|
|
|
WithFiles( new File("Test.scala") -> "object Test" ) { sources =>
|
|
|
|
|
withTemporaryDirectory { temp =>
|
2010-03-29 02:20:17 +02:00
|
|
|
val callback = new xsbti.TestCallback(Array(), Array())
|
2009-09-05 18:19:34 +02:00
|
|
|
compiler(Set() ++ sources, Set.empty, temp, Nil, callback, 10, log)
|
2009-08-24 04:21:15 +02:00
|
|
|
(callback.beganSources) must haveTheSameElementsAs(sources)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-23 01:42:59 +01:00
|
|
|
|
|
|
|
|
val UsingCompiler = "object Test { classOf[scala.tools.nsc.Global] }"
|
|
|
|
|
|
|
|
|
|
private def shouldFail(act: => Unit) =
|
|
|
|
|
{
|
|
|
|
|
val success = try { act; true } catch { case e: Exception => false }
|
|
|
|
|
if(success) error("Expected exception not thrown")
|
|
|
|
|
}
|
|
|
|
|
private def isMissingRequirementError(t: Throwable) = t.getClass.getName == "scala.tools.nsc.MissingRequirementError"
|
|
|
|
|
private def testClasspath(scalaVersion: String) =
|
|
|
|
|
WithCompiler.launcher { (launch, log) =>
|
|
|
|
|
def compiler(autoBoot: Boolean, compilerOnClasspath: Boolean): RawCompiler =
|
2010-05-14 00:32:49 +02:00
|
|
|
new RawCompiler(ScalaInstance(scalaVersion, launch), new ClasspathOptions(autoBoot, compilerOnClasspath, true), log)
|
2010-03-23 01:42:59 +01:00
|
|
|
|
2010-03-29 02:20:17 +02:00
|
|
|
val callback = new xsbti.TestCallback(Array(), Array())
|
2010-03-23 01:42:59 +01:00
|
|
|
|
|
|
|
|
val standard = compiler(true, true)
|
|
|
|
|
val noCompiler = compiler(true, false)
|
|
|
|
|
val fullExplicit = compiler(false, false)
|
|
|
|
|
|
|
|
|
|
val fullBoot = "-bootclasspath" :: fullExplicit.compilerArguments.createBootClasspath :: Nil
|
|
|
|
|
val withCompiler = Set() + noCompiler.scalaInstance.compilerJar
|
|
|
|
|
|
|
|
|
|
WithFiles( new File("Test.scala") -> "object Test", new File("Test2.scala") -> UsingCompiler ) { case Array(plain, useCompiler) =>
|
|
|
|
|
val plainSrcs = Set[File](plain)
|
|
|
|
|
val compSrcs = Set[File](useCompiler)
|
|
|
|
|
FileUtilities.withTemporaryDirectory { out =>
|
|
|
|
|
standard(plainSrcs, Set.empty, out, Nil) //success
|
|
|
|
|
standard(compSrcs, Set.empty, out, Nil) //success
|
|
|
|
|
|
|
|
|
|
noCompiler(plainSrcs, Set.empty, out, Nil) //success
|
|
|
|
|
shouldFail( noCompiler(compSrcs, Set.empty, out, Nil) )
|
|
|
|
|
noCompiler(compSrcs, withCompiler, out, Nil) //success
|
|
|
|
|
|
|
|
|
|
shouldFail( fullExplicit(plainSrcs, Set.empty, out, Nil) )// failure
|
|
|
|
|
shouldFail( fullExplicit(compSrcs, Set.empty, out, Nil) )// failure
|
|
|
|
|
fullExplicit(plainSrcs, Set.empty, out, fullBoot) // success
|
|
|
|
|
fullExplicit(compSrcs, withCompiler, out, fullBoot) // success
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-31 16:46:22 +02:00
|
|
|
}
|
|
|
|
|
object WithCompiler
|
|
|
|
|
{
|
2009-09-05 21:01:04 +02:00
|
|
|
def apply[T](scalaVersion: String)(f: (AnalyzingCompiler, CompileLogger) => T): T =
|
2009-08-31 16:46:22 +02:00
|
|
|
{
|
2010-03-23 01:42:59 +01:00
|
|
|
launcher { (launch, log) =>
|
|
|
|
|
FileUtilities.withTemporaryDirectory { componentDirectory =>
|
|
|
|
|
val manager = new ComponentManager(xsbt.boot.Locks, new boot.ComponentProvider(componentDirectory), log)
|
|
|
|
|
val compiler = new AnalyzingCompiler(ScalaInstance(scalaVersion, launch), manager, log)
|
|
|
|
|
compiler.newComponentCompiler(log).clearCache(ComponentCompiler.compilerInterfaceID)
|
|
|
|
|
define(manager, ComponentCompiler.compilerInterfaceSrcID, getResource("CompilerInterface.scala"), getClassResource(classOf[jline.Completor]))
|
|
|
|
|
define(manager, ComponentCompiler.xsbtiID, getClassResource(classOf[xsbti.AnalysisCallback]))
|
|
|
|
|
f(compiler, log)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
def launcher[T](f: (xsbti.Launcher, TestIvyLogger with CompileLogger) => T): T =
|
|
|
|
|
{
|
2009-09-05 18:19:34 +02:00
|
|
|
val log = new TestIvyLogger with CompileLogger
|
|
|
|
|
log.setLevel(Level.Debug)
|
|
|
|
|
log.bufferQuietly {
|
2010-03-23 01:42:59 +01:00
|
|
|
boot.LaunchTest.withLauncher { launch => f(launch, log) }
|
2009-08-31 16:46:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-02-09 00:34:43 +01:00
|
|
|
def getClassResource(resource: Class[_]): File = FileUtilities.classLocationFile(resource)
|
|
|
|
|
def getResource(resource: String): File =
|
2009-08-24 04:21:15 +02:00
|
|
|
{
|
|
|
|
|
val src = getClass.getClassLoader.getResource(resource)
|
2010-02-09 00:34:43 +01:00
|
|
|
if(src ne null) FileUtilities.asFile(src) else error("Resource not found: " + resource)
|
2009-10-03 15:39:16 +02:00
|
|
|
}
|
2010-02-09 00:34:43 +01:00
|
|
|
def define(manager: ComponentManager, id: String, files: File*)
|
2009-10-03 15:39:16 +02:00
|
|
|
{
|
|
|
|
|
manager.clearCache(id)
|
|
|
|
|
manager.define(id, files)
|
2009-08-24 04:21:15 +02:00
|
|
|
}
|
|
|
|
|
}
|