Get rid of unsafe cast function usages in :native:native.tests

This commit is contained in:
Svyatoslav Scherbina
2022-10-07 12:26:08 +02:00
committed by Space Team
parent 51e337a148
commit 3e1eddcf54
8 changed files with 23 additions and 24 deletions
-1
View File
@@ -415,7 +415,6 @@ val projectsWithOptInToUnsafeCastFunctionsFromAddToStdLib by extra {
":kotlin-scripting-jvm-host-test",
":native:frontend.native",
":native:kotlin-klib-commonizer",
":native:native.tests",
":plugins:android-extensions-compiler",
":plugins:jvm-abi-gen",
":plugins:parcelize:parcelize-compiler:parcelize.k1",
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.konan.blackboxtest.support.runner.get
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.KotlinNativeHome
import org.jetbrains.kotlin.konan.blackboxtest.support.util.SafeEnvVars
import org.jetbrains.kotlin.konan.blackboxtest.support.util.SafeProperties
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.io.File
import kotlin.time.Duration
import kotlin.time.DurationUnit
@@ -64,7 +63,7 @@ internal abstract class LoggedData {
get() = buildList {
sourceModules.forEach { module ->
if (module !is TestModule.Exclusive) return@forEach
this += module.testCase.id.safeAs<TestCaseId.TestDataFile>()?.file ?: return@forEach
this += (module.testCase.id as? TestCaseId.TestDataFile)?.file ?: return@forEach
}
sort()
}
@@ -22,8 +22,6 @@ import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.test.TestMetadata
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEquals
import org.jetbrains.kotlin.test.services.JUnit5Assertions.fail
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext
@@ -83,7 +81,7 @@ private object NativeTestSupport {
computeNativeClassLoader(),
computeBaseDirs()
)
}.cast()
} as TestProcessSettings
private fun computeNativeHome(): KotlinNativeHome = KotlinNativeHome(File(ProcessLevelProperty.KOTLIN_NATIVE_HOME.readValue()))
@@ -318,7 +316,7 @@ private object NativeTestSupport {
}
TestClassSettings(parent = testProcessSettings, settings)
}.cast()
} as TestClassSettings
private fun computeTestConfiguration(enclosingTestClass: Class<*>): ComputedTestConfiguration {
val findTestConfiguration: Class<*>.() -> ComputedTestConfiguration? = {
@@ -423,7 +421,7 @@ private object NativeTestSupport {
parent = getOrCreateTestProcessSettings(),
buildList { addCommonTestClassSettingsTo(enclosingTestClass, this) }
)
}.cast()
} as SimpleTestClassSettings
/*************** Test run settings (for black box tests only) ***************/
@@ -435,7 +433,8 @@ private object NativeTestSupport {
parent = getOrCreateTestClassSettings(),
listOfNotNull(
testInstances,
ExternalSourceTransformersProvider::class to testInstances.enclosingTestInstance.safeAs<ExternalSourceTransformersProvider>()
(testInstances.enclosingTestInstance as? ExternalSourceTransformersProvider)
?.let { ExternalSourceTransformersProvider::class to it }
)
)
}
@@ -478,7 +477,7 @@ private object NativeTestSupport {
root.getStore(NAMESPACE).getOrComputeIfAbsent(testClassKeyFor<TestRunProvider>()) {
val testCaseGroupProvider = createTestCaseGroupProvider(getOrCreateTestClassSettings().get())
TestRunProvider(testCaseGroupProvider)
}.cast()
} as TestRunProvider
private fun createTestCaseGroupProvider(computedTestConfiguration: ComputedTestConfiguration): TestCaseGroupProvider {
val (testConfiguration: TestConfiguration, testConfigurationAnnotation: Annotation) = computedTestConfiguration
@@ -498,7 +497,7 @@ private object NativeTestSupport {
}
}
return constructor.call(*arguments.toTypedArray()).cast()
return constructor.call(*arguments.toTypedArray())
}
private fun KParameter.hasTypeOf(clazz: KClass<*>): Boolean = (type.classifier as? KClass<*>)?.qualifiedName == clazz.qualifiedName
@@ -8,13 +8,21 @@ package org.jetbrains.kotlin.konan.blackboxtest.support.settings
import gnu.trove.THashMap
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertTrue
import org.jetbrains.kotlin.test.services.JUnit5Assertions.fail
import org.jetbrains.kotlin.utils.addToStdlib.cast
import kotlin.reflect.KClass
internal abstract class Settings(private val parent: Settings?, settings: Iterable<Any>) {
private val map: Map<KClass<*>, Any> = THashMap<KClass<*>, Any>().apply {
settings.forEach {
val (settingClass: KClass<*>, setting: Any) = if (it is Pair<*, *>) it.cast() else it::class to it
val settingClass: KClass<*>
val setting: Any
if (it is Pair<*, *>) {
settingClass = it.first as KClass<*>
setting = it.second ?: error("Setting $settingClass is null")
} else {
settingClass = it::class
setting = it
}
val previous = put(settingClass, setting)
assertTrue(previous == null) { "Duplicated settings: $settingClass, $previous, $setting" }
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.konan.blackboxtest.support.settings
import org.jetbrains.kotlin.konan.blackboxtest.AbstractNativeSimpleTest
import org.jetbrains.kotlin.utils.addToStdlib.cast
import java.io.File
/**
@@ -17,7 +16,7 @@ import java.io.File
*/
internal class SimpleTestInstances(val allInstances: List<Any>) {
val enclosingTestInstance: AbstractNativeSimpleTest
get() = allInstances.firstOrNull().cast()
get() = allInstances.firstOrNull() as AbstractNativeSimpleTest
}
/**
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.konan.blackboxtest.support.settings
import org.jetbrains.kotlin.konan.blackboxtest.AbstractNativeBlackBoxTest
import org.jetbrains.kotlin.konan.blackboxtest.support.util.ExternalSourceTransformers
import org.jetbrains.kotlin.utils.addToStdlib.cast
import java.io.File
/**
@@ -18,7 +17,7 @@ import java.io.File
*/
internal class BlackBoxTestInstances(val allInstances: List<Any>) {
val enclosingTestInstance: AbstractNativeBlackBoxTest
get() = allInstances.firstOrNull().cast()
get() = allInstances.firstOrNull() as AbstractNativeBlackBoxTest
}
internal interface ExternalSourceTransformersProvider {
@@ -7,8 +7,6 @@ package org.jetbrains.kotlin.konan.blackboxtest.support.util
import org.jetbrains.kotlin.konan.blackboxtest.support.TestName
import org.jetbrains.kotlin.test.services.JUnit5Assertions.fail
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
/**
* Extracts [TestName]s from the test listing produced immediately during the compilation (turned on with
@@ -110,7 +108,7 @@ internal object GTestListing {
}
private inline val ParseState.testSuite: ParseState.NewTestSuite
get() = safeAs<ParseState.NewTestSuite>() ?: cast<ParseState.NewTest>().testSuite
get() = this as? ParseState.NewTestSuite ?: (this as ParseState.NewTest).testSuite
// The very first line of stdlib test output may contain seed of Random. Such line should be ignored.
private const val STDLIB_TESTS_IGNORED_LINE_PREFIX = "Seed: "
@@ -8,8 +8,6 @@ package org.jetbrains.kotlin.konan.blackboxtest.support.util
import jetbrains.buildServer.messages.serviceMessages.*
import org.jetbrains.kotlin.konan.blackboxtest.support.TestName
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertTrue
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.konan.blackboxtest.support.util.TCTestReportParseState as State
import java.text.ParseException
@@ -160,7 +158,7 @@ private class TCTestMessageParserCallback : ServiceMessageParserCallback {
fun finish() {
// The last test state is "TestStarted" this likely means that the test process terminated during test execution (SIGSEGV, etc).
state.safeAs<State.TestStarted>()?.let { failedTests += it.testName }
(state as? State.TestStarted)?.let { failedTests += it.testName }
}
}
@@ -181,6 +179,6 @@ private sealed interface TCTestReportParseState {
}
private inline val State.testSuite: State.TestSuiteStarted
get() = if (this is State.TestSuiteStarted) this else cast<State.TestState>().testSuite
get() = if (this is State.TestSuiteStarted) this else (this as State.TestState).testSuite
private inline val BaseTestMessage.simpleTestName get() = testName