Skip to content

Commit

Permalink
Run the IntegrationTests in J2CL (assert the js version of the compil…
Browse files Browse the repository at this point in the history
…er has the same functionality).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=163526774
  • Loading branch information
tadeegan authored and blickly committed Jul 31, 2017
1 parent f232880 commit 5228853
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 30 deletions.
Expand Up @@ -16,14 +16,17 @@

package com.google.javascript.jscomp;

import static com.google.common.base.Throwables.throwIfUnchecked;

import java.util.concurrent.Callable;

/** GWT compatible replacement for {@code CompilerExecutor} */
final class CompilerExecutor {
<T> T runInCompilerThread(Callable<T> callable, boolean dumpTraceReport) {
try {
return callable.call();
} catch (Exception e) {
} catch (Throwable e) {
throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
Expand Down
35 changes: 7 additions & 28 deletions test/com/google/javascript/jscomp/CompilerTestCase.java
Expand Up @@ -21,6 +21,7 @@
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.javascript.jscomp.testing.JSErrorSubject.assertError;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
Expand All @@ -34,8 +35,6 @@
import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
Expand Down Expand Up @@ -66,7 +65,7 @@ public abstract class CompilerTestCase extends TestCase {
private final boolean emitUseStrict = false;

/** Externs for the test */
private final List<SourceFile> externsInputs;
final List<SourceFile> externsInputs;

/** Whether to compare input and output as trees instead of strings */
private boolean compareAsTree;
Expand Down Expand Up @@ -1412,7 +1411,9 @@ protected void testInternal(

// TODO(rluble): enable multistage compilation when invoking with modules.
if (inputs != null && compiler.getModuleGraph() == null) {
compiler = multistageSerializeAndDeserialize(compiler, inputs, recentChange);
compiler =
CompilerTestCaseUtils.multistageSerializeAndDeserialize(
this, compiler, inputs, recentChange);
root = compiler.getRoot();
externsRoot = compiler.getExternsRoot();
mainRoot = compiler.getJsRoot();
Expand Down Expand Up @@ -1670,30 +1671,6 @@ protected void testInternal(
}
}

private Compiler multistageSerializeAndDeserialize(
Compiler compiler,
List<SourceFile> inputs,
CodeChangeHandler changeHandler) {
ErrorManager errorManager = compiler.getErrorManager();
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
compiler.removeChangeHandler(changeHandler);
compiler.disableThreads();
compiler.saveState(baos);

try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
compiler = createCompiler();
compiler.disableThreads();
compiler.init(externsInputs, inputs, getOptions());
compiler.restoreState(bais);
compiler.setErrorManager(errorManager);
compiler.addChangeHandler(changeHandler);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return compiler;
}

private static void transpileToEs5(AbstractCompiler compiler, Node externsRoot, Node codeRoot) {
List<PassFactory> factories = new ArrayList<>();
TranspilationPasses.addEs6ModulePass(factories);
Expand Down Expand Up @@ -1959,13 +1936,15 @@ Node ensureLibraryInjected(String library, boolean force) {
}

@Override
@GwtIncompatible
public void saveState(OutputStream outputStream) throws IOException {
super.saveState(outputStream);
ObjectOutputStream out = new ObjectOutputStream(outputStream);
out.writeObject(injected);
}

@Override
@GwtIncompatible
public void restoreState(InputStream inputStream) throws IOException, ClassNotFoundException {
super.restoreState(inputStream);
ObjectInputStream in = new ObjectInputStream(inputStream);
Expand Down
50 changes: 50 additions & 0 deletions test/com/google/javascript/jscomp/CompilerTestCaseUtils.java
@@ -0,0 +1,50 @@
/*
* Copyright 2017 The Closure Compiler Authors.
*
* 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 com.google.javascript.jscomp;

import com.google.common.annotations.GwtIncompatible;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;

/** CompilerTestCase utilities that can be super sourced out for GWT/J2CL implementation. */
public class CompilerTestCaseUtils {
@GwtIncompatible
public static Compiler multistageSerializeAndDeserialize(
CompilerTestCase testCase,
Compiler compiler,
List<SourceFile> inputs,
CodeChangeHandler changeHandler) {
ErrorManager errorManager = compiler.getErrorManager();
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
compiler.removeChangeHandler(changeHandler);
compiler.disableThreads();
compiler.saveState(baos);

try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
compiler = testCase.createCompiler();
compiler.disableThreads();
compiler.init(testCase.externsInputs, inputs, testCase.getOptions());
compiler.restoreState(bais);
compiler.setErrorManager(errorManager);
compiler.addChangeHandler(changeHandler);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return compiler;
}
}
31 changes: 30 additions & 1 deletion test/com/google/javascript/jscomp/IntegrationTest.java
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.javascript.jscomp.TypeValidator.TYPE_MISMATCH_WARNING;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -147,6 +148,7 @@ public void testMultipleAliasesInlined_bug31437418() {
"A$z();"));
}

@GwtIncompatible // b/63595345
public void testBug1949424() {
CompilerOptions options = createCompilerOptions();
options.setCollapseProperties(true);
Expand All @@ -155,6 +157,7 @@ public void testBug1949424() {
CLOSURE_COMPILED + "var FOO$bar = 3;");
}

@GwtIncompatible // b/63595345
public void testBug1949424_v2() {
CompilerOptions options = createCompilerOptions();
options.setCollapseProperties(true);
Expand All @@ -170,6 +173,7 @@ public void testBug1949424_v2() {
"var FOO$BAR = 3;"));
}

@GwtIncompatible // b/63595345
public void testUnresolvedDefine() {
CompilerOptions options = new CompilerOptions();
options.setClosurePass(true);
Expand Down Expand Up @@ -422,6 +426,7 @@ public void testIssue90() {
test(options, "var x; x && alert(1);", "");
}

@GwtIncompatible // b/63595345
public void testClosurePassOff() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(false);
Expand All @@ -432,6 +437,7 @@ public void testClosurePassOff() {
"goog.getCssName('foo');");
}

@GwtIncompatible // b/63595345
public void testClosurePassOn() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
Expand Down Expand Up @@ -482,6 +488,7 @@ public void testChromePass_transpile() {
"});"));
}

@GwtIncompatible("CheckMissingGetCssName is incompatible")
public void testCssNameCheck() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
Expand Down Expand Up @@ -509,6 +516,7 @@ public void testBug2592659() {
TypeValidator.TYPE_MISMATCH_WARNING);
}

@GwtIncompatible // b/63595345
public void testTypedefBeforeOwner1() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
Expand All @@ -525,6 +533,7 @@ public void testTypedefBeforeOwner1() {
"foo.Bar = function() {};"));
}

