[K2, CLI] Support endOffset in Kotlin CLI

Duplicated messages in testdata appeared because default renderer
renders diagnostic spans ambiguously. It shows only start position.
In fact, there are 3 failures, 2 of them distinguish only by the
diagnostic end offset. See youtrack for more information.

The issue about inconvenient rendering is KT-64989.

#KT-64608
This commit is contained in:
Evgeniy.Zhelenskiy
2024-01-12 03:08:09 +01:00
committed by Space Team
parent 6404cede07
commit f05c972efb
12 changed files with 154 additions and 28 deletions
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.cli.common.CLITool;
import org.jetbrains.kotlin.cli.common.CompilerSystemProperties;
import org.jetbrains.kotlin.cli.common.ExitCode;
import org.jetbrains.kotlin.cli.common.Usage;
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer;
import org.jetbrains.kotlin.cli.js.K2JSCompiler;
import org.jetbrains.kotlin.cli.js.dce.K2JSDce;
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
@@ -62,7 +63,18 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
private static final String BUILD_FILE_ARGUMENT_PREFIX = "-Xbuild-file=";
public static Pair<String, ExitCode> executeCompilerGrabOutput(@NotNull CLITool<?> compiler, @NotNull List<String> args) {
public static Pair<String, ExitCode> executeCompilerGrabOutput(
@NotNull CLITool<?> compiler,
@NotNull List<String> args
) {
return executeCompilerGrabOutput(compiler, args, null);
}
public static Pair<String, ExitCode> executeCompilerGrabOutput(
@NotNull CLITool<?> compiler,
@NotNull List<String> args,
@Nullable MessageRenderer messageRenderer
) {
StringBuilder output = new StringBuilder();
int index = 0;
@@ -73,7 +85,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
} else {
next = index + next;
}
Pair<String, ExitCode> pair = CompilerTestUtil.executeCompiler(compiler, args.subList(index, next));
Pair<String, ExitCode> pair = CompilerTestUtil.executeCompiler(compiler, args.subList(index, next), messageRenderer);
output.append(pair.getFirst());
if (pair.getSecond() != ExitCode.OK) {
return new Pair<>(output.toString(), pair.getSecond());
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.test
import org.jetbrains.kotlin.cli.common.CLITool
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.test.util.KtTestUtil
import java.io.ByteArrayOutputStream
@@ -27,21 +28,22 @@ import kotlin.test.assertEquals
object CompilerTestUtil {
@JvmStatic
fun executeCompilerAssertSuccessful(compiler: CLITool<*>, args: List<String>) {
val (output, exitCode) = executeCompiler(compiler, args)
fun executeCompilerAssertSuccessful(compiler: CLITool<*>, args: List<String>, messageRenderer: MessageRenderer? = null) {
val (output, exitCode) = executeCompiler(compiler, args, messageRenderer)
assertEquals(ExitCode.OK, exitCode, output)
}
@JvmStatic
fun executeCompiler(compiler: CLITool<*>, args: List<String>): Pair<String, ExitCode> {
fun executeCompiler(compiler: CLITool<*>, args: List<String>, messageRenderer: MessageRenderer? = null): Pair<String, ExitCode> {
val bytes = ByteArrayOutputStream()
val origErr = System.err
try {
System.setErr(PrintStream(bytes))
val exitCode = CLITool.doMainNoExit(compiler, args.toTypedArray())
val exitCode =
if (messageRenderer == null) CLITool.doMainNoExit(compiler, args.toTypedArray())
else CLITool.doMainNoExit(compiler, args.toTypedArray(), messageRenderer)
return Pair(String(bytes.toByteArray()), exitCode)
}
finally {
} finally {
System.setErr(origErr)
}
}
@@ -52,7 +54,8 @@ object CompilerTestUtil {
src: File,
libraryName: String = "library",
extraOptions: List<String> = emptyList(),
extraClasspath: List<File> = emptyList()
extraClasspath: List<File> = emptyList(),
messageRenderer: MessageRenderer? = null,
): File {
val destination = File(KtTestUtil.tmpDir("testLibrary"), "$libraryName.jar")
val args = mutableListOf<String>().apply {
@@ -65,7 +68,7 @@ object CompilerTestUtil {
}
addAll(extraOptions)
}
executeCompilerAssertSuccessful(K2JVMCompiler(), args)
executeCompilerAssertSuccessful(K2JVMCompiler(), args, messageRenderer)
return destination
}
}