diff --git a/compiler/tests/org/jetbrains/jet/cli/AbstractKotlincExecutableTest.java b/compiler/tests/org/jetbrains/jet/cli/AbstractKotlincExecutableTest.java new file mode 100644 index 00000000000..4d96f0430f3 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/cli/AbstractKotlincExecutableTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2010-2013 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.jet.cli; + +import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.util.ArrayUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.cli.common.ExitCode; +import org.jetbrains.jet.test.TestCaseWithTmpdir; +import org.jetbrains.jet.utils.PathUtil; + +import java.io.File; +import java.io.IOException; + +public abstract class AbstractKotlincExecutableTest extends TestCaseWithTmpdir { + private void doTest(@NotNull String argsFilePath, @NotNull String executableName, @NotNull String testDataDir) throws Exception { + String executableFileName = SystemInfo.isWindows ? executableName + ".bat" : executableName; + File kotlincFile = new File(PathUtil.getKotlinPathsForDistDirectory().getHomePath(), "bin/" + executableFileName); + assertTrue("kotlinc executable not found, probably you need to invoke 'dist' Ant target: " + kotlincFile.getAbsolutePath(), kotlincFile.exists()); + + String[] args = CliBaseTest.readArgs(argsFilePath, testDataDir, tmpdir.getAbsolutePath()); + + final Process process = Runtime.getRuntime().exec(ArrayUtil.prepend(kotlincFile.getAbsolutePath(), args)); + + // We don't need contents of stderr, just read it to null to avoid process blocking + new Thread(new Runnable() { + @Override + public void run() { + try { + FileUtil.loadBytes(process.getErrorStream()); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + }).start(); + + String output = FileUtil.loadTextAndClose(process.getInputStream()); + + int intExitCode = process.waitFor(); + ExitCode exitCode = ExitCode.values()[intExitCode]; + + String normalizedOutput = CliBaseTest.getNormalizedCompilerOutput(output, exitCode, testDataDir); + File outFile = new File(argsFilePath.replace(".args", ".out")); + JetTestUtils.assertEqualsToFile(outFile, normalizedOutput); + } + + protected void doJvmTest(@NotNull String argsFilePath) throws Exception { + doTest(argsFilePath, "kotlinc-jvm", CliBaseTest.JVM_TEST_DATA); + } + + protected void doJsTest(@NotNull String argsFilePath) throws Exception { + doTest(argsFilePath, "kotlinc-js", CliBaseTest.JS_TEST_DATA); + } +} diff --git a/compiler/tests/org/jetbrains/jet/cli/CliBaseTest.java b/compiler/tests/org/jetbrains/jet/cli/CliBaseTest.java index 6ca3cf393f3..0e15a672cb4 100644 --- a/compiler/tests/org/jetbrains/jet/cli/CliBaseTest.java +++ b/compiler/tests/org/jetbrains/jet/cli/CliBaseTest.java @@ -16,6 +16,7 @@ package org.jetbrains.jet.cli; +import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; import com.intellij.util.ArrayUtil; import com.intellij.util.Function; @@ -38,8 +39,8 @@ import java.io.PrintStream; import java.util.List; public class CliBaseTest { - private static final String JS_TEST_DATA = "compiler/testData/cli/js"; - private static final String JVM_TEST_DATA = "compiler/testData/cli/jvm"; + static final String JS_TEST_DATA = "compiler/testData/cli/js"; + static final String JVM_TEST_DATA = "compiler/testData/cli/jvm"; @Rule public final Tmpdir tmpdir = new Tmpdir(); @@ -47,13 +48,13 @@ public class CliBaseTest { public final TestName testName = new TestName(); @NotNull - private static String executeCompilerGrabOutput(@NotNull CLICompiler compiler, @NotNull String[] args) { + private static Pair executeCompilerGrabOutput(@NotNull CLICompiler compiler, @NotNull String[] args) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); PrintStream origOut = System.out; try { System.setOut(new PrintStream(bytes)); ExitCode exitCode = CLICompiler.doMainNoExit(compiler, args); - return bytes.toString("utf-8") + exitCode + "\n"; + return Pair.create(bytes.toString("utf-8"), exitCode); } catch (Exception e) { throw ExceptionUtils.rethrow(e); @@ -63,16 +64,30 @@ public class CliBaseTest { } } - private void executeCompilerCompareOutput(@NotNull CLICompiler compiler, @NotNull String testDataDir) throws Exception { - String actual = executeCompilerGrabOutput(compiler, readArgs(testDataDir)) + @NotNull + static String getNormalizedCompilerOutput(@NotNull String pureOutput, @NotNull ExitCode exitCode, @NotNull String testDataDir) { + String normalizedOutputWithoutExitCode = pureOutput .replace(new File(testDataDir).getAbsolutePath(), "$TESTDATA_DIR$") .replace("\\", "/"); + return normalizedOutputWithoutExitCode + exitCode; + } + + private void executeCompilerCompareOutput(@NotNull CLICompiler compiler, @NotNull String testDataDir) throws Exception { + Pair outputAndExitCode = + executeCompilerGrabOutput(compiler, readArgs(testDataDir + "/" + testName.getMethodName() + ".args", testDataDir, + tmpdir.getTmpDir().getPath())); + String actual = getNormalizedCompilerOutput(outputAndExitCode.first, outputAndExitCode.second, testDataDir); JetTestUtils.assertEqualsToFile(new File(testDataDir + "/" + testName.getMethodName() + ".out"), actual); } - private String[] readArgs(@NotNull final String testDataDir) throws IOException { - List lines = FileUtil.loadLines(testDataDir + "/" + testName.getMethodName() + ".args"); + @NotNull + static String[] readArgs( + @NotNull String argsFilePath, + @NotNull final String testDataDir, + @NotNull final String tempDir + ) throws IOException { + List lines = FileUtil.loadLines(argsFilePath); return ArrayUtil.toStringArray(ContainerUtil.mapNotNull(lines, new Function() { @Override @@ -82,7 +97,7 @@ public class CliBaseTest { } return arg .replace(":", File.pathSeparator) - .replace("$TEMP_DIR$", tmpdir.getTmpDir().getPath()) + .replace("$TEMP_DIR$", tempDir) .replace("$TESTDATA_DIR$", testDataDir); } })); diff --git a/compiler/tests/org/jetbrains/jet/cli/KotlincExecutableTestGenerated.java b/compiler/tests/org/jetbrains/jet/cli/KotlincExecutableTestGenerated.java new file mode 100644 index 00000000000..faebb4a68db --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/cli/KotlincExecutableTestGenerated.java @@ -0,0 +1,168 @@ +/* + * Copyright 2010-2013 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.jet.cli; + +import junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestSuite; + +import java.io.File; +import java.util.regex.Pattern; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.test.InnerTestClasses; +import org.jetbrains.jet.test.TestMetadata; + +import org.jetbrains.jet.cli.AbstractKotlincExecutableTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@InnerTestClasses({KotlincExecutableTestGenerated.Jvm.class, KotlincExecutableTestGenerated.Js.class}) +public class KotlincExecutableTestGenerated extends AbstractKotlincExecutableTest { + @TestMetadata("compiler/testData/cli/jvm") + @InnerTestClasses({Jvm.WrongAbiVersionLib.class}) + public static class Jvm extends AbstractKotlincExecutableTest { + public void testAllFilesPresentInJvm() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/cli/jvm"), Pattern.compile("^(.+)\\.args$"), true); + } + + @TestMetadata("diagnosticsOrder.args") + public void testDiagnosticsOrder() throws Exception { + doJvmTest("compiler/testData/cli/jvm/diagnosticsOrder.args"); + } + + @TestMetadata("help.args") + public void testHelp() throws Exception { + doJvmTest("compiler/testData/cli/jvm/help.args"); + } + + @TestMetadata("multipleTextRangesInDiagnosticsOrder.args") + public void testMultipleTextRangesInDiagnosticsOrder() throws Exception { + doJvmTest("compiler/testData/cli/jvm/multipleTextRangesInDiagnosticsOrder.args"); + } + + @TestMetadata("nonExistingClassPathAndAnnotationsPath.args") + public void testNonExistingClassPathAndAnnotationsPath() throws Exception { + doJvmTest("compiler/testData/cli/jvm/nonExistingClassPathAndAnnotationsPath.args"); + } + + @TestMetadata("nonExistingSourcePath.args") + public void testNonExistingSourcePath() throws Exception { + doJvmTest("compiler/testData/cli/jvm/nonExistingSourcePath.args"); + } + + @TestMetadata("printArguments.args") + public void testPrintArguments() throws Exception { + doJvmTest("compiler/testData/cli/jvm/printArguments.args"); + } + + @TestMetadata("script.args") + public void testScript() throws Exception { + doJvmTest("compiler/testData/cli/jvm/script.args"); + } + + @TestMetadata("simple.args") + public void testSimple() throws Exception { + doJvmTest("compiler/testData/cli/jvm/simple.args"); + } + + @TestMetadata("suppressAllWarningsLowercase.args") + public void testSuppressAllWarningsLowercase() throws Exception { + doJvmTest("compiler/testData/cli/jvm/suppressAllWarningsLowercase.args"); + } + + @TestMetadata("suppressAllWarningsMixedCase.args") + public void testSuppressAllWarningsMixedCase() throws Exception { + doJvmTest("compiler/testData/cli/jvm/suppressAllWarningsMixedCase.args"); + } + + @TestMetadata("wrongAbiVersion.args") + public void testWrongAbiVersion() throws Exception { + doJvmTest("compiler/testData/cli/jvm/wrongAbiVersion.args"); + } + + @TestMetadata("wrongArgument.args") + public void testWrongArgument() throws Exception { + doJvmTest("compiler/testData/cli/jvm/wrongArgument.args"); + } + + @TestMetadata("wrongKotlinSignature.args") + public void testWrongKotlinSignature() throws Exception { + doJvmTest("compiler/testData/cli/jvm/wrongKotlinSignature.args"); + } + + @TestMetadata("compiler/testData/cli/jvm/wrongAbiVersionLib") + @InnerTestClasses({}) + public static class WrongAbiVersionLib extends AbstractKotlincExecutableTest { + public void testAllFilesPresentInWrongAbiVersionLib() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/cli/jvm/wrongAbiVersionLib"), Pattern.compile("^(.+)\\.args$"), true); + } + + public static Test innerSuite() { + TestSuite suite = new TestSuite("WrongAbiVersionLib"); + suite.addTestSuite(WrongAbiVersionLib.class); + return suite; + } + } + + public static Test innerSuite() { + TestSuite suite = new TestSuite("Jvm"); + suite.addTestSuite(Jvm.class); + suite.addTest(WrongAbiVersionLib.innerSuite()); + return suite; + } + } + + @TestMetadata("compiler/testData/cli/js") + public static class Js extends AbstractKotlincExecutableTest { + public void testAllFilesPresentInJs() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/cli/js"), Pattern.compile("^(.+)\\.args$"), true); + } + + @TestMetadata("outputPostfixFileNotFound.args") + public void testOutputPostfixFileNotFound() throws Exception { + doJsTest("compiler/testData/cli/js/outputPostfixFileNotFound.args"); + } + + @TestMetadata("outputPrefixFileNotFound.args") + public void testOutputPrefixFileNotFound() throws Exception { + doJsTest("compiler/testData/cli/js/outputPrefixFileNotFound.args"); + } + + @TestMetadata("printArgumentsWithManyValue.args") + public void testPrintArgumentsWithManyValue() throws Exception { + doJsTest("compiler/testData/cli/js/printArgumentsWithManyValue.args"); + } + + @TestMetadata("simple2js.args") + public void testSimple2js() throws Exception { + doJsTest("compiler/testData/cli/js/simple2js.args"); + } + + @TestMetadata("suppressAllWarningsJS.args") + public void testSuppressAllWarningsJS() throws Exception { + doJsTest("compiler/testData/cli/js/suppressAllWarningsJS.args"); + } + + } + + public static Test suite() { + TestSuite suite = new TestSuite("KotlincExecutableTestGenerated"); + suite.addTest(Jvm.innerSuite()); + suite.addTestSuite(Js.class); + return suite; + } +} diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java index b9094f25f9f..53cc7cdb03e 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve; import org.jetbrains.jet.checkers.AbstractJetJsCheckerTest; import org.jetbrains.jet.checkers.AbstractJetPsiCheckerTest; +import org.jetbrains.jet.cli.AbstractKotlincExecutableTest; import org.jetbrains.jet.codegen.AbstractBytecodeTextTest; import org.jetbrains.jet.codegen.AbstractCheckLocalVariablesTableTest; import org.jetbrains.jet.codegen.AbstractTopLevelMembersInvocationTest; @@ -256,6 +257,14 @@ public class GenerateTests { testModel("compiler/testData/writeSignature", true, "kt", "doTest") ); + generateTest( + "compiler/tests/", + "KotlincExecutableTestGenerated", + AbstractKotlincExecutableTest.class, + testModel("compiler/testData/cli/jvm", true, "args", "doJvmTest"), + testModel("compiler/testData/cli/js", true, "args", "doJsTest") + ); + generateTest( "idea/tests/", "JetPsiMatcherTest",