Extract compiler-executing code in AbstractCliTest to CompilerTestUtil
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.kotlin.test
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.CLITool
|
||||
import org.jetbrains.kotlin.cli.common.ExitCode
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
object CompilerTestUtil {
|
||||
@JvmStatic
|
||||
fun executeCompilerAssertSuccessful(compiler: CLITool<*>, args: List<String>) {
|
||||
val (output, exitCode) = executeCompiler(compiler, args)
|
||||
assertEquals(ExitCode.OK, exitCode, output)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun executeCompiler(compiler: CLITool<*>, args: List<String>): Pair<String, ExitCode> {
|
||||
val bytes = ByteArrayOutputStream()
|
||||
val origErr = System.err
|
||||
try {
|
||||
System.setErr(PrintStream(bytes))
|
||||
val exitCode = CLITool.doMainNoExit(compiler, args.toTypedArray())
|
||||
return Pair(String(bytes.toByteArray()), exitCode)
|
||||
}
|
||||
finally {
|
||||
System.setErr(origErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.cli;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import kotlin.Pair;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import kotlin.io.FilesKt;
|
||||
@@ -32,18 +31,16 @@ import org.jetbrains.kotlin.cli.js.dce.K2JSDce;
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
|
||||
import org.jetbrains.kotlin.config.KotlinCompilerVersion;
|
||||
import org.jetbrains.kotlin.load.kotlin.JvmMetadataVersion;
|
||||
import org.jetbrains.kotlin.test.CompilerTestUtil;
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TestCaseWithTmpdir;
|
||||
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
|
||||
import org.jetbrains.kotlin.utils.JsMetadataVersion;
|
||||
import org.jetbrains.kotlin.utils.PathUtil;
|
||||
import org.jetbrains.kotlin.utils.StringsKt;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -51,29 +48,24 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
private static final String TESTDATA_DIR = "$TESTDATA_DIR$";
|
||||
|
||||
public static Pair<String, ExitCode> executeCompilerGrabOutput(@NotNull CLITool<?> compiler, @NotNull List<String> args) {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
PrintStream origErr = System.err;
|
||||
try {
|
||||
System.setErr(new PrintStream(bytes));
|
||||
ExitCode exitCode;
|
||||
int index = 0;
|
||||
do {
|
||||
int next = args.subList(index, args.size()).indexOf("---");
|
||||
if (next == -1) {
|
||||
next = args.size();
|
||||
}
|
||||
exitCode = CLITool.doMainNoExit(compiler, ArrayUtil.toStringArray(args.subList(index, next)));
|
||||
if (exitCode != ExitCode.OK) break;
|
||||
index = next + 1;
|
||||
} while (index < args.size());
|
||||
return new Pair<>(bytes.toString("utf-8"), exitCode);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw ExceptionUtilsKt.rethrow(e);
|
||||
}
|
||||
finally {
|
||||
System.setErr(origErr);
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
int index = 0;
|
||||
do {
|
||||
int next = args.subList(index, args.size()).indexOf("---");
|
||||
if (next == -1) {
|
||||
next = args.size();
|
||||
}
|
||||
Pair<String, ExitCode> pair = CompilerTestUtil.executeCompiler(compiler, args.subList(index, next));
|
||||
output.append(pair.getFirst());
|
||||
if (pair.getSecond() != ExitCode.OK) {
|
||||
return new Pair<>(output.toString(), pair.getSecond());
|
||||
}
|
||||
index = next + 1;
|
||||
}
|
||||
while (index < args.size());
|
||||
|
||||
return new Pair<>(output.toString(), ExitCode.OK);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||
import org.jetbrains.kotlin.config.Services
|
||||
import org.jetbrains.kotlin.test.CompilerTestUtil
|
||||
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
|
||||
import org.junit.Assert
|
||||
import java.io.File
|
||||
@@ -37,8 +38,7 @@ class FriendPathsTest : TestCaseWithTmpdir() {
|
||||
fun testArchive() {
|
||||
val libSrc = File(getTestDataDirectory(), "lib.kt")
|
||||
val libDest = File(tmpdir, "lib.jar")
|
||||
val (output, libExitCode) = AbstractCliTest.executeCompilerGrabOutput(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
|
||||
Assert.assertEquals(output, ExitCode.OK, libExitCode)
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
|
||||
|
||||
Assert.assertEquals(ExitCode.OK, invokeCompiler(libDest.path))
|
||||
}
|
||||
@@ -46,8 +46,7 @@ class FriendPathsTest : TestCaseWithTmpdir() {
|
||||
fun testDirectory() {
|
||||
val libSrc = File(getTestDataDirectory(), "lib.kt")
|
||||
val libDest = File(tmpdir, "lib")
|
||||
val (output, libExitCode) = AbstractCliTest.executeCompilerGrabOutput(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
|
||||
Assert.assertEquals(output, ExitCode.OK, libExitCode)
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
|
||||
|
||||
Assert.assertEquals(ExitCode.OK, invokeCompiler(libDest.path))
|
||||
}
|
||||
|
||||
@@ -16,14 +16,13 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen
|
||||
|
||||
import org.jetbrains.kotlin.cli.AbstractCliTest
|
||||
import org.jetbrains.kotlin.cli.common.ExitCode
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||
import org.jetbrains.kotlin.config.ApiVersion
|
||||
import org.jetbrains.kotlin.config.LanguageVersion
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl
|
||||
import org.jetbrains.kotlin.load.kotlin.ModuleMapping
|
||||
import org.jetbrains.kotlin.resolve.CompilerDeserializationConfiguration
|
||||
import org.jetbrains.kotlin.test.CompilerTestUtil
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
|
||||
import java.io.File
|
||||
@@ -38,14 +37,12 @@ class JvmPackageTableTest : KtUsefulTestCase() {
|
||||
val tmpdir = KotlinTestUtils.tmpDir(this::class.simpleName)
|
||||
|
||||
val moduleName = "main"
|
||||
val (output, exitCode) = AbstractCliTest.executeCompilerGrabOutput(K2JVMCompiler(), listOf(
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf(
|
||||
directory,
|
||||
"-d", tmpdir.path,
|
||||
"-module-name", moduleName,
|
||||
"-language-version", compileWith.versionString
|
||||
))
|
||||
System.err.println(output) // normally output is empty
|
||||
assertEquals("Compilation should complete successfully", ExitCode.OK, exitCode)
|
||||
|
||||
val mapping = ModuleMapping.create(
|
||||
File(tmpdir, "META-INF/$moduleName.${ModuleMapping.MAPPING_FILE_EXT}").readBytes(), "test",
|
||||
|
||||
Reference in New Issue
Block a user