@GwtIncompatible // b/63595345
public void testTypedefBeforeOwner2() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
Expand Down Expand Up @@ -605,6 +614,7 @@ public void testExportTestFunctionsOn1() {
+ "goog.exportSymbol('testFoo', testFoo);");
}

@GwtIncompatible // b/63595345
public void testExportTestFunctionsOn2() {
CompilerOptions options = createCompilerOptions();
options.setExportTestFunctions(true);
Expand Down Expand Up @@ -748,6 +758,7 @@ public void testCheckReferencesOn() {
test(options, "x = 3; var x = 5;", VariableReferenceCheck.EARLY_REFERENCE);
}

@GwtIncompatible // b/63595345
public void testInferTypes() {
CompilerOptions options = createCompilerOptions();
options.inferTypes = true;
Expand Down Expand Up @@ -1159,6 +1170,7 @@ public void testGoogDefine1() {
test(options, code, CLOSURE_COMPILED + " var FLAG = false;");
}

@GwtIncompatible // b/63595345
public void testGoogDefine2() {
String code = CLOSURE_BOILERPLATE +
"goog.provide('ns');" +
Expand Down Expand Up @@ -1403,6 +1415,7 @@ public void testConstantTagsMustAlwaysBeRemoved() {
test(options, originalText, expectedText);
}

@GwtIncompatible // b/63595345
public void testClosurePassPreservesJsDoc() {
CompilerOptions options = createCompilerOptions();
options.setCheckTypes(true);
Expand All @@ -1426,6 +1439,7 @@ public void testClosurePassPreservesJsDoc() {
"var COMPILED=true;var goog={};goog.exportSymbol=function(){};var Foo={a:3}");
}

@GwtIncompatible // b/63595345
public void testProvidedNamespaceIsConst() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
Expand All @@ -1443,6 +1457,7 @@ public void testProvidedNamespaceIsConst() {
ConstCheck.CONST_REASSIGNED_VALUE_ERROR);
}

@GwtIncompatible // b/63595345
public void testProvidedNamespaceIsConst3() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
Expand All @@ -1458,6 +1473,7 @@ public void testProvidedNamespaceIsConst3() {
+ "var foo$bar$baz = function(){};");
}

@GwtIncompatible // b/63595345
public void testProvidedNamespaceIsConst4() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
Expand All @@ -1470,6 +1486,7 @@ public void testProvidedNamespaceIsConst4() {
"var foo = {}; foo = {}; foo.Bar = {};");
}

