From f3e5b8c67fae66615022bcbf8a6b9fa95cdf1108 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 18 Oct 2019 16:44:33 +0300 Subject: [PATCH] Allow to setup additional extension for mute with annotation --- .../kotlin/test/KotlinTestUtils.java | 13 +++-- .../org/jetbrains/kotlin/test/muteWithFile.kt | 53 +++++++++++++------ 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java index 16897dc336b..8b849fe8e02 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -709,7 +709,7 @@ public class KotlinTestUtils { @SuppressWarnings("unused") public static void runTest(@NotNull DoTest test, @NotNull TestCase testCase, @TestDataFile String testDataFile) throws Exception { - runTest(test, TargetBackend.ANY, testDataFile); + runTestImpl(testWithCustomIgnoreDirective(test, TargetBackend.ANY, IGNORE_BACKEND_DIRECTIVE_PREFIX), testCase, testDataFile); } // In this test runner version the `testDataFile` parameter is annotated by `TestDataFile`. @@ -719,7 +719,7 @@ public class KotlinTestUtils { } public static void runTestWithCustomIgnoreDirective(DoTest test, TargetBackend targetBackend, @TestDataFile String testDataFile, String ignoreDirective) throws Exception { - runTest(testWithCustomIgnoreDirective(test, targetBackend, ignoreDirective), testDataFile); + runTestImpl(testWithCustomIgnoreDirective(test, targetBackend, ignoreDirective), null, testDataFile); } // In this test runner version, NONE of the parameters are annotated by `TestDataFile`. @@ -731,11 +731,14 @@ public class KotlinTestUtils { // * sometimes, for too common/general names, it shows many variants to navigate // * it adds an additional step for navigation -- you must choose an exact file to navigate public static void runTest0(DoTest test, TargetBackend targetBackend, String testDataFilePath) throws Exception { - runTest(testWithCustomIgnoreDirective(test, targetBackend, IGNORE_BACKEND_DIRECTIVE_PREFIX), testDataFilePath); + runTestImpl(testWithCustomIgnoreDirective(test, targetBackend, IGNORE_BACKEND_DIRECTIVE_PREFIX), null, testDataFilePath); } - private static void runTest(DoTest test, String testDataFilePath) throws Exception { - MuteWithFileKt.testWithMuteInFile(test).invoke(testDataFilePath); + private static void runTestImpl(@NotNull DoTest test, @Nullable TestCase testCase, String testDataFilePath) throws Exception { + DoTest wrappedTest = testCase != null ? + MuteWithFileKt.testWithMuteInFile(test, testCase) : + MuteWithFileKt.testWithMuteInFile(test, ""); + wrappedTest.invoke(testDataFilePath); } private static DoTest testWithCustomIgnoreDirective(DoTest test, TargetBackend targetBackend, String ignoreDirective) throws Exception { diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/muteWithFile.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/test/muteWithFile.kt index 94bdb984083..f981a2e68ec 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/muteWithFile.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/muteWithFile.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.test +import junit.framework.TestCase import org.jetbrains.kotlin.test.KotlinTestUtils.DoTest import org.junit.Assert import java.io.File @@ -12,30 +13,38 @@ import java.io.File private val RUN_MUTED_TESTS = java.lang.Boolean.getBoolean("org.jetbrains.kotlin.run.muted.tests") private val AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT: String? = null +annotation class MuteExtraSuffix(val value: String = "") + @Throws(Exception::class) -fun testWithMuteInFile(test: DoTest): DoTest { +fun testWithMuteInFile(test: DoTest, testCase: TestCase): DoTest { + val extraSuffix = testCase.javaClass.getAnnotation(MuteExtraSuffix::class.java)?.value ?: "" + return testWithMuteInFile(test, extraSuffix) +} + +@Throws(Exception::class) +fun testWithMuteInFile(test: DoTest, extraSuffix: String): DoTest { return object : DoTest { override fun invoke(filePath: String) { val testDataFile = File(filePath) - val isMutedWithFile = isMutedWithFile(testDataFile) + val isMutedWithFile = isMutedWithFile(testDataFile, extraSuffix) if (isMutedWithFile && !RUN_MUTED_TESTS) { System.err.println("IGNORED TEST: $filePath") return } - val failFile = failFile(testDataFile) + val failFile = failFile(testDataFile, extraSuffix) val hasFailFile = failFile != null try { test.invoke(filePath) } catch (e: Throwable) { - if (checkFailFile(e, testDataFile)) { + if (checkFailFile(e, testDataFile, extraSuffix)) { return } if (!isMutedWithFile && !hasFailFile && AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT != null) { - createMuteFile(testDataFile, AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT) + createMuteFile(testDataFile, extraSuffix, AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT) } throw e } @@ -45,24 +54,38 @@ fun testWithMuteInFile(test: DoTest): DoTest { } } -private fun isMutedWithFile(testDataFile: File): Boolean { - if (!testDataFile.isFile) { +private fun isMutedWithFile(testPathFile: File, extraSuffix: String): Boolean { + if (!testPathFile.isFile) { return false } - val muteFile = File("${testDataFile.path}.mute") - return muteFile.exists() && muteFile.isFile + + return muteFile(testPathFile, extraSuffix) != null } -private fun createMuteFile(testDataFile: File, text: String) { +private fun createMuteFile(testDataFile: File, extraSuffix: String, text: String) { require(text.isNotEmpty()) { "Mute text must not be empty" } - File("${testDataFile.path}.mute").writeText(text) + muteFileNoCheck(testDataFile, extraSuffix).writeText(text) } -private fun failFile(testDataFile: File): File? { +private fun muteFileNoCheck(testPathFile: File, extraSuffix: String): File { + return File("${testPathFile.path}$extraSuffix.mute") +} + +private fun muteFile(testPathFile: File, extraSuffix: String): File? { + val muteFile = muteFileNoCheck(testPathFile, extraSuffix) + + if (muteFile.exists() && muteFile.isFile) { + return muteFile + } + + return null +} + +private fun failFile(testDataFile: File, extraSuffix: String): File? { if (!testDataFile.isFile) return null - val failFile = File("${testDataFile.path}.fail") + val failFile = File("${testDataFile.path}$extraSuffix.fail") if (!failFile.exists() || !failFile.isFile) { return null } @@ -70,8 +93,8 @@ private fun failFile(testDataFile: File): File? { return failFile } -private fun checkFailFile(failure: Throwable, testDataFile: File): Boolean { - val failFile = failFile(testDataFile) ?: return false +private fun checkFailFile(failure: Throwable, testDataFile: File, extraSuffix: String): Boolean { + val failFile = failFile(testDataFile, extraSuffix) ?: return false val cause = failure.cause val muteMessage = failure.message + if (cause != null) {