Support auto-mute from file for database with mutes
This commit is contained in:
committed by
Nikolay Krasko
parent
9356155f60
commit
6797fb476c
@@ -740,6 +740,22 @@ public class KotlinTestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void runTestImpl(@NotNull DoTest test, @Nullable TestCase testCase, String testDataFilePath) throws Exception {
|
private static void runTestImpl(@NotNull DoTest test, @Nullable TestCase testCase, String testDataFilePath) throws Exception {
|
||||||
|
if (testCase != null) {
|
||||||
|
Function0<Unit> wrapWithMuteInDatabase = MuteWithDatabaseKt.wrapWithMuteInDatabase(testCase, () -> {
|
||||||
|
try {
|
||||||
|
test.invoke(testDataFilePath);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
if (wrapWithMuteInDatabase != null) {
|
||||||
|
wrapWithMuteInDatabase.invoke();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DoTest wrappedTest = testCase != null ?
|
DoTest wrappedTest = testCase != null ?
|
||||||
MuteWithFileKt.testWithMuteInFile(test, testCase) :
|
MuteWithFileKt.testWithMuteInFile(test, testCase) :
|
||||||
MuteWithFileKt.testWithMuteInFile(test, "");
|
MuteWithFileKt.testWithMuteInFile(test, "");
|
||||||
|
|||||||
@@ -8,9 +8,27 @@ package org.jetbrains.kotlin.test
|
|||||||
import junit.framework.TestCase
|
import junit.framework.TestCase
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
|
private class AutoMute(
|
||||||
|
val file: String,
|
||||||
|
val issue: String
|
||||||
|
)
|
||||||
|
|
||||||
|
private 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class MutedTest(
|
private class MutedTest(
|
||||||
val key: String,
|
val key: String,
|
||||||
val issue: String?,
|
@Suppress("unused") val issue: String?,
|
||||||
val hasFailFile: Boolean
|
val hasFailFile: Boolean
|
||||||
) {
|
) {
|
||||||
val methodName = key.substringAfterLast(".", "").replace("`", "").also {
|
val methodName = key.substringAfterLast(".", "").replace("`", "").also {
|
||||||
@@ -92,21 +110,36 @@ internal fun isMutedInDatabase(testCase: TestCase): Boolean {
|
|||||||
return mutedTest != null && !mutedTest.hasFailFile
|
return mutedTest != null && !mutedTest.hasFailFile
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun wrapWithMuteInDatabase(testCase: TestCase, f: () -> Unit): () -> Unit {
|
internal fun wrapWithMuteInDatabase(testCase: TestCase, f: () -> Unit): (() -> Unit)? {
|
||||||
if (!isMutedInDatabase(testCase)) return f
|
if (isMutedInDatabase(testCase)) {
|
||||||
return { MUTED_DO_TEST_LAMBDA(testCase) }
|
return { MUTED_DO_TEST_LAMBDA(testCase) }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal val MUTED_DO_TEST_LAMBDA = { testCase: TestCase ->
|
val doAutoMute = DO_AUTO_MUTE ?: return null
|
||||||
System.err.println("MUTED TEST: ${testCase::class.java.name}.${testCase.name}")
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class MutedDoTest(val testCase: TestCase) : KotlinTestUtils.DoTest {
|
return {
|
||||||
override fun invoke(filePath: String) {
|
try {
|
||||||
MUTED_DO_TEST_LAMBDA(testCase)
|
f()
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
val file = File(doAutoMute.file)
|
||||||
|
val lines = file.readLines()
|
||||||
|
val firstLine = lines[0]
|
||||||
|
val muted = lines.drop(1).toMutableList()
|
||||||
|
muted.add("${testKey(testCase)}, ${doAutoMute.issue}")
|
||||||
|
val newMuted: List<String> = mutableListOf<String>() + firstLine + muted.sorted()
|
||||||
|
file.writeText(newMuted.joinToString("\n"))
|
||||||
|
|
||||||
|
throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun TestCase.runTest(test: () -> Unit) {
|
internal val MUTED_DO_TEST_LAMBDA = { testCase: TestCase ->
|
||||||
wrapWithMuteInDatabase(this, test).invoke()
|
System.err.println("MUTED TEST: ${testKey(testCase)}")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun testKey(testCase: TestCase) = "${testCase::class.java.canonicalName}.${testCase.name}"
|
||||||
|
|
||||||
|
fun TestCase.runTest(test: () -> Unit) {
|
||||||
|
(wrapWithMuteInDatabase(this, test) ?: test).invoke()
|
||||||
}
|
}
|
||||||
@@ -18,10 +18,6 @@ annotation class MuteExtraSuffix(val value: String = "")
|
|||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun testWithMuteInFile(test: DoTest, testCase: TestCase): DoTest {
|
fun testWithMuteInFile(test: DoTest, testCase: TestCase): DoTest {
|
||||||
if (isMutedInDatabase(testCase)) {
|
|
||||||
return MutedDoTest(testCase)
|
|
||||||
}
|
|
||||||
|
|
||||||
val extraSuffix = testCase.javaClass.getAnnotation(MuteExtraSuffix::class.java)?.value ?: ""
|
val extraSuffix = testCase.javaClass.getAnnotation(MuteExtraSuffix::class.java)?.value ?: ""
|
||||||
return testWithMuteInFile(test, extraSuffix)
|
return testWithMuteInFile(test, extraSuffix)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user