fix testlib test: compile own java classes

This commit is contained in:
Stepan Koltsov
2012-01-23 22:49:40 +04:00
parent 4b7f3f718e
commit 8dd62f124a
2 changed files with 69 additions and 37 deletions
@@ -5,10 +5,21 @@ import com.google.common.io.Files;
import com.intellij.openapi.util.Pair;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.KotlinCompiler;
import org.junit.Assert;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Stack;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
@@ -35,6 +46,7 @@ class ForTestCompileStdlib {
try {
doCompile();
} catch (Exception e) {
// TODO: do not try to compile again if failed
throw new RuntimeException(e);
}
@@ -44,15 +56,19 @@ class ForTestCompileStdlib {
private static void doCompile() throws Exception {
System.err.println("compiling stdlib for tests, resulting file: " + stdlibJarForTests);
File tmp = new File(stdlibJarForTests.getPath() + "~");
JetTestUtils.mkdirs(tmp.getParentFile());
File tmpFile = new File(stdlibJarForTests.getPath() + "~");
JetTestUtils.mkdirs(tmpFile.getParentFile());
File tmpDir = new File(JetTestUtils.tmpDirForTest(ForTestCompileStdlib.class), "classes");
JetTestUtils.recreateDirectory(tmpDir);
compileKotlinPartOfStdlib(tmpDir);
compileJavaPartOfStdlib(tmpDir);
FileOutputStream stdlibJar = new FileOutputStream(tmp);
FileOutputStream stdlibJar = new FileOutputStream(tmpFile);
try {
JarOutputStream jarOutputStream = new JarOutputStream(stdlibJar);
copyJavaPartOfStdlib(jarOutputStream);
compileKotlinPartOfStdlibToJar(jarOutputStream);
copyToJar(tmpDir, jarOutputStream);
jarOutputStream.close();
stdlibJar.close();
@@ -65,31 +81,17 @@ class ForTestCompileStdlib {
} catch (Throwable e) { }
}
if (!tmp.renameTo(stdlibJarForTests)) {
if (!tmpFile.renameTo(stdlibJarForTests)) {
throw new RuntimeException();
}
}
private static void copyJavaPartOfStdlib(JarOutputStream os) throws IOException {
File root = new File("out/production/stdlib");
if (!new File(root, "jet/JetObject.class").isFile()) {
throw new RuntimeException();
}
copyToJar(root, os);
}
private static void copyToJar(File root, JarOutputStream os) throws IOException {
System.err.println("cp directory " + root + " to jar");
Stack<Pair<String, File>> toCopy = new Stack<Pair<String, File>>();
toCopy.add(new Pair<String, File>("", root));
while (!toCopy.empty()) {
Pair<String, File> pop = toCopy.pop();
File file = pop.getSecond();
if (file.isFile()) {
System.err.println("cp file " + file + " to jar path " + pop.getFirst());
os.putNextEntry(new JarEntry(pop.getFirst()));
Files.copy(file, os);
} else if (file.isDirectory()) {
@@ -103,12 +105,41 @@ class ForTestCompileStdlib {
}
}
private static void compileKotlinPartOfStdlibToJar(JarOutputStream jarOutputStream) throws IOException {
File file = new File(JetTestUtils.tmpDirForTest(ForTestCompileStdlib.class), "stdlib-kt");
JetTestUtils.recreateDirectory(file);
private static void compileKotlinPartOfStdlib(File destdir) throws IOException {
// lame
KotlinCompiler.main("-excludeStdlib", "-output", file.getPath(), "-src", "./stdlib/ktSrc");
copyToJar(file, jarOutputStream);
KotlinCompiler.main("-excludeStdlib", "-output", destdir.getPath(), "-src", "./stdlib/ktSrc");
}
private static List<File> javaFilesInDir(File dir) {
List<File> r = new ArrayList<File>();
Stack<File> stack = new Stack<File>();
stack.push(dir);
while (!stack.empty()) {
File file = stack.pop();
if (file.isDirectory()) {
stack.addAll(Arrays.asList(file.listFiles()));
} else if (file.getName().endsWith(".java")) {
r.add(file);
}
}
return r;
}
private static void compileJavaPartOfStdlib(File destdir) throws IOException {
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, Locale.ENGLISH, Charset.forName("utf-8"));
try {
Iterable<? extends JavaFileObject> javaFileObjectsFromFiles = fileManager.getJavaFileObjectsFromFiles(javaFilesInDir(new File("stdlib/src")));
List<String> options = Arrays.asList(
"-d", destdir.getPath()
);
JavaCompiler.CompilationTask task = javaCompiler.getTask(null, fileManager, null, options, null, javaFileObjectsFromFiles);
Assert.assertTrue(task.call());
} finally {
fileManager.close();
}
}
@@ -64,17 +64,16 @@ public abstract class TestlibTestBase extends CodegenTestCase {
} else {
CompileEnvironment.initializeKotlinRuntime(myEnvironment);
}
URLClassLoader classLoader = (URLClassLoader) TestCase.class.getClassLoader();
CoreLocalFileSystem localFileSystem = myEnvironment.getLocalFileSystem();
for(URL url: classLoader.getURLs()) {
if(url.getProtocol().equals("file") && url.getPath().contains("junit")) {
File file = new File(URLDecoder.decode(url.getPath()));
if(file.exists()) {
myEnvironment.addToClasspath(file);
}
}
File junitJar = new File("testlib/lib/junit-4.9.jar");
if (!junitJar.exists()) {
throw new AssertionError();
}
myEnvironment.addToClasspath(junitJar);
CoreLocalFileSystem localFileSystem = myEnvironment.getLocalFileSystem();
VirtualFile path = localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../testlib/test");
session.addSources(path);
@@ -87,13 +86,15 @@ public abstract class TestlibTestBase extends CodegenTestCase {
}
ClassFileFactory classFileFactory = session.generate();
URLClassLoader dependenciesClassLoader = new URLClassLoader(new URL[] { junitJar.toURI().toURL() });
GeneratedClassLoader loader;
if (binary) {
URLClassLoader parentClassLoader = new URLClassLoader(new URL[]{
ForTestCompileStdlib.stdlibJarForTests().toURI().toURL() });
ForTestCompileStdlib.stdlibJarForTests().toURI().toURL() }, dependenciesClassLoader);
loader = new GeneratedClassLoader(classFileFactory, parentClassLoader);
} else {
loader = new GeneratedClassLoader(classFileFactory);
loader = new GeneratedClassLoader(classFileFactory, dependenciesClassLoader);
}
JetTypeMapper typeMapper = new JetTypeMapper(classFileFactory.state.getStandardLibrary(), session.getMyBindingContext());