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:
Christoph Sturm
2023-03-01 15:38:53 +01:00
committed by GitHub
parent 2f5064176e
commit be90be58eb
5 changed files with 20 additions and 26 deletions
@@ -18,6 +18,7 @@ dependencies {
testImplementation(kotlin("test-junit5")) testImplementation(kotlin("test-junit5"))
testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable") testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.4.9") testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.4.9")
testImplementation(enforcedPlatform("org.junit:junit-bom:5.9.1"))
} }
tasks.withType<KotlinCompile> { tasks.withType<KotlinCompile> {
@@ -19,11 +19,11 @@ package com.bnorm.power
import com.tschuchort.compiletesting.KotlinCompilation import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile import com.tschuchort.compiletesting.SourceFile
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.fail
class DebugFunctionTest { class DebugFunctionTest {
@Test @Test
@@ -71,7 +71,7 @@ private fun executeMainDebug(mainBody: String): String {
fun <T> dbg(value: T): T = value fun <T> dbg(value: T): T = value
fun <T> dbg(value: T, msg: String): T { fun <T> dbg(value: T, msg: String): T {
println(msg) throw RuntimeException("result:"+msg)
return value return value
} }
@@ -87,15 +87,18 @@ fun main() {
val kClazz = result.classLoader.loadClass("MainKt") val kClazz = result.classLoader.loadClass("MainKt")
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 } val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
val prevOut = System.out return getMainResult(main)
}
fun getMainResult(main: Method): String {
try { try {
val out = ByteArrayOutputStream()
System.setOut(PrintStream(out))
main.invoke(null) main.invoke(null)
return out.toString("UTF-8") fail("main did not throw expected exception")
} catch (t: InvocationTargetException) { } catch (t: InvocationTargetException) {
with(t.cause) {
if (this is RuntimeException && message != null && message!!.startsWith("result:"))
return message!!.substringAfter("result:")
}
throw t.cause!! 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)) val result = compile(listOf(file), PowerAssertComponentRegistrar(fqNames))
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode, "Failed with messages: " + result.messages) 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 } val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
try { try {
try { try {
@@ -19,9 +19,6 @@ package com.bnorm.power
import com.tschuchort.compiletesting.KotlinCompilation import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile import com.tschuchort.compiletesting.SourceFile
import org.jetbrains.kotlin.name.FqName 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.Test
import kotlin.test.assertEquals 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): T = value
fun <T> dbg(key: Any, value: T, msg: String): T { 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 return value
} }
@@ -156,15 +153,5 @@ fun main() {
val kClazz = result.classLoader.loadClass("MainKt") val kClazz = result.classLoader.loadClass("MainKt")
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 } val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
val prevOut = System.out return getMainResult(main)
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)
}
} }
@@ -0,0 +1,3 @@
# suppress inspection "UnusedProperty" for whole file
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent