add tests for jps-plugin (Kotlin Javascript projects)

This commit is contained in:
Michael Nedzelsky
2014-12-08 19:03:55 +03:00
parent f8f7ea8998
commit bd090d0e5f
32 changed files with 422 additions and 8 deletions
@@ -81,14 +81,33 @@ public abstract class AbstractKotlinJpsBuildTestCase extends JpsBuildTestCase {
return addKotlinRuntimeDependency(myProject);
}
protected JpsLibrary addKotlinJavaScriptStdlibDependency() {
return addKotlinJavaScriptStdlibDependency(myProject);
}
protected JpsLibrary addKotlinJavaScriptDependency(String libraryName, File libraryFile) {
return addDependency(JpsJavaDependencyScope.COMPILE, myProject.getModules(), false, libraryName, libraryFile);
}
static JpsLibrary addKotlinRuntimeDependency(@NotNull JpsProject project) {
return addKotlinRuntimeDependency(JpsJavaDependencyScope.COMPILE, project.getModules(), false);
}
static JpsLibrary addKotlinJavaScriptStdlibDependency(@NotNull JpsProject project) {
return addKotlinJavaScriptStdlibDependency(JpsJavaDependencyScope.COMPILE, project.getModules(), false);
}
protected static JpsLibrary addKotlinRuntimeDependency(JpsJavaDependencyScope type, Collection<JpsModule> modules, boolean exported) {
JpsLibrary library = modules.iterator().next().getProject().addLibrary("kotlin-runtime", JpsJavaLibraryType.INSTANCE);
File runtime = PathUtil.getKotlinPathsForDistDirectory().getRuntimePath();
library.addRoot(runtime, JpsOrderRootType.COMPILED);
return addDependency(type, modules, exported, "kotlin-runtime", PathUtil.getKotlinPathsForDistDirectory().getRuntimePath());
}
protected static JpsLibrary addKotlinJavaScriptStdlibDependency(JpsJavaDependencyScope type, Collection<JpsModule> modules, boolean exported) {
return addDependency(type, modules, exported, "KotlinJavaScript", PathUtil.getKotlinPathsForDistDirectory().getJsStdLibJarPath());
}
protected static JpsLibrary addDependency(JpsJavaDependencyScope type, Collection<JpsModule> modules, boolean exported, String libraryName, File file) {
JpsLibrary library = modules.iterator().next().getProject().addLibrary(libraryName, JpsJavaLibraryType.INSTANCE);
library.addRoot(file, JpsOrderRootType.COMPILED);
for (JpsModule module : modules) {
JpsModuleRootModificationUtil.addDependency(module, library, type, exported);
}
@@ -20,13 +20,14 @@ import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.io.ZipUtil;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.codegen.AsmUtil;
import org.jetbrains.jet.compiler.CompilerSettings;
import org.jetbrains.jet.jps.JpsKotlinCompilerSettings;
import org.jetbrains.jet.lang.resolve.kotlin.PackagePartClassUtils;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.utils.PathUtil;
import org.jetbrains.jps.builders.BuildResult;
import org.jetbrains.jps.model.java.JpsJavaDependencyScope;
import org.jetbrains.jps.model.java.JpsJavaExtensionService;
@@ -38,9 +39,12 @@ import org.jetbrains.org.objectweb.asm.MethodVisitor;
import org.jetbrains.org.objectweb.asm.Opcodes;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
import java.util.regex.Pattern;
import java.util.zip.ZipOutputStream;
public class KotlinJpsBuildTest extends AbstractKotlinJpsBuildTestCase {
private static final String PROJECT_NAME = "kotlinProject";
@@ -48,7 +52,31 @@ public class KotlinJpsBuildTest extends AbstractKotlinJpsBuildTestCase {
private static final String[] EXCLUDE_FILES = { "Excluded.class", "YetAnotherExcluded.class" };
private static final String[] NOTHING = {};
private static final String KOTLIN_JS_LIBRARY = "jslib-example";
private static final String PATH_TO_KOTLIN_JS_LIBRARY = TEST_DATA_PATH + "general/KotlinJavaScriptProjectWithDirectoryAsLibrary/" + KOTLIN_JS_LIBRARY;
private static final String KOTLIN_JS_LIBRARY_JAR = KOTLIN_JS_LIBRARY + ".jar";
private static final Set<String> EXPECTED_JS_FILES_IN_OUTPUT_FOR_STDLIB_ONLY = KotlinPackage.hashSetOf(PROJECT_NAME + ".js", "lib/kotlin.js");
private static final Set<String> EXPECTED_JS_FILES_IN_OUTPUT_NO_COPY = KotlinPackage.hashSetOf(PROJECT_NAME + ".js");
private static final Set<String> EXPECTED_JS_FILES_IN_OUTPUT_WITH_ADDITIONAL_LIB_AND_DEFAULT_DIR =
KotlinPackage.hashSetOf(
PROJECT_NAME + ".js",
"lib/kotlin.js",
"lib/jslib-example.js",
"lib/file0.js",
"lib/dir/file1.js",
"lib/META-INF-ex/file2.js",
"lib/res0.js",
"lib/resdir/res1.js");
private static final Set<String> EXPECTED_JS_FILES_IN_OUTPUT_WITH_ADDITIONAL_LIB_AND_CUSTOM_DIR =
KotlinPackage.hashSetOf(
PROJECT_NAME + ".js",
"custom/kotlin.js",
"custom/jslib-example.js",
"custom/file0.js",
"custom/dir/file1.js",
"custom/META-INF-ex/file2.js",
"custom/res0.js",
"custom/resdir/res1.js");
@Override
public void setUp() throws Exception {
super.setUp();
@@ -84,12 +112,77 @@ public class KotlinJpsBuildTest extends AbstractKotlinJpsBuildTestCase {
makeAll().assertSuccessful();
}
public void doTestWithKotlinJavaScriptLibrary() {
initProject();
addKotlinJavaScriptStdlibDependency();
createKotlinJavaScriptLibraryArchive();
addKotlinJavaScriptDependency(KOTLIN_JS_LIBRARY, new File(workDir, KOTLIN_JS_LIBRARY_JAR));
makeAll().assertSuccessful();
}
public void testKotlinProject() {
doTest();
checkWhen(touch("src/test1.kt"), null, packageClasses("kotlinProject", "src/test1.kt", "_DefaultPackage"));
}
public void testKotlinJavaScriptProject() {
initProject();
addKotlinJavaScriptStdlibDependency();
makeAll().assertSuccessful();
assertEquals(EXPECTED_JS_FILES_IN_OUTPUT_FOR_STDLIB_ONLY, contentOfOutputDir(PROJECT_NAME));
checkWhen(touch("src/test1.kt"), null, k2jsOutput(PROJECT_NAME));
}
public void testKotlinJavaScriptProjectWithDirectoryAsStdlib() {
initProject();
File jslibJar = PathUtil.getKotlinPathsForDistDirectory().getJsStdLibJarPath();
File jslibDir = new File(workDir, "KotlinJavaScript");
try {
ZipUtil.extract(jslibJar, jslibDir, null);
}
catch (IOException ex) {
throw new IllegalStateException(ex.getMessage());
}
addKotlinJavaScriptDependency("KotlinJavaScript", jslibDir);
makeAll().assertSuccessful();
assertEquals(EXPECTED_JS_FILES_IN_OUTPUT_FOR_STDLIB_ONLY, contentOfOutputDir(PROJECT_NAME));
checkWhen(touch("src/test1.kt"), null, k2jsOutput(PROJECT_NAME));
}
public void testKotlinJavaScriptProjectWithDirectoryAsLibrary() {
initProject();
addKotlinJavaScriptStdlibDependency();
addKotlinJavaScriptDependency(KOTLIN_JS_LIBRARY, new File(workDir, KOTLIN_JS_LIBRARY));
makeAll().assertSuccessful();
assertEquals(EXPECTED_JS_FILES_IN_OUTPUT_WITH_ADDITIONAL_LIB_AND_DEFAULT_DIR, contentOfOutputDir(PROJECT_NAME));
checkWhen(touch("src/test1.kt"), null, k2jsOutput(PROJECT_NAME));
}
public void testKotlinJavaScriptProjectWithLibrary() {
doTestWithKotlinJavaScriptLibrary();
assertEquals(EXPECTED_JS_FILES_IN_OUTPUT_WITH_ADDITIONAL_LIB_AND_DEFAULT_DIR, contentOfOutputDir(PROJECT_NAME));
checkWhen(touch("src/test1.kt"), null, k2jsOutput(PROJECT_NAME));
}
public void testKotlinJavaScriptProjectWithLibraryCustomOutputDir() {
doTestWithKotlinJavaScriptLibrary();
assertEquals(EXPECTED_JS_FILES_IN_OUTPUT_WITH_ADDITIONAL_LIB_AND_CUSTOM_DIR, contentOfOutputDir(PROJECT_NAME));
checkWhen(touch("src/test1.kt"), null, k2jsOutput(PROJECT_NAME));
}
public void testKotlinJavaScriptProjectWithLibraryNoCopy() {
doTestWithKotlinJavaScriptLibrary();
assertEquals(EXPECTED_JS_FILES_IN_OUTPUT_NO_COPY, contentOfOutputDir(PROJECT_NAME));
checkWhen(touch("src/test1.kt"), null, k2jsOutput(PROJECT_NAME));
}
public void testExcludeFolderInSourceRoot() {
doTest();
@@ -276,6 +369,43 @@ public class KotlinJpsBuildTest extends AbstractKotlinJpsBuildTestCase {
checkWhen(touch("module2/src/b.kt"), null, packageClasses("module2", "module2/src/b.kt", "test.TestPackage"));
}
private void createKotlinJavaScriptLibraryArchive() {
File jarFile = new File(workDir, KOTLIN_JS_LIBRARY_JAR);
try {
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(jarFile));
ZipUtil.addDirToZipRecursively(zip, jarFile, new File(PATH_TO_KOTLIN_JS_LIBRARY), "", null, null);
zip.close();
}
catch (FileNotFoundException ex) {
throw new IllegalStateException(ex.getMessage());
}
catch (IOException ex) {
throw new IllegalStateException(ex.getMessage());
}
}
@NotNull
private static String[] k2jsOutput(String moduleName) {
String outputDir = "out/production/" + moduleName;
String[] result = new String[1];
result[0] = outputDir + "/" + moduleName + ".js";
return result;
}
@NotNull
private Set<String> contentOfOutputDir(String moduleName) {
String outputDir = "out/production/" + moduleName;
File baseDir = new File(workDir, outputDir);
List<File> files = FileUtil.findFilesByMask(Pattern.compile(".*"), baseDir);
Set<String> result = new HashSet<String>();
for(File file : files) {
String relativePath = FileUtil.getRelativePath(baseDir, file);
assert relativePath != null : "relativePath should not be null";
result.add(FileUtil.toSystemIndependentName(relativePath));
}
return result;
}
@NotNull
private static Set<String> getMethodsOfClass(@NotNull File classFile) throws IOException {
final Set<String> result = new TreeSet<String>();
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="IDEA_JDK" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="kotlinProject" />
</component>
</module>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
</component>
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/kotlinProject.iml" filepath="$PROJECT_DIR$/kotlinProject.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA_JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,3 @@
fun foo() {
}
@@ -0,0 +1,8 @@
package library.sample
public fun pairAdd(p: Pair<Int, Int>): Int = p.first + p.second
public fun pairMul(p: Pair<Int, Int>): Int = p.first * p.second
public data class IntHolder(val value: Int)
@@ -0,0 +1,9 @@
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.1
Created-By: 1.7.0_72-b14 (Oracle Corporation)
Built-By: JetBrains
Implementation-Vendor: JetBrains
Implementation-Version: snapshot
Specification-Title: Kotlin JavaScript Lib
Kotlin-JS-Module-Name: jslib-example
@@ -0,0 +1,6 @@
# jslib-example
Path to sources: compiler/integration-tests/testData/ant/js/simpleWithStdlibAndFolderAsAnotherLib/jslib-example
The archive compiler/integration-tests/testData/ant/js/simpleWithStdlibAndAnotherLib/jslib-example.jar should be updated after
changing some files in source folder.
@@ -0,0 +1,37 @@
(function (Kotlin) {
'use strict';
var _ = Kotlin.defineRootPackage(null, /** @lends _ */ {
library: Kotlin.definePackage(null, /** @lends _.library */ {
sample: Kotlin.definePackage(null, /** @lends _.library.sample */ {
pairAdd_bunuun$: function (p) {
return p.first + p.second;
},
pairMul_bunuun$: function (p) {
return p.first * p.second;
},
IntHolder: Kotlin.createClass(null, function (value) {
this.value = value;
}, /** @lends _.library.sample.IntHolder.prototype */ {
component1: function () {
return this.value;
},
copy_za3lpa$: function (value) {
return new _.library.sample.IntHolder(value === void 0 ? this.value : value);
},
toString: function () {
return 'IntHolder(value=' + Kotlin.toString(this.value) + ')';
},
hashCode: function () {
var result = 0;
result = result * 31 + Kotlin.hashCode(this.value) | 0;
return result;
},
equals_za3rmp$: function (other) {
return this === other || (other !== null && (Object.getPrototypeOf(this) === Object.getPrototypeOf(other) && Kotlin.equals(this.value, other.value)));
}
})
})
})
});
Kotlin.defineModule('jslib-example', _);
}(Kotlin));
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="IDEA_JDK" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="kotlinProject" />
</component>
</module>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
</component>
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/kotlinProject.iml" filepath="$PROJECT_DIR$/kotlinProject.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA_JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,7 @@
import library.sample.pairAdd
fun foo() {
val p = Pair(10, 20)
val x = pairAdd(p)
println("x=$x")
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="IDEA_JDK" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="kotlinProject" />
</component>
</module>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
</component>
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/kotlinProject.iml" filepath="$PROJECT_DIR$/kotlinProject.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA_JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,3 @@
fun foo() {
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="IDEA_JDK" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="kotlinProject" />
</component>
</module>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
</component>
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/kotlinProject.iml" filepath="$PROJECT_DIR$/kotlinProject.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA_JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,7 @@
import library.sample.pairAdd
fun foo() {
val p = Pair(10, 20)
val x = pairAdd(p)
println("x=$x")
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="IDEA_JDK" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="kotlinProject" />
</component>
</module>
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
</component>
<component name="KotlinCompilerSettings">
<option name="copyJsLibraryFiles" value="true" />
<option name="outputDirectoryForJsLibraryFiles" value="custom" />
</component>
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/kotlinProject.iml" filepath="$PROJECT_DIR$/kotlinProject.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA_JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,7 @@
import library.sample.pairAdd
fun foo() {
val p = Pair(10, 20)
val x = pairAdd(p)
println("x=$x")
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="IDEA_JDK" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="kotlinProject" />
</component>
</module>
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
</component>
<component name="KotlinCompilerSettings">
<option name="copyJsLibraryFiles" value="false" />
</component>
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/kotlinProject.iml" filepath="$PROJECT_DIR$/kotlinProject.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA_JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,7 @@
import library.sample.pairAdd
fun foo() {
val p = Pair(10, 20)
val x = pairAdd(p)
println("x=$x")
}