added a (currently disabled) test case for compiling the JS code from the kotlin-js-library maven build

This commit is contained in:
James Strachan
2012-07-04 22:03:08 +01:00
parent c2ca06a8cf
commit 98f6650236
4 changed files with 213 additions and 2 deletions
@@ -148,10 +148,13 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
private static Config getConfig(@NotNull K2JSCompilerArguments arguments, @NotNull Project project) {
EcmaVersion ecmaVersion = EcmaVersion.fromString(arguments.target);
String moduleId = FileUtil.getNameWithoutExtension(new File(arguments.outputFile));
if (arguments.libraryFiles == null) {
if (arguments.libraryFiles != null) {
return new LibrarySourcesConfig(project, moduleId, Arrays.asList(arguments.libraryFiles), ecmaVersion);
} if (arguments.libraryDirectories != null) {
return new LibrarySourceDirectoriesConfig(project, moduleId, arguments.libraryDirectories, ecmaVersion);
} else {
// lets discover the JS library definitions on the classpath
return new ClassPathLibraryDefintionsConfig(project, moduleId, ecmaVersion);
}
return new LibrarySourcesConfig(project, moduleId, Arrays.asList(arguments.libraryFiles), ecmaVersion);
}
}
@@ -37,6 +37,9 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "libraryFiles", description = "Path to zipped lib sources or kotlin files")
public String[] libraryFiles;
@Argument(value = "libraryDirectories", description = "Path to directory containing source kotlin files")
public String[] libraryDirectories;
@Argument(value = "sourceFiles", description = "Source files (dir or file)")
public String[] sourceFiles;
@@ -0,0 +1,122 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.test.semantics;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.ExitCode;
import org.jetbrains.jet.cli.js.K2JSCompiler;
import org.jetbrains.jet.cli.js.K2JSCompilerArguments;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.test.SingleFileTranslationTest;
import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
/**
* Lets test compiling all the JS code thats used by the library-js-library from the maven build
*
* This test case isn't run by default but can only be ran after the maven build
* has completed so that there are various kotlin files to be compiled
*/
public class CompileMavenGeneratedJSLibrary extends SingleFileTranslationTest {
private final String generatedJsDir = "libraries/tools/kotlin-js-library/target/";
private String generatedJsDefinitionsDir = generatedJsDir + "generated-js-definitions";
private File generatedJsLibraryDir = new File( generatedJsDir + "generated-js-library");
public CompileMavenGeneratedJSLibrary() {
super("kotlin-js-library/");
}
public void testDisabled() throws Exception {
}
public void TODO_testGenerateTestCase() throws Exception {
if (generatedJsLibraryDir.exists() && generatedJsLibraryDir.isDirectory()) {
generateJavaScriptFiles(EcmaVersion.all(),
"libraries/stdlib/test",
/*
"dom/DomTest.kt",
"js/MapTest.kt",
"js/JsDomTest.kt",
"iterators/FunctionIteratorTest.kt",
"iterators/IteratorsTest.kt",
"GetOrElseTest.kt",
"ListTest.kt",
"SetTest.kt",
*/
"StringTest.kt");
} else {
System.out.println("Warning " + generatedJsLibraryDir + " does not exist - I guess you've not run the maven build in library/ yet?");
}
}
protected void generateJavaScriptFiles(@NotNull EnumSet<EcmaVersion> ecmaVersions,
@NotNull String sourceDir, @NotNull String... stdLibFiles) throws Exception {
List<String> files = Lists.newArrayList();
// now lets add all the files from the definitions and library
//addAllSourceFiles(files, generatedJsDefinitionsDir);
addAllSourceFiles(files, generatedJsLibraryDir);
File stdlibDir = new File(sourceDir);
assertTrue("Cannot find stdlib test source: " + stdlibDir, stdlibDir.exists());
for (String file : stdLibFiles) {
files.add(new File(stdlibDir, file).getPath());
}
// now lets try invoke the compiler
for (EcmaVersion version : ecmaVersions) {
K2JSCompiler compiler = new K2JSCompiler();
K2JSCompilerArguments arguments = new K2JSCompilerArguments();
arguments.outputFile = getOutputFilePath(getTestName(false) + ".compiler.kt", version);
arguments.sourceFiles = files.toArray(new String[files.size()]);
arguments.verbose = true;
arguments.libraryDirectories = new String[] {generatedJsDefinitionsDir};
System.out.println("Compiling with version: " + version + " to: " + arguments.outputFile);
ExitCode answer = compiler.exec(System.out, arguments);
assertEquals("Compile failed", ExitCode.OK, answer);
}
}
private void addAllSourceFiles(List<String> files, File dir) {
File[] children = dir.listFiles();
if (children != null && children.length > 0) {
for (File child : children) {
if (child.isDirectory()) {
addAllSourceFiles(files, child);
} else {
String name = child.getName();
if (name.toLowerCase().endsWith(".kt")) {
files.add(child.getParent());
}
}
}
}
}
}
@@ -0,0 +1,83 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.config;
import com.google.common.collect.Lists;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
public class LibrarySourceDirectoriesConfig extends Config {
@NotNull
protected final String[] directories;
public LibrarySourceDirectoriesConfig(@NotNull Project project, @NotNull String moduleId, @NotNull String[] directories, @NotNull EcmaVersion ecmaVersion) {
super(project, moduleId, ecmaVersion);
this.directories = directories;
}
@NotNull
@Override
public List<JetFile> generateLibFiles() {
try {
List<JetFile> results = Lists.newArrayList();
for (String directory : directories) {
File rootDir = new File(directory);
results.addAll(traverseDirectory(rootDir, rootDir));
}
return results;
}
catch (IOException e) {
System.out.println("Caught: " + e);
e.printStackTrace();
return Collections.emptyList();
}
}
@NotNull
private List<JetFile> traverseDirectory(@NotNull File rootDir, @NotNull File dir) throws IOException {
File[] files = dir.listFiles();
File[] children = dir.listFiles();
List<JetFile> result = Lists.newArrayList();
if (children != null && children.length > 0) {
for (File child : children) {
if (child.isDirectory()) {
List<JetFile> childFiles = traverseDirectory(rootDir, child);
result.addAll(childFiles);
}
else {
String name = child.getName();
if (name.toLowerCase().endsWith(".kt")) {
String text = FileUtil.loadFile(child);
//String path = FileUtil.getRelativePath(directoryFile, child);
String path = child.getPath();
JetFile jfile = JetFileUtils.createPsiFile(path, text, getProject());
result.add(jfile);
}
}
}
}
return result;
}
}