From 6351c6cc8c3014fd1fbb8244e872c9e28773dad0 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 19 Oct 2016 15:30:27 +0200 Subject: Create dotty-lib.jar for run tests --- test/test/CompilerTest.scala | 42 +++++++++++++++++++++++++++------ test/test/DottyTest.scala | 9 ++++++- test/test/InterfaceEntryPointTest.scala | 15 ++++++++++-- 3 files changed, 56 insertions(+), 10 deletions(-) (limited to 'test/test') diff --git a/test/test/CompilerTest.scala b/test/test/CompilerTest.scala index 8cf6b2feb..fe63106b5 100644 --- a/test/test/CompilerTest.scala +++ b/test/test/CompilerTest.scala @@ -124,6 +124,26 @@ abstract class CompilerTest { compileFile(prefix, fileName, args, extension, true) } + def findJarFromRuntime(partialName: String): String = { + val urls = ClassLoader.getSystemClassLoader.asInstanceOf[java.net.URLClassLoader].getURLs.map(_.getFile.toString) + urls.find(_.contains(partialName)).getOrElse { + throw new java.io.FileNotFoundException( + s"""Unable to locate $partialName on classpath:\n${urls.toList.mkString("\n")}""" + ) + } + } + + private def compileWithJavac(fs: Array[String], args: Array[String]): Unit = { + val scalaLib = findJarFromRuntime("scala-library") + val fullArgs = Array( + "javac", + "-classpath", + s".:$scalaLib" + ) ++ args ++ fs ++ Array("-d", defaultOutputDir) + + Runtime.getRuntime.exec(fullArgs).waitFor() + } + /** Compiles the code files in the given directory together. If args starts * with "-deep", all files in subdirectories (and so on) are included. */ def compileDir(prefix: String, dirName: String, args: List[String] = Nil, runTest: Boolean = false) @@ -134,14 +154,21 @@ abstract class CompilerTest { case "-deep" :: args1 => (dir.deepFiles, args1) case _ => (dir.files, args) } - val filePaths = files.toArray.map(_.toString).filter(name => (name endsWith ".scala") || (name endsWith ".java")) + val (filePaths, javaFilePaths) = files + .toArray.map(_.toString) + .foldLeft((Array.empty[String], Array.empty[String])) { case (acc @ (fp, jfp), name) => + if (name endsWith ".scala") (name +: fp, jfp) + else if (name endsWith ".java") (fp, name +: jfp) + else (fp, jfp) + } val expErrors = expectedErrors(filePaths.toList) - (filePaths, normArgs, expErrors) + (filePaths, javaFilePaths, normArgs, expErrors) } if (!generatePartestFiles || !partestableDir(prefix, dirName, args ++ defaultOptions)) { if (runTest) log(s"WARNING: run tests can only be run by partest, JUnit just verifies compilation: $prefix$dirName") - val (filePaths, normArgs, expErrors) = computeFilePathsAndExpErrors + val (filePaths, javaFilePaths, normArgs, expErrors) = computeFilePathsAndExpErrors + compileWithJavac(javaFilePaths, Array.empty) // javac needs to run first on dotty-library compileArgs(filePaths ++ normArgs, expErrors) } else { val (sourceDir, flags, deep) = args match { @@ -154,7 +181,7 @@ abstract class CompilerTest { if (sourceDir.exists) { val firstDest = Directory(DPConfig.testRoot + JFile.separator + kind + JFile.separator + dirName) val xerrors = if (isNegTest(prefix)) { - val (_, _, expErrors) = computeFilePathsAndExpErrors + val (_, _, _, expErrors) = computeFilePathsAndExpErrors expErrors.map(_.totalErrors).sum } else 0 computeDestAndCopyFiles(sourceDir, firstDest, kind, flags, xerrors.toString) @@ -234,6 +261,7 @@ abstract class CompilerTest { private def compileArgs(args: Array[String], expectedErrorsPerFile: List[ErrorsInFile]) (implicit defaultOptions: List[String]): Unit = { val allArgs = args ++ defaultOptions + //println(s"""all args: ${allArgs.mkString("\n")}""") val processor = if (allArgs.exists(_.startsWith("#"))) Bench else Main val storeReporter = new Reporter with UniqueMessagePositions with HideNonSensicalMessages { private val consoleReporter = new ConsoleReporter() @@ -472,16 +500,16 @@ abstract class CompilerTest { try { SFile(dest)(scala.io.Codec.UTF8).writeAll((s"/* !!!!! WARNING: DO NOT MODIFY. Original is at: $file !!!!! */").replace("\\", "/"), file.slurp("UTF-8")) } catch { - case unmappable: java.nio.charset.MalformedInputException => + case unmappable: java.nio.charset.MalformedInputException => copyfile(file, true) //there are bytes that can't be mapped with UTF-8. Bail and just do a straight byte-wise copy without the warning header. } } } - processFileDir(sourceFile, { sf => + processFileDir(sourceFile, { sf => if (extensionsToCopy.contains(sf.extension)) { dest.parent.jfile.mkdirs - copyfile(sf, false) + copyfile(sf, false) } else { log(s"WARNING: ignoring $sf") } diff --git a/test/test/DottyTest.scala b/test/test/DottyTest.scala index 4c8cd8a7b..57bd9bbc4 100644 --- a/test/test/DottyTest.scala +++ b/test/test/DottyTest.scala @@ -22,8 +22,14 @@ class DottyTest extends ContextEscapeDetection{ val base = new ContextBase {} import base.settings._ val ctx = base.initialCtx.fresh - base.initialize()(ctx) ctx.setSetting(ctx.settings.encoding, "UTF8") + ctx.setSetting( + ctx.settings.classpath, + "./library/target/scala-2.11/dotty-library_2.11-0.1-SNAPSHOT.jar" + ) + // when classpath is changed in ctx, we need to re-initialize to get the + // correct classpath from PathResolver + base.initialize()(ctx) ctx } @@ -31,6 +37,7 @@ class DottyTest extends ContextEscapeDetection{ override def clearCtx() = { ctx = null } + private def compilerWithChecker(phase: String)(assertion:(tpd.Tree, Context) => Unit) = new Compiler { override def phases = { val allPhases = super.phases diff --git a/test/test/InterfaceEntryPointTest.scala b/test/test/InterfaceEntryPointTest.scala index 438a9fa47..a1a8433d7 100644 --- a/test/test/InterfaceEntryPointTest.scala +++ b/test/test/InterfaceEntryPointTest.scala @@ -18,8 +18,17 @@ import scala.collection.mutable.ListBuffer */ class InterfaceEntryPointTest { @Test def runCompilerFromInterface = { - val sources = List("./tests/pos/HelloWorld.scala").map(p => new java.io.File(p).getPath()) - val args = sources ++ List("-d", "./out/") + val sources = + List("./tests/pos/HelloWorld.scala").map(p => new java.io.File(p).getPath()) + val dottyInterfaces = + new java.io.File("./interfaces/dotty-interfaces-0.1-SNAPSHOT.jar").getPath + val dottyLibrary = + new java.io.File("./library/target/scala-2.11/dotty-library_2.11-0.1-SNAPSHOT.jar").getPath + + val args = + sources ++ + List("-d", "./out/") ++ + List("-classpath", dottyInterfaces + ":" + dottyLibrary) val mainClass = Class.forName("dotty.tools.dotc.Main") val process = mainClass.getMethod("process", @@ -45,6 +54,8 @@ class InterfaceEntryPointTest { errorCount += 1 if (diag.level == Diagnostic.WARNING) warningCount += 1 + + println(diag.message) } } -- cgit v1.2.3