configure to run tests in parallel and eliminate global state (#73)
* bump junit, run tests in parallel. * fix global state by not using System.out
This commit is contained in:
@@ -18,6 +18,7 @@ dependencies {
|
||||
testImplementation(kotlin("test-junit5"))
|
||||
testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
|
||||
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.4.9")
|
||||
testImplementation(enforcedPlatform("org.junit:junit-bom:5.9.1"))
|
||||
}
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
|
||||
@@ -19,11 +19,11 @@ package com.bnorm.power
|
||||
import com.tschuchort.compiletesting.KotlinCompilation
|
||||
import com.tschuchort.compiletesting.SourceFile
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.lang.reflect.Method
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.fail
|
||||
|
||||
class DebugFunctionTest {
|
||||
@Test
|
||||
@@ -71,7 +71,7 @@ private fun executeMainDebug(mainBody: String): String {
|
||||
fun <T> dbg(value: T): T = value
|
||||
|
||||
fun <T> dbg(value: T, msg: String): T {
|
||||
println(msg)
|
||||
throw RuntimeException("result:"+msg)
|
||||
return value
|
||||
}
|
||||
|
||||
@@ -87,15 +87,18 @@ fun main() {
|
||||
|
||||
val kClazz = result.classLoader.loadClass("MainKt")
|
||||
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
|
||||
val prevOut = System.out
|
||||
return getMainResult(main)
|
||||
}
|
||||
|
||||
fun getMainResult(main: Method): String {
|
||||
try {
|
||||
val out = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(out))
|
||||
main.invoke(null)
|
||||
return out.toString("UTF-8")
|
||||
fail("main did not throw expected exception")
|
||||
} catch (t: InvocationTargetException) {
|
||||
with(t.cause) {
|
||||
if (this is RuntimeException && message != null && message!!.startsWith("result:"))
|
||||
return message!!.substringAfter("result:")
|
||||
}
|
||||
throw t.cause!!
|
||||
} finally {
|
||||
System.setOut(prevOut)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,11 +349,11 @@ class InfixFunctionTest {
|
||||
)
|
||||
}
|
||||
|
||||
private fun run(file: SourceFile, fqNames: Set<FqName>): String {
|
||||
private fun run(file: SourceFile, fqNames: Set<FqName>, main: String = "MainKt"): String {
|
||||
val result = compile(listOf(file), PowerAssertComponentRegistrar(fqNames))
|
||||
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode, "Failed with messages: " + result.messages)
|
||||
|
||||
val kClazz = result.classLoader.loadClass("MainKt")
|
||||
val kClazz = result.classLoader.loadClass(main)
|
||||
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
|
||||
try {
|
||||
try {
|
||||
|
||||
+2
-15
@@ -19,9 +19,6 @@ package com.bnorm.power
|
||||
import com.tschuchort.compiletesting.KotlinCompilation
|
||||
import com.tschuchort.compiletesting.SourceFile
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@@ -140,7 +137,7 @@ private fun executeMainDebug(mainBody: String): String {
|
||||
fun <T> dbg(key: Any, value: T): T = value
|
||||
|
||||
fun <T> dbg(key: Any, value: T, msg: String): T {
|
||||
println(key.toString() + "=" + value + "\n" + msg)
|
||||
throw RuntimeException("result:"+key.toString() + "=" + value + "\n" + msg)
|
||||
return value
|
||||
}
|
||||
|
||||
@@ -156,15 +153,5 @@ fun main() {
|
||||
|
||||
val kClazz = result.classLoader.loadClass("MainKt")
|
||||
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
|
||||
val prevOut = System.out
|
||||
try {
|
||||
val out = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(out))
|
||||
main.invoke(null)
|
||||
return out.toString("UTF-8")
|
||||
} catch (t: InvocationTargetException) {
|
||||
throw t.cause!!
|
||||
} finally {
|
||||
System.setOut(prevOut)
|
||||
}
|
||||
return getMainResult(main)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# suppress inspection "UnusedProperty" for whole file
|
||||
junit.jupiter.execution.parallel.enabled = true
|
||||
junit.jupiter.execution.parallel.mode.default = concurrent
|
||||
Reference in New Issue
Block a user