diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/AutoMute.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/test/AutoMute.kt index 04d8587016a..7927354334f 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/AutoMute.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/AutoMute.kt @@ -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 = mutableListOf() + 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, diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/muteWithDatabase.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/test/muteWithDatabase.kt index dfcfd26d7b2..b7a08e1743a 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/muteWithDatabase.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/muteWithDatabase.kt @@ -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() } diff --git a/compiler/tests-mutes/src/org/jetbrains/kotlin/test/mutes/AutoMute.kt b/compiler/tests-mutes/src/org/jetbrains/kotlin/test/mutes/AutoMute.kt new file mode 100644 index 00000000000..8631f3a2adc --- /dev/null +++ b/compiler/tests-mutes/src/org/jetbrains/kotlin/test/mutes/AutoMute.kt @@ -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 = mutableListOf() + 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 + } +} diff --git a/compiler/tests-mutes/src/org/jetbrains/kotlin/test/mutes/muteWithDatabaseWrapper.kt b/compiler/tests-mutes/src/org/jetbrains/kotlin/test/mutes/muteWithDatabaseWrapper.kt new file mode 100644 index 00000000000..43b9f5a5af6 --- /dev/null +++ b/compiler/tests-mutes/src/org/jetbrains/kotlin/test/mutes/muteWithDatabaseWrapper.kt @@ -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") + } +}