fix testlib test: compile own java classes
This commit is contained in:
@@ -5,10 +5,21 @@ import com.google.common.io.Files;
|
|||||||
import com.intellij.openapi.util.Pair;
|
import com.intellij.openapi.util.Pair;
|
||||||
import org.jetbrains.jet.JetTestUtils;
|
import org.jetbrains.jet.JetTestUtils;
|
||||||
import org.jetbrains.jet.cli.KotlinCompiler;
|
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.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
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.Stack;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarOutputStream;
|
import java.util.jar.JarOutputStream;
|
||||||
@@ -35,6 +46,7 @@ class ForTestCompileStdlib {
|
|||||||
try {
|
try {
|
||||||
doCompile();
|
doCompile();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
// TODO: do not try to compile again if failed
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,15 +56,19 @@ class ForTestCompileStdlib {
|
|||||||
private static void doCompile() throws Exception {
|
private static void doCompile() throws Exception {
|
||||||
System.err.println("compiling stdlib for tests, resulting file: " + stdlibJarForTests);
|
System.err.println("compiling stdlib for tests, resulting file: " + stdlibJarForTests);
|
||||||
|
|
||||||
File tmp = new File(stdlibJarForTests.getPath() + "~");
|
File tmpFile = new File(stdlibJarForTests.getPath() + "~");
|
||||||
JetTestUtils.mkdirs(tmp.getParentFile());
|
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 {
|
try {
|
||||||
JarOutputStream jarOutputStream = new JarOutputStream(stdlibJar);
|
JarOutputStream jarOutputStream = new JarOutputStream(stdlibJar);
|
||||||
|
|
||||||
copyJavaPartOfStdlib(jarOutputStream);
|
copyToJar(tmpDir, jarOutputStream);
|
||||||
compileKotlinPartOfStdlibToJar(jarOutputStream);
|
|
||||||
|
|
||||||
jarOutputStream.close();
|
jarOutputStream.close();
|
||||||
stdlibJar.close();
|
stdlibJar.close();
|
||||||
@@ -65,31 +81,17 @@ class ForTestCompileStdlib {
|
|||||||
} catch (Throwable e) { }
|
} catch (Throwable e) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tmp.renameTo(stdlibJarForTests)) {
|
if (!tmpFile.renameTo(stdlibJarForTests)) {
|
||||||
throw new RuntimeException();
|
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 {
|
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>>();
|
Stack<Pair<String, File>> toCopy = new Stack<Pair<String, File>>();
|
||||||
toCopy.add(new Pair<String, File>("", root));
|
toCopy.add(new Pair<String, File>("", root));
|
||||||
while (!toCopy.empty()) {
|
while (!toCopy.empty()) {
|
||||||
Pair<String, File> pop = toCopy.pop();
|
Pair<String, File> pop = toCopy.pop();
|
||||||
File file = pop.getSecond();
|
File file = pop.getSecond();
|
||||||
if (file.isFile()) {
|
if (file.isFile()) {
|
||||||
System.err.println("cp file " + file + " to jar path " + pop.getFirst());
|
|
||||||
|
|
||||||
os.putNextEntry(new JarEntry(pop.getFirst()));
|
os.putNextEntry(new JarEntry(pop.getFirst()));
|
||||||
Files.copy(file, os);
|
Files.copy(file, os);
|
||||||
} else if (file.isDirectory()) {
|
} else if (file.isDirectory()) {
|
||||||
@@ -103,12 +105,41 @@ class ForTestCompileStdlib {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void compileKotlinPartOfStdlibToJar(JarOutputStream jarOutputStream) throws IOException {
|
private static void compileKotlinPartOfStdlib(File destdir) throws IOException {
|
||||||
File file = new File(JetTestUtils.tmpDirForTest(ForTestCompileStdlib.class), "stdlib-kt");
|
|
||||||
JetTestUtils.recreateDirectory(file);
|
|
||||||
// lame
|
// lame
|
||||||
KotlinCompiler.main("-excludeStdlib", "-output", file.getPath(), "-src", "./stdlib/ktSrc");
|
KotlinCompiler.main("-excludeStdlib", "-output", destdir.getPath(), "-src", "./stdlib/ktSrc");
|
||||||
copyToJar(file, jarOutputStream);
|
}
|
||||||
|
|
||||||
|
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 {
|
} else {
|
||||||
CompileEnvironment.initializeKotlinRuntime(myEnvironment);
|
CompileEnvironment.initializeKotlinRuntime(myEnvironment);
|
||||||
}
|
}
|
||||||
|
|
||||||
URLClassLoader classLoader = (URLClassLoader) TestCase.class.getClassLoader();
|
File junitJar = new File("testlib/lib/junit-4.9.jar");
|
||||||
CoreLocalFileSystem localFileSystem = myEnvironment.getLocalFileSystem();
|
|
||||||
for(URL url: classLoader.getURLs()) {
|
if (!junitJar.exists()) {
|
||||||
if(url.getProtocol().equals("file") && url.getPath().contains("junit")) {
|
throw new AssertionError();
|
||||||
File file = new File(URLDecoder.decode(url.getPath()));
|
|
||||||
if(file.exists()) {
|
|
||||||
myEnvironment.addToClasspath(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
myEnvironment.addToClasspath(junitJar);
|
||||||
|
|
||||||
|
CoreLocalFileSystem localFileSystem = myEnvironment.getLocalFileSystem();
|
||||||
VirtualFile path = localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../testlib/test");
|
VirtualFile path = localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../testlib/test");
|
||||||
session.addSources(path);
|
session.addSources(path);
|
||||||
|
|
||||||
@@ -87,13 +86,15 @@ public abstract class TestlibTestBase extends CodegenTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ClassFileFactory classFileFactory = session.generate();
|
ClassFileFactory classFileFactory = session.generate();
|
||||||
|
|
||||||
|
URLClassLoader dependenciesClassLoader = new URLClassLoader(new URL[] { junitJar.toURI().toURL() });
|
||||||
GeneratedClassLoader loader;
|
GeneratedClassLoader loader;
|
||||||
if (binary) {
|
if (binary) {
|
||||||
URLClassLoader parentClassLoader = new URLClassLoader(new URL[]{
|
URLClassLoader parentClassLoader = new URLClassLoader(new URL[]{
|
||||||
ForTestCompileStdlib.stdlibJarForTests().toURI().toURL() });
|
ForTestCompileStdlib.stdlibJarForTests().toURI().toURL() }, dependenciesClassLoader);
|
||||||
loader = new GeneratedClassLoader(classFileFactory, parentClassLoader);
|
loader = new GeneratedClassLoader(classFileFactory, parentClassLoader);
|
||||||
} else {
|
} else {
|
||||||
loader = new GeneratedClassLoader(classFileFactory);
|
loader = new GeneratedClassLoader(classFileFactory, dependenciesClassLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
JetTypeMapper typeMapper = new JetTypeMapper(classFileFactory.state.getStandardLibrary(), session.getMyBindingContext());
|
JetTypeMapper typeMapper = new JetTypeMapper(classFileFactory.state.getStandardLibrary(), session.getMyBindingContext());
|
||||||
|
|||||||
Reference in New Issue
Block a user