[minor] Rearrange test utils for easier reuse

This commit is contained in:
Ilya Chernikov
2019-08-27 11:29:43 +02:00
parent 947867286c
commit fda37eaaae
4 changed files with 174 additions and 131 deletions
@@ -5,93 +5,24 @@
package org.jetbrains.kotlin.mainKts.test
import junit.framework.Assert.*
import junit.framework.Assert
import org.jetbrains.kotlin.scripting.compiler.plugin.runWithK2JVMCompiler
import org.jetbrains.kotlin.scripting.compiler.plugin.runWithKotlinc
import org.junit.Test
import java.io.File
import java.io.InputStream
import java.util.concurrent.TimeUnit
import kotlin.concurrent.thread
class MainKtsIT {
// TODO: partially copypasted from LauncherReplTest, consider extracting common parts to some (new) test util module
private fun runWithKotlinc(
scriptPath: String,
expectedOutPatterns: List<String> = emptyList(),
expectedExitCode: Int = 0,
workDirectory: File? = null
) {
val executableName = "kotlinc"
// TODO:
val executableFileName =
if (System.getProperty("os.name").contains("windows", ignoreCase = true)) "$executableName.bat" else executableName
val launcherFile = File("dist/kotlinc/bin/$executableFileName")
assertTrue("Launcher script not found, run dist task: ${launcherFile.absolutePath}", launcherFile.exists())
val mainKtsJar = File("dist/kotlinc/lib/kotlin-main-kts.jar")
assertTrue("kotlin-main-kts.jar not found, run dist task: ${mainKtsJar.absolutePath}", mainKtsJar.exists())
val processBuilder = ProcessBuilder(launcherFile.absolutePath, "-cp", mainKtsJar.absolutePath, "-script", scriptPath)
if (workDirectory != null) {
processBuilder.directory(workDirectory)
}
val process = processBuilder.start()
data class ExceptionContainer(
var value: Throwable? = null
)
fun InputStream.captureStream(): Triple<Thread, ExceptionContainer, ArrayList<String>> {
val out = ArrayList<String>()
val exceptionContainer = ExceptionContainer()
val thread = thread {
try {
reader().forEachLine {
out.add(it.trim())
}
} catch (e: Throwable) {
exceptionContainer.value = e
}
}
return Triple(thread, exceptionContainer, out)
}
val (stdoutThread, stdoutException, processOut) = process.inputStream.captureStream()
val (stderrThread, stderrException, processErr) = process.errorStream.captureStream()
process.waitFor(30000, TimeUnit.MILLISECONDS)
try {
if (process.isAlive) {
process.destroyForcibly()
fail("Process terminated forcibly")
}
stdoutThread.join(300)
assertFalse("stdout thread not finished", stdoutThread.isAlive)
assertNull(stdoutException.value)
stderrThread.join(300)
assertFalse("stderr thread not finished", stderrThread.isAlive)
assertNull(stderrException.value)
assertEquals(expectedOutPatterns.size, processOut.size)
for ((expectedPattern, actualLine) in expectedOutPatterns.zip(processOut)) {
assertTrue(
"line \"$actualLine\" do not match with expected pattern \"$expectedPattern\"",
Regex(expectedPattern).matches(actualLine)
)
}
assertEquals(expectedExitCode, process.exitValue())
} catch (e: Throwable) {
println("OUT:\n${processOut.joinToString("\n")}")
println("ERR:\n${processErr.joinToString("\n")}")
throw e
}
}
@Test
fun testResolveJunit() {
runWithKotlinc("$TEST_DATA_ROOT/hello-resolve-junit.main.kts", listOf("Hello, World!"))
runWithKotlinc(
"$TEST_DATA_ROOT/hello-resolve-junit.main.kts", listOf("Hello, World!"),
classpath = listOf(
File("dist/kotlinc/lib/kotlin-main-kts.jar").also {
Assert.assertTrue("kotlin-main-kts.jar not found, run dist task: ${it.absolutePath}", it.exists())
}
)
)
}
@Test