summaryrefslogtreecommitdiff
path: root/test/files/run
diff options
context:
space:
mode:
authorGrzegorz Kossakowski <grzegorz.kossakowski@gmail.com>2014-12-05 14:19:00 +0100
committerGrzegorz Kossakowski <grzegorz.kossakowski@gmail.com>2014-12-05 14:19:00 +0100
commit124cf2f62f559caf37a5d5df7e15db7ba5958bcf (patch)
tree7cf981e5940bc70c8dfa2def557c631f8dc6c81e /test/files/run
parent59832b0afedeaae8d0c1a48cf92d2b5f529ccd82 (diff)
parent35811876a3a089706951620e2434d171090ac0b0 (diff)
downloadscala-124cf2f62f559caf37a5d5df7e15db7ba5958bcf.tar.gz
scala-124cf2f62f559caf37a5d5df7e15db7ba5958bcf.tar.bz2
scala-124cf2f62f559caf37a5d5df7e15db7ba5958bcf.zip
Merge pull request #4176 from mpociecha/flat-classpath2
The alternative, flat representation of classpath elements
Diffstat (limited to 'test/files/run')
-rw-r--r--test/files/run/t6502.scala27
-rw-r--r--test/files/run/t6669.scala7
-rw-r--r--test/files/run/various-flat-classpath-types.check12
-rw-r--r--test/files/run/various-flat-classpath-types.scala214
4 files changed, 252 insertions, 8 deletions
diff --git a/test/files/run/t6502.scala b/test/files/run/t6502.scala
index ced1b5812d..4ce034a482 100644
--- a/test/files/run/t6502.scala
+++ b/test/files/run/t6502.scala
@@ -1,6 +1,7 @@
-import scala.tools.partest._
-import java.io.File
+import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.ILoop
+import scala.tools.nsc.settings.ClassPathRepresentationType
+import scala.tools.partest._
object Test extends StoreReporterDirectTest {
def code = ???
@@ -10,6 +11,14 @@ object Test extends StoreReporterDirectTest {
compileString(newCompiler("-cp", classpath, "-d", s"${testOutput.path}/$jarFileName"))(code)
}
+ // TODO flat classpath doesn't support the classpath invalidation yet so we force using the recursive one
+ // it's the only test which needed such a workaround
+ override def settings = {
+ val settings = new Settings
+ settings.YclasspathImpl.value = ClassPathRepresentationType.Recursive
+ settings
+ }
+
def app1 = """
package test
@@ -41,7 +50,8 @@ object Test extends StoreReporterDirectTest {
val jar = "test1.jar"
compileCode(app1, jar)
- val output = ILoop.run(List(s":require ${testOutput.path}/$jar", "test.Test.test()"))
+ val codeToRun = toCodeInSeparateLines(s":require ${testOutput.path}/$jar", "test.Test.test()")
+ val output = ILoop.run(codeToRun, settings)
val lines = output.split("\n")
val res1 = lines(4).contains("Added") && lines(4).contains("test1.jar")
val res2 = lines(lines.length-3).contains("testing...")
@@ -56,7 +66,8 @@ object Test extends StoreReporterDirectTest {
val jar2 = "test2.jar"
compileCode(app2, jar2)
- val output = ILoop.run(List(s":require ${testOutput.path}/$jar1", s":require ${testOutput.path}/$jar2"))
+ val codeToRun = toCodeInSeparateLines(s":require ${testOutput.path}/$jar1", s":require ${testOutput.path}/$jar2")
+ val output = ILoop.run(codeToRun, settings)
val lines = output.split("\n")
val res1 = lines(4).contains("Added") && lines(4).contains("test1.jar")
val res2 = lines(lines.length-3).contains("test2.jar") && lines(lines.length-3).contains("existing classpath entries conflict")
@@ -71,7 +82,8 @@ object Test extends StoreReporterDirectTest {
val jar3 = "test3.jar"
compileCode(app3, jar3)
- val output = ILoop.run(List(s":require ${testOutput.path}/$jar1", s":require ${testOutput.path}/$jar3", "test.Test3.test()"))
+ val codeToRun = toCodeInSeparateLines(s":require ${testOutput.path}/$jar1", s":require ${testOutput.path}/$jar3", "test.Test3.test()")
+ val output = ILoop.run(codeToRun, settings)
val lines = output.split("\n")
val res1 = lines(4).contains("Added") && lines(4).contains("test1.jar")
val res2 = lines(lines.length-3).contains("new object in existing package")
@@ -83,7 +95,8 @@ object Test extends StoreReporterDirectTest {
def test4(): Unit = {
// twice the same jar should be rejected
val jar1 = "test1.jar"
- val output = ILoop.run(List(s":require ${testOutput.path}/$jar1", s":require ${testOutput.path}/$jar1"))
+ val codeToRun = toCodeInSeparateLines(s":require ${testOutput.path}/$jar1", s":require ${testOutput.path}/$jar1")
+ val output = ILoop.run(codeToRun, settings)
val lines = output.split("\n")
val res1 = lines(4).contains("Added") && lines(4).contains("test1.jar")
val res2 = lines(lines.length-3).contains("test1.jar") && lines(lines.length-3).contains("existing classpath entries conflict")
@@ -98,4 +111,6 @@ object Test extends StoreReporterDirectTest {
test3()
test4()
}
+
+ def toCodeInSeparateLines(lines: String*): String = lines.map(_ + "\n").mkString
}
diff --git a/test/files/run/t6669.scala b/test/files/run/t6669.scala
index e18f2514a9..27c4970d60 100644
--- a/test/files/run/t6669.scala
+++ b/test/files/run/t6669.scala
@@ -1,4 +1,5 @@
import java.io.{ByteArrayOutputStream, PrintStream}
+import scala.reflect.io.File
object Test extends App {
val baos = new ByteArrayOutputStream()
@@ -9,9 +10,11 @@ object Test extends App {
scala.tools.scalap.Main.main(Array("-verbose", "java.lang.Object"))
}
+ val currentLocationCpFragment = File.pathSeparator + "."
+
// now make sure we saw the '.' in the classpath
val msg1 = baos.toString()
- assert(msg1 contains "directory classpath: .", s"Did not see '.' in the default class path. Full results were:\n$msg1")
+ assert(msg1 contains currentLocationCpFragment, s"Did not see '.' in the default class path. Full results were:\n$msg1")
// then test again with a user specified classpath
baos.reset
@@ -22,5 +25,5 @@ object Test extends App {
// now make sure we did not see the '.' in the classpath
val msg2 = baos.toString()
- assert(!(msg2 contains "directory classpath: ."), s"Did saw '.' in the user specified class path. Full results were:\n$msg2")
+ assert(!(msg2 contains currentLocationCpFragment), s"Did saw '.' in the user specified class path. Full results were:\n$msg2")
}
diff --git a/test/files/run/various-flat-classpath-types.check b/test/files/run/various-flat-classpath-types.check
new file mode 100644
index 0000000000..401f707d0e
--- /dev/null
+++ b/test/files/run/various-flat-classpath-types.check
@@ -0,0 +1,12 @@
+ZipBin()
+JarBin()
+DirBin()
+ZipSrc()
+JarSrc()
+DirSrc()
+NestedZipBin()
+NestedJarBin()
+NestedDirBin()
+NestedZipSrc()
+NestedJarSrc()
+NestedDirSrc() \ No newline at end of file
diff --git a/test/files/run/various-flat-classpath-types.scala b/test/files/run/various-flat-classpath-types.scala
new file mode 100644
index 0000000000..d39019e885
--- /dev/null
+++ b/test/files/run/various-flat-classpath-types.scala
@@ -0,0 +1,214 @@
+/*
+ * Copyright (c) 2014 Contributor. All rights reserved.
+ */
+
+import java.io.{File => JFile, FileInputStream, FileOutputStream}
+import java.util.zip.{ZipEntry, ZipOutputStream}
+import scala.reflect.io.{Directory, File}
+import scala.tools.nsc.classpath.FlatClassPath.RootPackage
+import scala.tools.nsc.classpath.PackageNameUtils
+import scala.tools.nsc.io.Jar
+
+/**
+ * Generates directories, jars and zip files containing sources and classes
+ * (the result of a compilation which is executed here)
+ * and use them as a class- and sourcepath during compilation and running
+ * created application. At the end everything is cleaned up.
+ *
+ * It can test also current, recursive classpath. Just right now we force
+ * flat classpath to test it also when the recursive one would be set as a default.
+ */
+object Test {
+
+ private implicit class JFileOps(file: JFile) {
+
+ def createDir(newDirName: String) = {
+ val newDir = new JFile(file, newDirName)
+ newDir.mkdir()
+ newDir
+ }
+
+ def createSrcFile(newFileName: String) = createFile(newFileName + ".scala")
+
+ def createFile(fullFileName: String) = {
+ val newFile = new JFile(file, fullFileName)
+ newFile.createNewFile()
+ newFile
+ }
+
+ def writeAll(text: String): Unit = File(file) writeAll text
+
+ def moveContentToZip(zipName: String): Unit = {
+ val newZip = zipsDir createFile s"$zipName.zip"
+ val outputStream = new ZipOutputStream(new FileOutputStream(newZip))
+
+ def addFileToZip(dirPrefix: String = "")(fileToAdd: JFile): Unit =
+ if (fileToAdd.isDirectory) {
+ val dirEntryName = fileToAdd.getName + "/"
+ outputStream.putNextEntry(new ZipEntry(dirEntryName))
+ fileToAdd.listFiles() foreach addFileToZip(dirEntryName)
+ } else {
+ val inputStream = new FileInputStream(fileToAdd)
+ outputStream.putNextEntry(new ZipEntry(dirPrefix + fileToAdd.getName))
+
+ val buffer = new Array[Byte](1024)
+ var count = inputStream.read(buffer)
+ while (count > 0) {
+ outputStream.write(buffer, 0, count)
+ count = inputStream.read(buffer)
+ }
+
+ inputStream.close()
+ }
+
+ file.listFiles() foreach addFileToZip()
+ outputStream.close()
+
+ cleanDir(file)
+ }
+
+ def moveContentToJar(jarName: String): Unit = {
+ val newJar = jarsDir createFile s"$jarName.jar"
+ Jar.create(file = File(newJar), sourceDir = Directory(file), mainClass = "won't be used")
+ cleanDir(file)
+ }
+
+ def path: String = file.getAbsolutePath
+ }
+
+ private case class DirRep(name: String, nestedDirs: Seq[DirRep] = Nil, sourceFiles: Seq[String] = Nil)
+
+ private val compiler = new scala.tools.nsc.MainClass
+ private val appRunner = new scala.tools.nsc.MainGenericRunner
+ private val classPathImplFlag = "-YclasspathImpl:flat"
+ private val javaClassPath = sys.props("java.class.path")
+
+ // creates a test dir in a temporary dir containing compiled files of this test
+ // root dir will be automatically deleted after the end of test
+ private val rootDir = new JFile(sys.props("partest.output"))
+ private val testDir = rootDir createDir s"cp-tests-${System.currentTimeMillis()}"
+
+ private val jarsDir = testDir createDir "jars"
+ private val zipsDir = testDir createDir "zips"
+ private val srcDir = testDir createDir "src"
+ private val binDir = testDir createDir "bin"
+ private val outDir = testDir createDir "out"
+
+ def main(args: Array[String]): Unit = {
+ createClassesZipInZipsDir()
+ createClassesJarInJarsDir()
+ createClassesInBinDir()
+ createSourcesZipInZipsDir()
+ createSourcesJarInJarsDir()
+ createSourcesInSrcDir()
+ compileFinalApp()
+ runApp()
+ // at the end all created files will be deleted automatically
+ }
+
+ private def createClassesZipInZipsDir(): Unit = {
+ val baseFileName = "ZipBin"
+ createStandardSrcHierarchy(baseFileName)
+ compileSrc(baseFileName)
+ outDir moveContentToZip "Bin"
+ cleanDir(srcDir)
+ }
+
+ private def createClassesJarInJarsDir(): Unit = {
+ val baseFileName = "JarBin"
+ createStandardSrcHierarchy(baseFileName)
+ compileSrc(baseFileName)
+ outDir moveContentToJar "Bin"
+ cleanDir(srcDir)
+ }
+
+ private def createClassesInBinDir(): Unit = {
+ val baseFileName = "DirBin"
+ createStandardSrcHierarchy(baseFileName)
+ compileSrc(baseFileName, destination = binDir)
+ cleanDir(srcDir)
+ }
+
+ private def createSourcesZipInZipsDir(): Unit = {
+ createStandardSrcHierarchy(baseFileName = "ZipSrc")
+ srcDir moveContentToZip "Src"
+ }
+
+ private def createSourcesJarInJarsDir(): Unit = {
+ createStandardSrcHierarchy(baseFileName = "JarSrc")
+ srcDir moveContentToJar "Src"
+ }
+
+ private def createSourcesInSrcDir(): Unit = {
+ createStandardSrcHierarchy(baseFileName = "DirSrc")
+
+ val appFile = srcDir createSrcFile "Main"
+ appFile writeAll s"""import nested._
+ | object Main extends App {
+ | println(new ZipBin)
+ | println(new JarBin)
+ | println(new DirBin)
+ | println(new ZipSrc)
+ | println(new JarSrc)
+ | println(new DirSrc)
+ |
+ | println(new NestedZipBin)
+ | println(new NestedJarBin)
+ | println(new NestedDirBin)
+ | println(new NestedZipSrc)
+ | println(new NestedJarSrc)
+ | println(new NestedDirSrc)
+ | }
+ """.stripMargin
+ }
+
+ private def compileFinalApp(): Unit = {
+ val classPath = mkPath(javaClassPath, binDir.path, zipsDir.path + "/Bin.zip", jarsDir.path + "/Bin.jar")
+ val sourcePath = mkPath(srcDir.path, zipsDir.path + "/Src.zip", jarsDir.path + "/Src.jar")
+
+ compiler.process(Array(classPathImplFlag, "-cp", classPath, "-sourcepath", sourcePath,
+ "-d", outDir.path, s"${srcDir.path}/Main.scala"))
+ }
+
+ private def runApp(): Unit = {
+ val classPath = mkPath(javaClassPath, outDir.path, binDir.path, zipsDir.path + "/Bin.zip", jarsDir.path + "/Bin.jar")
+ appRunner.process(Array(classPathImplFlag, "-cp", classPath, "Main"))
+ }
+
+ private def createStandardSrcHierarchy(baseFileName: String): Unit =
+ createSources(RootPackage, srcDir,
+ DirRep("",
+ nestedDirs = Seq(DirRep("nested", sourceFiles = Seq("Nested" + baseFileName))),
+ sourceFiles = Seq(baseFileName)
+ )
+ )
+
+ private def createSources(pkg: String, dirFile: JFile, dirRep: DirRep): Unit = {
+ dirRep.nestedDirs foreach { rep =>
+ val nestedDir = dirFile createDir rep.name
+ val nestedPkg = PackageNameUtils.packagePrefix(pkg) + rep.name
+ createSources(nestedPkg, nestedDir, rep)
+ }
+
+ val pkgHeader = if (pkg == RootPackage) "" else s"package $pkg\n\n"
+ dirRep.sourceFiles foreach { srcName =>
+ val text = s"""${pkgHeader}case class $srcName(x: String = "")"""
+ val srcFile = dirFile createSrcFile srcName
+ srcFile writeAll text
+ }
+ }
+
+ private def compileSrc(baseFileName: String, destination: JFile = outDir): Unit = {
+ val srcDirPath = srcDir.path
+ compiler.process(Array(classPathImplFlag, "-cp", javaClassPath, "-d", destination.path,
+ s"$srcDirPath/$baseFileName.scala", s"$srcDirPath/nested/Nested$baseFileName.scala"))
+ }
+
+ private def cleanDir(dir: JFile): Unit =
+ dir.listFiles().foreach { file =>
+ if (file.isDirectory) cleanDir(file)
+ file.delete()
+ }
+
+ private def mkPath(pathEntries: String*) = pathEntries.mkString(File.pathSeparator)
+}