Skip to content

Commit

Permalink
[Gradle, JS] Parse karma output to find problem with browser launch
Browse files Browse the repository at this point in the history
#KT-38109 fixed
  • Loading branch information
ilgonmic committed Apr 16, 2020
1 parent 831fbba commit 0534773
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.gradle.api.tasks.testing.TestOutputEvent.Destination.StdOut
import org.gradle.api.tasks.testing.TestResult
import org.gradle.api.tasks.testing.TestResult.ResultType.*
import org.gradle.internal.operations.OperationIdentifier
import org.gradle.process.internal.ExecHandle
import org.jetbrains.kotlin.gradle.logging.kotlinDebug
import org.jetbrains.kotlin.gradle.testing.KotlinTestFailure
import org.slf4j.Logger
Expand Down Expand Up @@ -51,6 +52,9 @@ internal open class TCServiceMessagesClient(
log.error("Failed to parse test process messages: \"$text\"", e)
}

internal open fun testFailedMessage(execHandle: ExecHandle, exitValue: Int): String =
"$execHandle exited with errors (exit code: $exitValue)"

override fun serviceMessage(message: ServiceMessage) {

// If a user uses TeamCity, this log may be treated by TC as an actual service message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TCServiceMessagesTestExecutor(
val runListeners: MutableList<KotlinTestRunnerListener>,
val ignoreRunFailures: Boolean
) : TestExecuter<TCServiceMessagesTestExecutionSpec> {
var execHandle: ExecHandle? = null
lateinit var execHandle: ExecHandle
var outputReaderThread: Thread? = null
var shouldStop = false

Expand All @@ -51,12 +51,12 @@ class TCServiceMessagesTestExecutor(

lateinit var result: ExecResult
client.root(rootOperation) {
execHandle!!.start()
result = execHandle!!.waitForFinish()
execHandle.start()
result = execHandle.waitForFinish()
}

if (spec.checkExitCode && result.exitValue != 0) {
error("$execHandle exited with errors (exit code: ${result.exitValue})")
error(client.testFailedMessage(execHandle, result.exitValue))
}
} catch (e: Throwable) {
spec.showSuppressedOutput()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.gradle.api.Project
import org.gradle.api.internal.tasks.testing.TestResultProcessor
import org.gradle.internal.logging.progress.ProgressLogger
import org.gradle.process.ProcessForkOptions
import org.gradle.process.internal.ExecHandle
import org.jetbrains.kotlin.gradle.internal.operation
import org.jetbrains.kotlin.gradle.internal.testing.TCServiceMessagesClientSettings
import org.jetbrains.kotlin.gradle.internal.testing.TCServiceMessagesTestExecutionSpec
Expand Down Expand Up @@ -409,13 +410,54 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) :
val baseTestNameSuffix get() = settings.testNameSuffix
override var testNameSuffix: String? = baseTestNameSuffix

private val errorBrowsers: MutableList<String> = mutableListOf()

override fun printNonTestOutput(text: String) {
val value = text.trimEnd()
progressLogger.progress(value)

parseConsole(value)

super.printNonTestOutput(text)
}

private fun parseConsole(text: String) {
if (KARMA_PROBLEM.matches(text)) {
config.browsers
.filter { it in text }
.filterNot { it in errorBrowsers }
.also {
errorBrowsers.addAll(it)
}
}
}

override fun testFailedMessage(execHandle: ExecHandle, exitValue: Int): String {
if (errorBrowsers.isEmpty()) {
return super.testFailedMessage(execHandle, exitValue)
}

val failedBrowsers = errorBrowsers
.joinToString("\n") {
"- $it"
}
return """
|Errors occurred during launch of browser for testing.
|$failedBrowsers
|Please make sure that you have installed browsers.
|Or change it via
|browser {
| testTask {
| useKarma {
| useFirefox()
| useChrome()
| useSafari()
| }
| }
|}
""".trimMargin()
}

override fun processStackTrace(stackTrace: String): String =
processKarmaStackTrace(stackTrace)

Expand Down Expand Up @@ -458,3 +500,5 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) :
appendln()
}
}

private val KARMA_PROBLEM = "(?m)^.*\\d{2} \\d{2} \\d{4,} \\d{2}:\\d{2}:\\d{2}.\\d{3}:(ERROR|WARN) \\[.*]: (.*)\$".toRegex()

0 comments on commit 0534773

Please sign in to comment.