Use only custom karma reporter
- Ideally these changes should be on karma-teamcity-reporter
This commit is contained in:
-73
@@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.kotlin.gradle.targets.js.testing.karma
|
|
||||||
|
|
||||||
class KarmaBrowserLogParser {
|
|
||||||
private var overflowConsole: Boolean = false
|
|
||||||
private var previousType: BrowserLogType? = null
|
|
||||||
|
|
||||||
fun parseKarmaBrowserLog(text: String): BrowserLog? {
|
|
||||||
val logStartIndex = text.indexOf("$BROWSER_LOG_SURROUNDER[")
|
|
||||||
if (logStartIndex == -1 && !overflowConsole) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
var browser: String? = null
|
|
||||||
var type: String? = null
|
|
||||||
var log: String = text
|
|
||||||
|
|
||||||
if (logStartIndex != -1 && !overflowConsole) {
|
|
||||||
val afterPrefix = text.substring(logStartIndex)
|
|
||||||
browser = afterPrefix.substringAfter("browser='").substringBefore("'")
|
|
||||||
type = afterPrefix.substringAfter("type='").substringBefore("'")
|
|
||||||
|
|
||||||
log = afterPrefix.substringAfter("log=")
|
|
||||||
}
|
|
||||||
|
|
||||||
val logEndIndex = text.indexOf("]$BROWSER_LOG_SURROUNDER")
|
|
||||||
if (logEndIndex != -1) {
|
|
||||||
overflowConsole = false
|
|
||||||
log = log.substringBefore("]$BROWSER_LOG_SURROUNDER")
|
|
||||||
} else {
|
|
||||||
overflowConsole = true
|
|
||||||
}
|
|
||||||
|
|
||||||
val currentType = when (type) {
|
|
||||||
"log" -> BrowserLogType.LOG
|
|
||||||
"info" -> BrowserLogType.INFO
|
|
||||||
"warn" -> BrowserLogType.WARN
|
|
||||||
"error" -> BrowserLogType.ERROR
|
|
||||||
"debug" -> BrowserLogType.DEBUG
|
|
||||||
else -> previousType
|
|
||||||
}
|
|
||||||
|
|
||||||
previousType = currentType
|
|
||||||
|
|
||||||
return BrowserLog(
|
|
||||||
browser = browser,
|
|
||||||
type = currentType,
|
|
||||||
log = log
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val BROWSER_LOG_SURROUNDER = "##browser"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class BrowserLog(
|
|
||||||
val browser: String?,
|
|
||||||
val type: BrowserLogType?,
|
|
||||||
val log: String
|
|
||||||
)
|
|
||||||
|
|
||||||
enum class BrowserLogType(val value: String) {
|
|
||||||
LOG("log"),
|
|
||||||
INFO("info"),
|
|
||||||
WARN("warn"),
|
|
||||||
ERROR("error"),
|
|
||||||
DEBUG("debug")
|
|
||||||
}
|
|
||||||
+79
-55
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
|
|||||||
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
|
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTestFramework
|
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTestFramework
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.testing.karma.KarmaBrowserLogParser.Companion.BROWSER_LOG_SURROUNDER
|
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
|
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
|
||||||
import org.jetbrains.kotlin.gradle.testing.internal.reportsDir
|
import org.jetbrains.kotlin.gradle.testing.internal.reportsDir
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
@@ -54,7 +53,6 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) : KotlinJsTestF
|
|||||||
requiredDependencies.add(versions.karma)
|
requiredDependencies.add(versions.karma)
|
||||||
|
|
||||||
useLogReporter()
|
useLogReporter()
|
||||||
useTeamcityReporter()
|
|
||||||
useMocha()
|
useMocha()
|
||||||
useWebpack()
|
useWebpack()
|
||||||
useSourceMapSupport()
|
useSourceMapSupport()
|
||||||
@@ -63,12 +61,8 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) : KotlinJsTestF
|
|||||||
config.autoWatch = false
|
config.autoWatch = false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun useTeamcityReporter() {
|
|
||||||
requiredDependencies.add(versions.karmaTeamcityReporter)
|
|
||||||
config.reporters.add("teamcity")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun useLogReporter() {
|
private fun useLogReporter() {
|
||||||
|
requiredDependencies.add(versions.karmaTeamcityReporter)
|
||||||
config.reporters.add("karma-browser-log-reporter")
|
config.reporters.add("karma-browser-log-reporter")
|
||||||
|
|
||||||
confJsWriters.add {
|
confJsWriters.add {
|
||||||
@@ -76,27 +70,86 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) : KotlinJsTestF
|
|||||||
it.appendln(
|
it.appendln(
|
||||||
"""
|
"""
|
||||||
// reporter for browser logs
|
// reporter for browser logs
|
||||||
const LogReporter = function (baseReporterDecorator) {
|
(function () {
|
||||||
baseReporterDecorator(this);
|
const util = require('util');
|
||||||
|
|
||||||
this.onBrowserLog = (browser, log, type) => {
|
const escapeMessage = function (message) {
|
||||||
this.write(
|
if (message === null || message === undefined) {
|
||||||
"$BROWSER_LOG_SURROUNDER[" +
|
return ''
|
||||||
"browser='" + browser + "' " +
|
}
|
||||||
"type='" + type + "' " +
|
|
||||||
"log=" + log +
|
|
||||||
"]$BROWSER_LOG_SURROUNDER\n"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
LogReporter.${'$'}inject = ['baseReporterDecorator'];
|
return message.toString()
|
||||||
|
.replace(/\|/g, '||')
|
||||||
config.plugins = config.plugins || [];
|
.replace(/'/g, "|'")
|
||||||
config.plugins.push('karma-*'); // default
|
.replace(/\n/g, '|n')
|
||||||
config.plugins.push({
|
.replace(/\r/g, '|r')
|
||||||
'reporter:karma-browser-log-reporter': ['type', LogReporter]
|
.replace(/\u0085/g, '|x')
|
||||||
});
|
.replace(/\u2028/g, '|l')
|
||||||
|
.replace(/\u2029/g, '|p')
|
||||||
|
.replace(/\[/g, '|[')
|
||||||
|
.replace(/\]/g, '|]')
|
||||||
|
};
|
||||||
|
|
||||||
|
var formatMessage = function () {
|
||||||
|
var args = Array.prototype.slice.call(arguments);
|
||||||
|
|
||||||
|
for (var i = args.length - 1; i > 0; i--) {
|
||||||
|
args[i] = escapeMessage(args[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return util.format.apply(null, args) + '\n'
|
||||||
|
};
|
||||||
|
|
||||||
|
const LogReporter = function (baseReporterDecorator) {
|
||||||
|
const teamcityReporter = require("karma-teamcity-reporter")["reporter:teamcity"][1];
|
||||||
|
teamcityReporter.call(this, baseReporterDecorator);
|
||||||
|
|
||||||
|
this.logBuffer = [];
|
||||||
|
|
||||||
|
this.TEST_STD_OUT = "##teamcity[testStdOut name='%name%' out='%s' flowId='']";
|
||||||
|
|
||||||
|
this.onBrowserLog = (browser, log, type) => {
|
||||||
|
this.logBuffer.push(formatMessage(this.TEST_STD_OUT, `[${"$"}{type}] ${"$"}{log}\n`))
|
||||||
|
};
|
||||||
|
|
||||||
|
this.specSuccess = (browser, result) => {
|
||||||
|
const log = this.getLog(browser, result);
|
||||||
|
const testName = result.description;
|
||||||
|
|
||||||
|
log.push(formatMessage(this.TEST_START, testName));
|
||||||
|
this.logBuffer.forEach(item => {
|
||||||
|
log.push(
|
||||||
|
item.replace('%name%', result.description)
|
||||||
|
)
|
||||||
|
});
|
||||||
|
log.push(formatMessage(this.TEST_END, testName, result.time))
|
||||||
|
};
|
||||||
|
|
||||||
|
this.specFailure = function (browser, result) {
|
||||||
|
const log = this.getLog(browser, result);
|
||||||
|
const testName = result.description;
|
||||||
|
|
||||||
|
log.push(formatMessage(this.TEST_START, testName));
|
||||||
|
this.logBuffer.forEach(item => {
|
||||||
|
log.push(
|
||||||
|
item.replace('%name%', result.description)
|
||||||
|
)
|
||||||
|
});
|
||||||
|
log.push(formatMessage(this.TEST_FAILED, testName, result.log.join('\n\n')));
|
||||||
|
log.push(formatMessage(this.TEST_END, testName, result.time));
|
||||||
|
|
||||||
|
this.logBuffer = [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
LogReporter.${"$"}inject = ['baseReporterDecorator'];
|
||||||
|
|
||||||
|
config.plugins = config.plugins || [];
|
||||||
|
config.plugins.push('karma-*'); // default
|
||||||
|
config.plugins.push({
|
||||||
|
'reporter:karma-browser-log-reporter': ['type', LogReporter]
|
||||||
|
});
|
||||||
|
})();
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -345,8 +398,6 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) : KotlinJsTestF
|
|||||||
val baseTestNameSuffix get() = settings.testNameSuffix
|
val baseTestNameSuffix get() = settings.testNameSuffix
|
||||||
override var testNameSuffix: String? = baseTestNameSuffix
|
override var testNameSuffix: String? = baseTestNameSuffix
|
||||||
|
|
||||||
val browserLogParser = KarmaBrowserLogParser()
|
|
||||||
|
|
||||||
override fun printNonTestOutput(text: String) {
|
override fun printNonTestOutput(text: String) {
|
||||||
val value = text.trimEnd()
|
val value = text.trimEnd()
|
||||||
progressLogger.progress(value)
|
progressLogger.progress(value)
|
||||||
@@ -388,33 +439,6 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) : KotlinJsTestF
|
|||||||
}
|
}
|
||||||
|
|
||||||
suppressedOutput.appendln(text)
|
suppressedOutput.appendln(text)
|
||||||
|
|
||||||
val browserLog = browserLogParser.parseKarmaBrowserLog(text) ?: return
|
|
||||||
|
|
||||||
val formattedMessage = formatMessage(browserLog)
|
|
||||||
|
|
||||||
when (browserLog.type) {
|
|
||||||
BrowserLogType.LOG -> println(formattedMessage)
|
|
||||||
BrowserLogType.WARN -> log.warn(formattedMessage)
|
|
||||||
BrowserLogType.ERROR -> log.error(formattedMessage)
|
|
||||||
BrowserLogType.DEBUG -> log.debug(formattedMessage)
|
|
||||||
else -> log.info(formattedMessage)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun formatMessage(browserLog: BrowserLog): String {
|
|
||||||
val browser = browserLog.browser
|
|
||||||
val browserPart = browser?.plus(" ") ?: ""
|
|
||||||
|
|
||||||
val type = browserLog.type
|
|
||||||
val typePart = if (browser != null && type != null) {
|
|
||||||
type
|
|
||||||
.value
|
|
||||||
.toUpperCase()
|
|
||||||
.plus(" ")
|
|
||||||
} else ""
|
|
||||||
|
|
||||||
return "$browserPart$typePart${browserLog.log}"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user