mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-20 08:31:28 +00:00
[TEST] Move auto mute wrapping utils to :compiler:tests-mutes
This commit is contained in:
@@ -5,55 +5,13 @@
|
||||
|
||||
package org.jetbrains.kotlin.test
|
||||
|
||||
import org.jetbrains.kotlin.test.mutes.DO_AUTO_MUTE
|
||||
import org.jetbrains.kotlin.test.mutes.muteTest
|
||||
import org.junit.runner.notification.Failure
|
||||
import org.junit.runner.notification.RunListener
|
||||
import org.junit.runner.notification.RunNotifier
|
||||
import java.io.File
|
||||
|
||||
class AutoMute(
|
||||
val file: String,
|
||||
val issue: String
|
||||
)
|
||||
|
||||
val DO_AUTO_MUTE: AutoMute? by lazy {
|
||||
val autoMuteFile = File("tests/automute")
|
||||
if (autoMuteFile.exists()) {
|
||||
val lines = autoMuteFile.readLines().filter { it.isNotBlank() }.map { it.trim() }
|
||||
AutoMute(
|
||||
lines.getOrNull(0) ?: error("A file path is expected in tne first line"),
|
||||
lines.getOrNull(1) ?: error("An issue description is the second line")
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun AutoMute.muteTest(testKey: String) {
|
||||
val file = File(file)
|
||||
val lines = file.readLines()
|
||||
val firstLine = lines[0] // Drop file header
|
||||
val muted = lines.drop(1).toMutableList()
|
||||
muted.add("$testKey, $issue")
|
||||
val newMuted: List<String> = mutableListOf<String>() + firstLine + muted.sorted()
|
||||
file.writeText(newMuted.joinToString("\n"))
|
||||
}
|
||||
|
||||
internal fun wrapWithAutoMute(f: () -> Unit, testKey: String): (() -> Unit)? {
|
||||
val doAutoMute = DO_AUTO_MUTE
|
||||
if (doAutoMute != null) {
|
||||
return {
|
||||
try {
|
||||
f()
|
||||
} catch (e: Throwable) {
|
||||
doAutoMute.muteTest(testKey)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
internal inline fun RunNotifier.withAutoMuteListener(
|
||||
testKey: String,
|
||||
crossinline run: () -> Unit,
|
||||
|
||||
@@ -6,9 +6,7 @@
|
||||
package org.jetbrains.kotlin.test
|
||||
|
||||
import junit.framework.TestCase
|
||||
import org.jetbrains.kotlin.test.mutes.MutedTest
|
||||
import org.jetbrains.kotlin.test.mutes.getMutedTest
|
||||
import org.jetbrains.kotlin.test.mutes.mutedSet
|
||||
import org.jetbrains.kotlin.test.mutes.*
|
||||
import org.junit.internal.runners.statements.InvokeMethod
|
||||
import org.junit.runner.Runner
|
||||
import org.junit.runner.notification.RunNotifier
|
||||
@@ -19,55 +17,10 @@ import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters
|
||||
import org.junit.runners.parameterized.ParametersRunnerFactory
|
||||
import org.junit.runners.parameterized.TestWithParameters
|
||||
|
||||
private val SKIP_MUTED_TESTS = java.lang.Boolean.getBoolean("org.jetbrains.kotlin.skip.muted.tests")
|
||||
|
||||
private fun isMutedInDatabase(testClass: Class<*>, methodKey: String): Boolean {
|
||||
val mutedTest = mutedSet.mutedTest(testClass, methodKey)
|
||||
return SKIP_MUTED_TESTS && isPresentedInDatabaseWithoutFailMarker(mutedTest)
|
||||
}
|
||||
|
||||
private fun isMutedInDatabaseWithLog(testClass: Class<*>, methodKey: String): Boolean {
|
||||
val mutedInDatabase = isMutedInDatabase(testClass, methodKey)
|
||||
|
||||
if (mutedInDatabase) {
|
||||
System.err.println(mutedMessage(testClass, methodKey))
|
||||
}
|
||||
|
||||
return mutedInDatabase
|
||||
}
|
||||
|
||||
private fun isPresentedInDatabaseWithoutFailMarker(mutedTest: MutedTest?): Boolean {
|
||||
return mutedTest != null && !mutedTest.hasFailFile
|
||||
}
|
||||
|
||||
internal fun wrapWithMuteInDatabase(testCase: TestCase, f: () -> Unit): (() -> Unit)? {
|
||||
val testClass = testCase.javaClass
|
||||
val methodKey = testCase.name
|
||||
|
||||
val mutedTest = getMutedTest(testClass, methodKey)
|
||||
val testKey = testKey(testClass, methodKey)
|
||||
|
||||
if (isMutedInDatabase(testClass, methodKey)) {
|
||||
return {
|
||||
System.err.println(mutedMessage(testClass, methodKey))
|
||||
}
|
||||
} else if (isPresentedInDatabaseWithoutFailMarker(mutedTest)) {
|
||||
if (mutedTest?.isFlaky == true) {
|
||||
return f
|
||||
} else {
|
||||
return {
|
||||
invertMutedTestResultWithLog(f, testKey)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return wrapWithAutoMute(f, testKey)
|
||||
}
|
||||
return wrapWithMuteInDatabase(testCase.javaClass, testCase.name, f)
|
||||
}
|
||||
|
||||
private fun mutedMessage(klass: Class<*>, methodKey: String) = "MUTED TEST: ${testKey(klass, methodKey)}"
|
||||
|
||||
private fun testKey(klass: Class<*>, methodKey: String) = "${klass.canonicalName}.$methodKey"
|
||||
|
||||
class RunnerFactoryWithMuteInDatabase : ParametersRunnerFactory {
|
||||
override fun createRunnerForTestWithParameters(testWithParameters: TestWithParameters?): Runner {
|
||||
return object : BlockJUnit4ClassRunnerWithParameters(testWithParameters) {
|
||||
@@ -95,7 +48,11 @@ class RunnerFactoryWithMuteInDatabase : ParametersRunnerFactory {
|
||||
private fun parametrizedMethodKey(child: FrameworkMethod, parametersName: String) = "${child.method.name}$parametersName"
|
||||
}
|
||||
|
||||
class MethodInvokerWithMutedTests(val method: FrameworkMethod, val test: Any?, val mainMethodKey: String? = null) : InvokeMethod(method, test) {
|
||||
class MethodInvokerWithMutedTests(
|
||||
val method: FrameworkMethod,
|
||||
val test: Any?,
|
||||
val mainMethodKey: String? = null
|
||||
) : InvokeMethod(method, test) {
|
||||
override fun evaluate() {
|
||||
val methodClass = method.declaringClass
|
||||
val mutedTest =
|
||||
@@ -133,21 +90,6 @@ class RunnerWithMuteInDatabase(klass: Class<*>?) : BlockJUnit4ClassRunner(klass)
|
||||
}
|
||||
}
|
||||
|
||||
private fun invertMutedTestResultWithLog(f: () -> Unit, testKey: String) {
|
||||
var isTestGreen = true
|
||||
try {
|
||||
f()
|
||||
} catch (e: Throwable) {
|
||||
println("MUTED TEST STILL FAILS: $testKey")
|
||||
isTestGreen = false
|
||||
}
|
||||
|
||||
if (isTestGreen) {
|
||||
System.err.println("SUCCESS RESULT OF MUTED TEST: $testKey")
|
||||
throw Exception("Muted non-flaky test $testKey finished successfully. Please remove it from csv file")
|
||||
}
|
||||
}
|
||||
|
||||
fun TestCase.runTest(test: () -> Unit) {
|
||||
(wrapWithMuteInDatabase(this, test) ?: test).invoke()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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.test.mutes
|
||||
|
||||
import java.io.File
|
||||
|
||||
class AutoMute(
|
||||
val file: String,
|
||||
val issue: String
|
||||
)
|
||||
|
||||
val DO_AUTO_MUTE: AutoMute? by lazy {
|
||||
val autoMuteFile = File("tests/automute")
|
||||
if (autoMuteFile.exists()) {
|
||||
val lines = autoMuteFile.readLines().filter { it.isNotBlank() }.map { it.trim() }
|
||||
AutoMute(
|
||||
lines.getOrNull(0) ?: error("A file path is expected in tne first line"),
|
||||
lines.getOrNull(1) ?: error("An issue description is the second line")
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun AutoMute.muteTest(testKey: String) {
|
||||
val file = File(file)
|
||||
val lines = file.readLines()
|
||||
val firstLine = lines[0] // Drop file header
|
||||
val muted = lines.drop(1).toMutableList()
|
||||
muted.add("$testKey, $issue")
|
||||
val newMuted: List<String> = mutableListOf<String>() + firstLine + muted.sorted()
|
||||
file.writeText(newMuted.joinToString("\n"))
|
||||
}
|
||||
|
||||
internal fun wrapWithAutoMute(f: () -> Unit, testKey: String): (() -> Unit)? {
|
||||
val doAutoMute = DO_AUTO_MUTE
|
||||
if (doAutoMute != null) {
|
||||
return {
|
||||
try {
|
||||
f()
|
||||
} catch (e: Throwable) {
|
||||
doAutoMute.muteTest(testKey)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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.test.mutes
|
||||
|
||||
private val SKIP_MUTED_TESTS = java.lang.Boolean.getBoolean("org.jetbrains.kotlin.skip.muted.tests")
|
||||
|
||||
fun isMutedInDatabase(testClass: Class<*>, methodKey: String): Boolean {
|
||||
val mutedTest = mutedSet.mutedTest(testClass, methodKey)
|
||||
return SKIP_MUTED_TESTS && isPresentedInDatabaseWithoutFailMarker(mutedTest)
|
||||
}
|
||||
|
||||
fun isMutedInDatabaseWithLog(testClass: Class<*>, methodKey: String): Boolean {
|
||||
val mutedInDatabase = isMutedInDatabase(testClass, methodKey)
|
||||
|
||||
if (mutedInDatabase) {
|
||||
System.err.println(mutedMessage(testClass, methodKey))
|
||||
}
|
||||
|
||||
return mutedInDatabase
|
||||
}
|
||||
|
||||
fun isPresentedInDatabaseWithoutFailMarker(mutedTest: MutedTest?): Boolean {
|
||||
return mutedTest != null && !mutedTest.hasFailFile
|
||||
}
|
||||
|
||||
fun mutedMessage(klass: Class<*>, methodKey: String): String = "MUTED TEST: ${testKey(klass, methodKey)}"
|
||||
|
||||
fun testKey(klass: Class<*>, methodKey: String): String = "${klass.canonicalName}.$methodKey"
|
||||
|
||||
fun wrapWithMuteInDatabase(testClass: Class<*>, methodName: String, f: () -> Unit): (() -> Unit)? {
|
||||
val mutedTest = getMutedTest(testClass, methodName)
|
||||
val testKey = testKey(testClass, methodName)
|
||||
|
||||
if (isMutedInDatabase(testClass, methodName)) {
|
||||
return {
|
||||
System.err.println(mutedMessage(testClass, methodName))
|
||||
}
|
||||
} else if (isPresentedInDatabaseWithoutFailMarker(mutedTest)) {
|
||||
if (mutedTest?.isFlaky == true) {
|
||||
return f
|
||||
} else {
|
||||
return {
|
||||
invertMutedTestResultWithLog(f, testKey)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return wrapWithAutoMute(f, testKey)
|
||||
}
|
||||
}
|
||||
|
||||
fun invertMutedTestResultWithLog(f: () -> Unit, testKey: String) {
|
||||
var isTestGreen = true
|
||||
try {
|
||||
f()
|
||||
} catch (e: Throwable) {
|
||||
println("MUTED TEST STILL FAILS: $testKey")
|
||||
isTestGreen = false
|
||||
}
|
||||
|
||||
if (isTestGreen) {
|
||||
System.err.println("SUCCESS RESULT OF MUTED TEST: $testKey")
|
||||
throw Exception("Muted non-flaky test $testKey finished successfully. Please remove it from csv file")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user