@GwtIncompatible // b/63595345
public void testProvidedNamespaceIsConst5() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
Expand All @@ -1496,6 +1513,7 @@ public void testProcessDefinesAdditionalReplacements() {
"var HI = false;");
}

@GwtIncompatible("com.google.javascript.jscomp.ReplaceMessages is incompatible")
public void testReplaceMessages() {
CompilerOptions options = createCompilerOptions();
String prefix = "var goog = {}; goog.getMsg = function() {};";
Expand Down Expand Up @@ -3430,6 +3448,7 @@ public void testRenamePrefixNamespaceActivatesMoveFunctionDeclarations() {
test(options, code, "_.f = function() { return 3; }; _.x = _.f;");
}


public void testBrokenNameSpace() {
CompilerOptions options = createCompilerOptions();
String code = "var goog; goog.provide('i.am.on.a.Horse');" +
Expand Down Expand Up @@ -3655,6 +3674,7 @@ public void testUnboundedArrayLiteralInfiniteLoop() {
testParseError(options, "var x = [1, 2", "var x = [1, 2]");
}

@GwtIncompatible // b/63595345
public void testProvideRequireSameFile() throws Exception {
CompilerOptions options = createCompilerOptions();
options.setDependencyOptions(
Expand Down Expand Up @@ -3878,8 +3898,17 @@ private void testES6UnusedClassesAreRemoved(CodingConvention convention) {
assertThat(result).isEqualTo("alert(1)");
}

public void testES6UnusedClassesAreRemoved() {
// Due to JsFileParse not being supported in the JS version, the dependency parsing delegates to
// the {@link CompilerInput$DepsFinder} class which is incompatible with the
// DefaultCodingConvention due to it throwing on methods such as extractIsModuleFile which is
// needed in {@link CompilerInput$DepsFinder#visitSubtree}. Disable this test in the JsVersion.
// TODO(tdeegan): DepsFinder should error out early if run with DefaultCodingConvention.
@GwtIncompatible
public void testES6UnusedClassesAreRemovedDefaultCodingConvention() {
testES6UnusedClassesAreRemoved(CodingConventions.getDefault());
}

public void testES6UnusedClassesAreRemoved() {
testES6UnusedClassesAreRemoved(new ClosureCodingConvention());
testES6UnusedClassesAreRemoved(new GoogleCodingConvention());
}
Expand Down

0 comments on commit 5228853

Please sign in to comment.