From a8cd11f0f957ec02553a1f135c0eab031d683fe2 Mon Sep 17 00:00:00 2001 From: Maxim Manuylov Date: Sun, 29 Jul 2012 23:30:49 +0400 Subject: [PATCH] more tests for compiler --- .../jetbrains/jet/cli/common/CLICompiler.java | 24 ++++++++++++------- compiler/testData/cli/hello.ktscript | 1 + compiler/testData/cli/help.out | 2 +- compiler/testData/cli/printArguments.out | 5 ++++ compiler/testData/cli/wrongArgument.out | 19 +++++++++++++++ .../org/jetbrains/jet/cli/jvm/CliTest.java | 20 +++++++++++----- 6 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 compiler/testData/cli/hello.ktscript create mode 100644 compiler/testData/cli/printArguments.out create mode 100644 compiler/testData/cli/wrongArgument.out diff --git a/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java index a4e67c95285..0f1e147eb31 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java @@ -148,22 +148,30 @@ public abstract class CLICompiler { * Useful main for derived command line tools */ public static void doMain(@NotNull CLICompiler compiler, @NotNull String[] args) { + ExitCode exitCode = doDoMain(compiler, args); + if (exitCode != OK) { + System.exit(exitCode.getCode()); + } + } + + @NotNull + public static ExitCode doDoMain(CLICompiler compiler, String[] args) { try { ExitCode rc = compiler.exec(System.out, args); if (rc != OK) { System.err.println("exec() finished with " + rc + " return code"); - if (Boolean.parseBoolean(System.getProperty("kotlin.print.cmd.args"))) { - System.err.println("Command line arguments: "); - for (String arg : args) { - System.err.println(arg); - } - } - System.exit(rc.getCode()); } + if (Boolean.parseBoolean(System.getProperty("kotlin.print.cmd.args"))) { + System.out.println("Command line arguments:"); + for (String arg : args) { + System.out.println(arg); + } + } + return rc; } catch (CompileEnvironmentException e) { System.err.println(e.getMessage()); - System.exit(INTERNAL_ERROR.getCode()); + return INTERNAL_ERROR; } } } diff --git a/compiler/testData/cli/hello.ktscript b/compiler/testData/cli/hello.ktscript new file mode 100644 index 00000000000..99e79b45715 --- /dev/null +++ b/compiler/testData/cli/hello.ktscript @@ -0,0 +1 @@ +println("hello") diff --git a/compiler/testData/cli/help.out b/compiler/testData/cli/help.out index 22cfb0df5a7..84fd7fd6d21 100644 --- a/compiler/testData/cli/help.out +++ b/compiler/testData/cli/help.out @@ -15,4 +15,4 @@ Usage: org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments -verbose [flag] Enable verbose logging output -version [flag] Display compiler version -help (-h) [flag] show help -INTERNAL_ERROR +OK diff --git a/compiler/testData/cli/printArguments.out b/compiler/testData/cli/printArguments.out new file mode 100644 index 00000000000..172db5d6aaa --- /dev/null +++ b/compiler/testData/cli/printArguments.out @@ -0,0 +1,5 @@ +hello +Command line arguments: +-script +compiler/testData/cli/hello.ktscript +OK diff --git a/compiler/testData/cli/wrongArgument.out b/compiler/testData/cli/wrongArgument.out new file mode 100644 index 00000000000..5435afc50af --- /dev/null +++ b/compiler/testData/cli/wrongArgument.out @@ -0,0 +1,19 @@ +Invalid argument: -wrongArgument +Usage: org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments + -jar [String] jar file name + -src [String] source file or directory + -classpath [String] classpath to use when compiling + -annotations [String] paths to external annotations + -includeRuntime [flag] include Kotlin runtime in to resulting jar + -noJdk [flag] don't include Java runtime into classpath + -noStdlib [flag] don't include Kotlin runtime into classpath + -noJdkAnnotations [flag] don't include JDK external annotations into classpath + -builtins [flag] compile builtin classes (internal) + -output [String] output directory + -module [String] module to compile + -script [flag] evaluate script + -tags [flag] Demarcate each compilation message (error, warning, etc) with an open and close tag + -verbose [flag] Enable verbose logging output + -version [flag] Display compiler version + -help (-h) [flag] show help +INTERNAL_ERROR diff --git a/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java b/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java index d314e30877c..4e11b3e25e0 100644 --- a/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java +++ b/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java @@ -17,9 +17,9 @@ package org.jetbrains.jet.cli.jvm; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.vfs.LocalFileSystem; import junit.framework.Assert; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.cli.common.CLICompiler; import org.jetbrains.jet.cli.common.ExitCode; import org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler; import org.jetbrains.jet.lang.parsing.JetScriptDefinition; @@ -37,10 +37,8 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStream; import java.io.StringReader; -import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.LinkedList; -import java.util.List; /** * @author Stepan Koltsov @@ -57,9 +55,8 @@ public class CliTest { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); PrintStream origOut = System.out; try { - // we change System.out because scripts ignore passed OutputStream and write to System.out System.setOut(new PrintStream(bytes)); - ExitCode exitCode = new K2JVMCompiler().exec(System.out, args); + ExitCode exitCode = CLICompiler.doDoMain(new K2JVMCompiler(), args); return bytes.toString("utf-8") + exitCode + "\n"; } catch (Exception e) { @@ -115,7 +112,7 @@ public class CliTest { @Test public void help() throws Exception { - executeCompilerCompareOutput(new String[] {"--help"}); + executeCompilerCompareOutput(new String[] {"-help"}); } @Test @@ -128,6 +125,17 @@ public class CliTest { executeCompilerCompareOutput(new String[]{ "-src", "compiler/testData/cli/ideTemplates.kt", "-output", tmpdir.getTmpDir().getPath()}); } + @Test + public void wrongArgument() { + executeCompilerCompareOutput(new String[] { "-wrongArgument" }); + } + + @Test + public void printArguments() { + System.setProperty("kotlin.print.cmd.args", "true"); + executeCompilerCompareOutput(new String[] {"-script", "compiler/testData/cli/hello.ktscript"}); + } + @Test public void testScript() { LinkedList scriptParameters = new LinkedList();