[Native][tests] Handle unexpected errors during test run

This commit is contained in:
Dmitriy Dolovov
2021-11-17 11:44:12 +03:00
parent f33618330d
commit 200d92c287
3 changed files with 46 additions and 24 deletions
@@ -70,20 +70,7 @@ internal abstract class LoggedData {
} }
} }
class CompilerCallUnexpectedFailure( class CompilerCallUnexpectedFailure(parameters: CompilerParameters, throwable: Throwable) : UnexpectedFailure(parameters, throwable)
private val parameters: CompilerParameters,
private val throwable: Throwable
) : LoggedData() {
override fun computeText() = buildString {
appendLine("UNEXPECTED FAILURE IN COMPILER:")
appendLine("$throwable")
appendLine()
appendLine("STACK TRACE:")
appendLine(throwable.stackTraceToString())
appendLine()
appendLine(parameters)
}
}
class TestRunParameters( class TestRunParameters(
private val compilerCall: CompilerCall, private val compilerCall: CompilerCall,
@@ -135,6 +122,23 @@ internal abstract class LoggedData {
} }
} }
class TestRunUnexpectedFailure(parameters: TestRunParameters, throwable: Throwable) : UnexpectedFailure(parameters, throwable)
abstract class UnexpectedFailure(
private val parameters: LoggedData,
private val throwable: Throwable
) : LoggedData() {
override fun computeText() = buildString {
appendLine("ERROR MESSAGE:")
appendLine("${throwable.message}")
appendLine()
appendLine("STACK TRACE:")
appendLine(throwable.stackTraceToString().trimEnd())
appendLine()
appendLine(parameters)
}
}
companion object { companion object {
@JvmStatic @JvmStatic
protected fun StringBuilder.appendList(header: String, list: Iterable<Any?>): StringBuilder { protected fun StringBuilder.appendList(header: String, list: Iterable<Any?>): StringBuilder {
@@ -8,17 +8,25 @@ package org.jetbrains.kotlin.konan.blackboxtest.runner
import org.jetbrains.kotlin.konan.blackboxtest.LoggedData import org.jetbrains.kotlin.konan.blackboxtest.LoggedData
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEquals import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEquals
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertTrue import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertTrue
import java.lang.AssertionError
internal abstract class AbstractRunner<R> { internal abstract class AbstractRunner<R> {
protected abstract fun buildRun(): AbstractRun protected abstract fun buildRun(): AbstractRun
protected abstract fun buildResultHandler(runResult: RunResult): ResultHandler protected abstract fun buildResultHandler(runResult: RunResult): ResultHandler
protected abstract fun handleUnexpectedFailure(t: Throwable): Nothing
fun run(): R { fun run(): R = try {
val run = buildRun() val run = buildRun()
val runResult = run.run() val runResult = run.run()
val resultHandler = buildResultHandler(runResult) val resultHandler = buildResultHandler(runResult)
return resultHandler.handle() resultHandler.handle()
} catch (t: Throwable) {
if (t is AssertionError)
throw t
else {
// An unexpected failure.
handleUnexpectedFailure(t)
}
} }
interface AbstractRun { interface AbstractRun {
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.konan.blackboxtest.LoggedData
import org.jetbrains.kotlin.konan.blackboxtest.TestRun import org.jetbrains.kotlin.konan.blackboxtest.TestRun
import org.jetbrains.kotlin.konan.blackboxtest.TestRunParameter import org.jetbrains.kotlin.konan.blackboxtest.TestRunParameter
import org.jetbrains.kotlin.konan.blackboxtest.get import org.jetbrains.kotlin.konan.blackboxtest.get
import org.jetbrains.kotlin.test.services.JUnit5Assertions.fail
internal class LocalTestRunner(private val testRun: TestRun) : AbstractLocalProcessRunner<Unit>() { internal class LocalTestRunner(private val testRun: TestRun) : AbstractLocalProcessRunner<Unit>() {
override val visibleProcessName get() = "Tested process" override val visibleProcessName get() = "Tested process"
@@ -20,12 +21,13 @@ internal class LocalTestRunner(private val testRun: TestRun) : AbstractLocalProc
testRun.runParameters.forEach { it.applyTo(this) } testRun.runParameters.forEach { it.applyTo(this) }
} }
private fun getLoggedParameters() = LoggedData.TestRunParameters( private val loggedParameters: LoggedData.TestRunParameters
compilerCall = executable.loggedCompilerCall, get() = LoggedData.TestRunParameters(
origin = testRun.origin, compilerCall = executable.loggedCompilerCall,
runArgs = programArgs, origin = testRun.origin,
runParameters = testRun.runParameters runArgs = programArgs,
) runParameters = testRun.runParameters
)
override fun customizeProcess(process: Process) { override fun customizeProcess(process: Process) {
testRun.runParameters.get<TestRunParameter.WithInputData> { testRun.runParameters.get<TestRunParameter.WithInputData> {
@@ -36,8 +38,16 @@ internal class LocalTestRunner(private val testRun: TestRun) : AbstractLocalProc
override fun buildResultHandler(runResult: RunResult) = ResultHandler(runResult) override fun buildResultHandler(runResult: RunResult) = ResultHandler(runResult)
override fun handleUnexpectedFailure(t: Throwable) = fail {
buildString {
appendLine("Test execution failed with unexpected exception.")
appendLine()
appendLine(LoggedData.TestRunUnexpectedFailure(loggedParameters, t))
}
}
inner class ResultHandler(runResult: RunResult) : AbstractLocalProcessRunner<Unit>.ResultHandler(runResult) { inner class ResultHandler(runResult: RunResult) : AbstractLocalProcessRunner<Unit>.ResultHandler(runResult) {
override fun getLoggedRun() = LoggedData.TestRun(getLoggedParameters(), exitCode, stdOut, stdErr, durationMillis) override fun getLoggedRun() = LoggedData.TestRun(loggedParameters, exitCode, stdOut, stdErr, durationMillis)
override fun doHandle() { override fun doHandle() {
if (testRun.runParameters.has<TestRunParameter.WithGTestLogger>()) { if (testRun.runParameters.has<TestRunParameter.WithGTestLogger>()) {