Scripting: fix CLI REPL error reporting behavior

discrepancy after switching to the new REPL internals was
discovered also on IJ plugin tests, so added a test emulating it.
also some test infra refactoring
This commit is contained in:
Ilya Chernikov
2022-06-27 14:01:46 +02:00
parent 8bc43917ec
commit 6e59c2e079
5 changed files with 80 additions and 17 deletions
@@ -29,7 +29,9 @@ import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.TestJdkKind
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
import org.junit.Assert
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
import java.io.PrintWriter
import java.util.*
import java.util.regex.Pattern
@@ -86,6 +88,24 @@ abstract class AbstractReplInterpreterTest : KtUsefulTestCase() {
return result
}
internal fun <T> captureOutErrRet(body: () -> T): Triple<String, String, T> {
val outStream = ByteArrayOutputStream()
val errStream = ByteArrayOutputStream()
val prevOut = System.out
val prevErr = System.err
System.setOut(PrintStream(outStream))
System.setErr(PrintStream(errStream))
val ret = try {
body()
} finally {
System.out.flush()
System.err.flush()
System.setOut(prevOut)
System.setErr(prevErr)
}
return Triple(outStream.toString().trim(), errStream.toString().trim(), ret)
}
protected fun doTest(path: String) {
val configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK)
loadScriptingPlugin(configuration)
@@ -101,7 +121,7 @@ abstract class AbstractReplInterpreterTest : KtUsefulTestCase() {
)
for ((code, expected) in loadLines(File(path))) {
val lineResult = repl.eval(code)
val (output, _, lineResult) = captureOutErrRet { repl.eval(code) }
if (DUMP_BYTECODE) {
repl.dumpClasses(PrintWriter(System.out))
@@ -109,7 +129,7 @@ abstract class AbstractReplInterpreterTest : KtUsefulTestCase() {
val actual = when (lineResult) {
is ReplEvalResult.ValueResult -> lineResult.value.toString()
is ReplEvalResult.Error.CompileTime -> MessageRenderer.WITHOUT_PATHS.render(CompilerMessageSeverity.ERROR, lineResult.message, lineResult.location)
is ReplEvalResult.Error.CompileTime -> output
is ReplEvalResult.Error -> lineResult.message
is ReplEvalResult.Incomplete -> INCOMPLETE_LINE_MESSAGE
is ReplEvalResult.UnitResult -> ""