sbt/compile/src/test/scala/CompileTest.scala

94 lines
3.3 KiB
Scala
Raw Normal View History

package sbt
2011-03-02 12:46:28 +01:00
package compiler
import java.io.File
import IO.withTemporaryDirectory
import org.specs._
object CompileTest extends Specification
{
2010-03-29 02:20:17 +02:00
"Analysis compiler" should {
"compile basic sources" in {
2009-09-06 22:05:31 +02:00
WithCompiler( "2.7.4" )(testCompileAnalysis)
WithCompiler( "2.7.5" )(testCompileAnalysis)
WithCompiler( "2.7.7" )(testCompileAnalysis)
WithCompiler( "2.8.0" )(testCompileAnalysis)
2011-02-26 00:35:52 +01:00
WithCompiler( "2.8.1" )(testCompileAnalysis)
}
2010-03-29 02:20:17 +02:00
}
2011-02-26 00:35:52 +01:00
2010-03-23 01:42:59 +01:00
"Raw compiler" should {
"Properly handle classpaths" in {
testClasspath("2.7.4")
2010-03-23 01:42:59 +01:00
testClasspath("2.7.7")
testClasspath("2.8.0")
2011-02-26 00:35:52 +01:00
testClasspath("2.8.1")
2010-03-23 01:42:59 +01:00
}
}
2010-03-23 01:42:59 +01:00
private def testCompileAnalysis(compiler: AnalyzingCompiler, log: Logger)
{
WithFiles( new File("Test.scala") -> "object Test" ) { sources =>
withTemporaryDirectory { temp =>
val callback = new xsbti.TestCallback
compiler(sources, Nil, temp, Nil, callback, 10, log)
(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) =
{
2011-02-26 00:35:52 +01:00
val success = try { act; true } catch { case t if expectedException(t) => false }
2010-03-23 01:42:59 +01:00
if(success) error("Expected exception not thrown")
}
2011-02-26 00:35:52 +01:00
private def expectedException(t: Throwable) =
t match
{
case e: Exception => true
case t if isMissingRequirementError(t) => true
case _ => false
}
private def shouldSucceed(act: => Unit) =
try { act } catch { case c: xsbti.CompileFailed => error(c.toString) }
2010-03-23 01:42:59 +01:00
private def isMissingRequirementError(t: Throwable) = t.getClass.getName == "scala.tools.nsc.MissingRequirementError"
private def testClasspath(scalaVersion: String) =
WithCompiler.launcher { (launch, log) =>
2011-03-23 12:06:51 +01:00
def compiler(bootLibrary: Boolean, compilerOnClasspath: Boolean): RawCompiler =
new RawCompiler(ScalaInstance(scalaVersion, launch), new ClasspathOptions(bootLibrary, compilerOnClasspath, true, true), log)
2010-03-23 01:42:59 +01:00
val callback = new xsbti.TestCallback
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 = noCompiler.scalaInstance.compilerJar :: Nil
val withLibrary = noCompiler.scalaInstance.libraryJar :: Nil
val withLibraryCompiler = withLibrary ++ withCompiler
2010-03-23 01:42:59 +01:00
WithFiles( new File("Test.scala") -> "object Test", new File("Test2.scala") -> UsingCompiler ) { case Seq(plain, useCompiler) =>
val plainSrcs = Seq[File](plain)
val compSrcs = Seq[File](useCompiler)
withTemporaryDirectory { out =>
shouldSucceed( standard(plainSrcs, Nil, out, Nil) )//success
shouldSucceed( standard(compSrcs, Nil, out, Nil) )//success
2010-03-23 01:42:59 +01:00
shouldSucceed( noCompiler(plainSrcs, Nil, out, Nil) )//success
shouldFail( noCompiler(compSrcs, Nil, out, Nil) )
shouldSucceed( noCompiler(compSrcs, withCompiler, out, Nil) )//success
2010-03-23 01:42:59 +01:00
shouldFail( fullExplicit(plainSrcs, Nil, out, Nil) )// failure
shouldFail( fullExplicit(compSrcs, Nil, out, Nil) )// failure
shouldSucceed( fullExplicit(plainSrcs, withLibrary, out, fullBoot) )// success
shouldSucceed( fullExplicit(compSrcs, withLibraryCompiler, out, fullBoot) )// success
true must beTrue
2010-03-23 01:42:59 +01:00
}
}
}
2009-08-31 16:46:22 +02:00
}