From 42f9442728f023ced1512cf5cfe4f8d1305c760b Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 22 Jan 2021 12:34:56 +0300 Subject: [PATCH] [Test] Handle any Throwable from test instead of AssertionError --- .../tests/org/jetbrains/kotlin/test/Assertions.kt | 2 +- .../jetbrains/kotlin/test/ExceptionFromTestError.kt | 3 +++ .../tests/org/jetbrains/kotlin/test/TestRunner.kt | 7 ++++--- .../kotlin/test/model/AfterAnalysisChecker.kt | 4 ++-- .../kotlin/test/backend/BlackBoxCodegenSuppressor.kt | 10 +++++----- .../backend/handlers/FirIrDumpIdenticalCheckers.kt | 2 +- .../classic/handlers/FirTestDataConsistencyHandler.kt | 2 +- .../test/frontend/fir/FirFailingTestSuppressor.kt | 2 +- .../test/frontend/fir/handlers/FirIdenticalChecker.kt | 2 +- .../jetbrains/kotlin/test/services/JUnit5Assertions.kt | 2 +- .../org/jetbrains/kotlin/test/util/JUnit4Assertions.kt | 2 +- 11 files changed, 21 insertions(+), 17 deletions(-) diff --git a/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/test/Assertions.kt b/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/test/Assertions.kt index 28b58934b85..349f47219e6 100644 --- a/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/test/Assertions.kt +++ b/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/test/Assertions.kt @@ -44,7 +44,7 @@ abstract class Assertions { return collection.joinToString("\n") } - abstract fun assertAll(exceptions: List) + abstract fun assertAll(exceptions: List) abstract fun fail(message: () -> String): Nothing } diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/ExceptionFromTestError.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/ExceptionFromTestError.kt index 91b2f7330b5..5e0a80e446b 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/ExceptionFromTestError.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/ExceptionFromTestError.kt @@ -8,4 +8,7 @@ package org.jetbrains.kotlin.test class ExceptionFromTestError(cause: Throwable) : AssertionError(cause) { override val message: String get() = "Exception was thrown" + + override val cause: Throwable + get() = super.cause!! } diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestRunner.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestRunner.kt index 7a1332471ff..7c60db6d9b8 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestRunner.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestRunner.kt @@ -12,7 +12,7 @@ import org.jetbrains.kotlin.test.services.* import java.io.IOException class TestRunner(private val testConfiguration: TestConfiguration) { - private val failedAssertions = mutableListOf() + private val failedAssertions = mutableListOf() fun runTest(@TestDataFile testDataFileName: String) { try { @@ -79,9 +79,10 @@ class TestRunner(private val testConfiguration: TestConfiguration) { } val filteredFailedAssertions = testConfiguration.afterAnalysisCheckers - .fold>(failedAssertions) { assertions, checker -> + .fold>(failedAssertions) { assertions, checker -> checker.suppressIfNeeded(assertions) } + .map { if (it is ExceptionFromTestError) it.cause else it } services.assertions.assertAll(filteredFailedAssertions) } @@ -134,7 +135,7 @@ class TestRunner(private val testConfiguration: TestConfiguration) { private inline fun withAssertionCatching(insertExceptionInStart: Boolean = false, block: () -> Unit) { try { block() - } catch (e: AssertionError) { + } catch (e: Throwable) { if (insertExceptionInStart) { failedAssertions.add(0, e) } else { diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/model/AfterAnalysisChecker.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/model/AfterAnalysisChecker.kt index 52ec8c4e69d..8cca687e6e5 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/model/AfterAnalysisChecker.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/model/AfterAnalysisChecker.kt @@ -12,7 +12,7 @@ abstract class AfterAnalysisChecker(protected val testServices: TestServices) { open val directives: List get() = emptyList() - open fun check(failedAssertions: List) {} + open fun check(failedAssertions: List) {} - open fun suppressIfNeeded(failedAssertions: List): List = failedAssertions + open fun suppressIfNeeded(failedAssertions: List): List = failedAssertions } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/BlackBoxCodegenSuppressor.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/BlackBoxCodegenSuppressor.kt index 33be898c94d..8f6388c5efd 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/BlackBoxCodegenSuppressor.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/BlackBoxCodegenSuppressor.kt @@ -22,7 +22,7 @@ class BlackBoxCodegenSuppressor(testServices: TestServices) : AfterAnalysisCheck override val directives: List get() = listOf(CodegenTestDirectives) - override fun suppressIfNeeded(failedAssertions: List): List { + override fun suppressIfNeeded(failedAssertions: List): List { val moduleStructure = testServices.moduleStructure val targetBackends = moduleStructure.modules.mapNotNull { it.targetBackend } return when (moduleStructure.modules.map { it.frontendKind }.first()) { @@ -36,8 +36,8 @@ class BlackBoxCodegenSuppressor(testServices: TestServices) : AfterAnalysisCheck moduleStructure: TestModuleStructure, directive: ValueDirective, targetBackends: List, - failedAssertions: List - ): List { + failedAssertions: List + ): List { val ignoredBackends = moduleStructure.allDirectives[directive] if (ignoredBackends.isEmpty()) return failedAssertions val matchedBackend = ignoredBackends.intersect(targetBackends) @@ -52,10 +52,10 @@ class BlackBoxCodegenSuppressor(testServices: TestServices) : AfterAnalysisCheck private fun processAssertions( - failedAssertions: List, + failedAssertions: List, directive: ValueDirective, additionalMessage: String = "" - ): List { + ): List { return if (failedAssertions.isNotEmpty()) emptyList() else { val message = buildString { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/FirIrDumpIdenticalCheckers.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/FirIrDumpIdenticalCheckers.kt index 23e719d6ca7..89a93d9c695 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/FirIrDumpIdenticalCheckers.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/FirIrDumpIdenticalCheckers.kt @@ -39,7 +39,7 @@ class FirIrDumpIdenticalChecker(testServices: TestServices) : AfterAnalysisCheck } } - override fun check(failedAssertions: List) { + override fun check(failedAssertions: List) { if (failedAssertions.isNotEmpty()) return val testDataFile = testServices.moduleStructure.originalTestDataFiles.first() if (FIR_IDENTICAL in testServices.moduleStructure.allDirectives) { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/FirTestDataConsistencyHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/FirTestDataConsistencyHandler.kt index fc1628bf56a..7c85ad66ac0 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/FirTestDataConsistencyHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/FirTestDataConsistencyHandler.kt @@ -21,7 +21,7 @@ class FirTestDataConsistencyHandler(testServices: TestServices) : AfterAnalysisC override val directives: List get() = listOf(FirDiagnosticsDirectives) - override fun check(failedAssertions: List) { + override fun check(failedAssertions: List) { val moduleStructure = testServices.moduleStructure val testData = moduleStructure.originalTestDataFiles.first() if (testData.extension == "kts") return diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/FirFailingTestSuppressor.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/FirFailingTestSuppressor.kt index dc8b4b787f0..1eafe5222a1 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/FirFailingTestSuppressor.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/FirFailingTestSuppressor.kt @@ -11,7 +11,7 @@ import org.jetbrains.kotlin.test.services.TestServices import org.jetbrains.kotlin.test.services.moduleStructure class FirFailingTestSuppressor(testServices: TestServices) : AfterAnalysisChecker(testServices) { - override fun suppressIfNeeded(failedAssertions: List): List { + override fun suppressIfNeeded(failedAssertions: List): List { val testFile = testServices.moduleStructure.originalTestDataFiles.first() val failFile = testFile.parentFile.resolve("${testFile.nameWithoutExtension}.fail") val exceptionFromFir = failedAssertions.firstOrNull { it is ExceptionFromTestError } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/handlers/FirIdenticalChecker.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/handlers/FirIdenticalChecker.kt index d080ddbef6f..b3008bcdf96 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/handlers/FirIdenticalChecker.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/handlers/FirIdenticalChecker.kt @@ -25,7 +25,7 @@ class FirIdenticalChecker(testServices: TestServices) : AfterAnalysisChecker(tes } } - override fun check(failedAssertions: List) { + override fun check(failedAssertions: List) { if (failedAssertions.isNotEmpty()) return val testDataFile = testServices.moduleStructure.originalTestDataFiles.first() if (testDataFile.isFirTestData) { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/JUnit5Assertions.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/JUnit5Assertions.kt index 3596f788b2a..2b9b75dc273 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/JUnit5Assertions.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/JUnit5Assertions.kt @@ -51,7 +51,7 @@ object JUnit5Assertions : AssertionsService() { JUnit5PlatformAssertions.assertFalse(value, message?.invoke()) } - override fun assertAll(exceptions: List) { + override fun assertAll(exceptions: List) { exceptions.singleOrNull()?.let { throw it } JUnit5PlatformAssertions.assertAll(exceptions.map { Executable { throw it } }) } diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/JUnit4Assertions.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/JUnit4Assertions.kt index 921e830fe6e..ab6845daf1a 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/JUnit4Assertions.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/JUnit4Assertions.kt @@ -40,7 +40,7 @@ object JUnit4Assertions : Assertions() { KtUsefulTestCase.assertSameElements(message?.invoke() ?: "", expected, actual) } - override fun assertAll(exceptions: List) { + override fun assertAll(exceptions: List) { exceptions.forEach { throw it } }