[kotlin][test] Replace CustomImportChecker.kt with KotlinMppTestHooks

GitOrigin-RevId: a83a3849e1e0955051ab20c5c2946f671eae87b2
This commit is contained in:
Sebastian Sellmair
2024-10-01 09:39:03 +00:00
committed by intellij-monorepo-bot
parent f412fe5046
commit da1a2ef3f8
6 changed files with 51 additions and 78 deletions
@@ -14,13 +14,12 @@ import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.cont
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.facets.KotlinFacetSettingsChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.highlighting.HighlightingCheckDsl
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.highlighting.HighlightingChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.hooks.TestHooks
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.hooks.TestHooksDsl
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.hooks.KotlinMppTestHooks
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.hooks.KotlinMppTestHooksDsl
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.orderEntries.OrderEntriesChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.runConfigurations.ExecuteRunConfigurationsChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.runConfigurations.RunConfigurationChecksDsl
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.runConfigurations.RunConfigurationsChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.workspace.GeneralWorkspaceChecks
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.workspace.WorkspaceChecksDsl
import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginMode
import org.jetbrains.kotlin.idea.base.test.AndroidStudioTestUtils
@@ -87,13 +86,12 @@ abstract class AbstractKotlinMppGradleImportingTest : GradleImportingTestCase(),
HighlightingCheckDsl,
TestWithKotlinPluginAndGradleVersions, DevModeTweaksDsl,
AllFilesUnderContentRootConfigurationDsl, RunConfigurationChecksDsl,
CustomGradlePropertiesDsl, DocumentationCheckerDsl, TestHooksDsl {
CustomGradlePropertiesDsl, DocumentationCheckerDsl, KotlinMppTestHooksDsl {
internal val installedFeatures = listOf<TestFeature<*>>(
GradleProjectsPublishingTestsFeature,
LinkedProjectPathsTestsFeature,
NoErrorEventsDuringImportFeature,
CustomImportChecker, // NB: Corresponding DSL is not implemented by default in most suites to not pollute the DSL
CustomGradlePropertiesTestFeature,
ContentRootsChecker,
@@ -106,7 +104,7 @@ abstract class AbstractKotlinMppGradleImportingTest : GradleImportingTestCase(),
AllFilesAreUnderContentRootChecker,
DocumentationChecker,
ReferenceTargetChecker,
TestHooks
KotlinMppTestHooks
)
private val context: KotlinMppTestsContextImpl = KotlinMppTestsContextImpl(installedFeatures)
@@ -1,52 +0,0 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures
import org.jetbrains.kotlin.gradle.multiplatformTests.*
import org.jetbrains.kotlin.idea.codeInsight.gradle.BuildGradleModelDebuggerOptions
import org.jetbrains.kotlin.idea.codeInsight.gradle.BuiltGradleModel
import org.jetbrains.kotlin.idea.codeInsight.gradle.map
import org.jetbrains.kotlin.idea.gradleTooling.KotlinMPPGradleModel
import org.jetbrains.kotlin.idea.gradleTooling.KotlinMPPGradleModelBinary
import java.io.ByteArrayInputStream
import java.io.ObjectInputStream
import kotlin.reflect.KClass
/**
* Allows to have custom post-import checks via `customChecks { ... }`-block
*
* Test infra will always load the project into IDE (a.k.a. `configureByFiles`)
*
* Use `doTest(runImport = false)` if you don't need the import to be run before your custom checks
*/
interface CustomChecksDsl {
fun TestConfigurationDslScope.customChecks(check: KotlinMppTestsContext.() -> Unit) {
writeAccess.getConfiguration(CustomImportChecker).check = check
}
fun KotlinMppTestsContext.buildKotlinMPPGradleModel(
debuggerOptions: BuildGradleModelDebuggerOptions? = null
): BuiltGradleModel<KotlinMPPGradleModel> = buildGradleModel(KotlinMPPGradleModelBinary::class, debuggerOptions)
.map { model -> ObjectInputStream(ByteArrayInputStream(model.data)).readObject() as KotlinMPPGradleModel }
fun <T : Any> KotlinMppTestsContext.buildGradleModel(
clazz: KClass<T>,
debuggerOptions: BuildGradleModelDebuggerOptions? = null
): BuiltGradleModel<T> =
org.jetbrains.kotlin.idea.codeInsight.gradle.buildGradleModel(
this.testProjectRoot,
gradleVersion,
gradleJdkPath.absolutePath,
clazz,
debuggerOptions
)
}
object CustomImportChecker : AbstractTestChecker<CustomCheck>() {
override fun createDefaultConfiguration() = CustomCheck()
override fun KotlinMppTestsContext.check() {
testConfiguration.getConfiguration(CustomImportChecker).check(this)
}
}
data class CustomCheck(var check: KotlinMppTestsContext.() -> Unit = { })
@@ -0,0 +1,25 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers
import org.jetbrains.kotlin.gradle.multiplatformTests.KotlinMppTestsContext
import org.jetbrains.kotlin.idea.codeInsight.gradle.BuildGradleModelDebuggerOptions
import org.jetbrains.kotlin.idea.codeInsight.gradle.BuiltGradleModel
import org.jetbrains.kotlin.idea.codeInsight.gradle.map
import org.jetbrains.kotlin.idea.gradleTooling.KotlinMPPGradleModel
import org.jetbrains.kotlin.idea.gradleTooling.KotlinMPPGradleModelBinary
import java.io.ByteArrayInputStream
import java.io.ObjectInputStream
import kotlin.reflect.KClass
fun <T : Any> KotlinMppTestsContext.buildGradleModel(
clazz: KClass<T>,
debuggerOptions: BuildGradleModelDebuggerOptions? = null
): BuiltGradleModel<T> =
org.jetbrains.kotlin.idea.codeInsight.gradle.buildGradleModel(
this.testProjectRoot, gradleVersion, gradleJdkPath.absolutePath, clazz, debuggerOptions
)
fun KotlinMppTestsContext.buildKotlinMPPGradleModel(
debuggerOptions: BuildGradleModelDebuggerOptions? = null
): BuiltGradleModel<KotlinMPPGradleModel> = buildGradleModel(KotlinMPPGradleModelBinary::class, debuggerOptions)
.map { model -> ObjectInputStream(ByteArrayInputStream(model.data)).readObject() as KotlinMPPGradleModel }
@@ -6,7 +6,10 @@ import org.jetbrains.kotlin.gradle.multiplatformTests.TestConfigurationDslScope
import org.jetbrains.kotlin.gradle.multiplatformTests.TestFeature
import org.jetbrains.kotlin.gradle.multiplatformTests.writeAccess
interface TestHooksDsl {
/**
* Allows 'hooking' into any stage of the import test, executing custom logic/checks/tests at this stage.
*/
interface KotlinMppTestHooksDsl {
fun TestConfigurationDslScope.runBeforeImport(action: KotlinMppTestsContext.() -> Unit) {
configuration.beforeImportHooks += action
}
@@ -24,9 +27,9 @@ interface TestHooksDsl {
}
}
object TestHooks : TestFeature<TestHooksConfiguration> {
override fun createDefaultConfiguration(): TestHooksConfiguration {
return TestHooksConfiguration()
object KotlinMppTestHooks : TestFeature<KotlinMppTestHooksConfiguration> {
override fun createDefaultConfiguration(): KotlinMppTestHooksConfiguration {
return KotlinMppTestHooksConfiguration()
}
override fun KotlinMppTestsContext.beforeImport() {
@@ -47,7 +50,7 @@ object TestHooks : TestFeature<TestHooksConfiguration> {
}
}
class TestHooksConfiguration {
class KotlinMppTestHooksConfiguration {
val beforeImportHooks = mutableListOf<KotlinMppTestsContext.() -> Unit>()
val afterImportHooks = mutableListOf<KotlinMppTestsContext.() -> Unit>()
val beforeTestExecutionHooks = mutableListOf<KotlinMppTestsContext.() -> Unit>()
@@ -55,4 +58,4 @@ class TestHooksConfiguration {
}
private val TestConfigurationDslScope.configuration
get() = writeAccess.getConfiguration(TestHooks)
get() = writeAccess.getConfiguration(KotlinMppTestHooks)
@@ -3,10 +3,11 @@ package org.jetbrains.kotlin.gradle.idea.importing.multiplatformTests
import junit.framework.AssertionFailedError
import org.jetbrains.kotlin.gradle.multiplatformTests.AbstractKotlinMppGradleImportingTest
import org.jetbrains.kotlin.gradle.multiplatformTests.TestConfigurationDslScope
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.CustomChecksDsl
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.GradleProjectsLinker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.CustomImportChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.buildGradleModel
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.buildKotlinMPPGradleModel
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.highlighting.HighlightingChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.hooks.KotlinMppTestHooks
import org.jetbrains.kotlin.idea.codeInsight.gradle.KotlinGradlePluginVersions
import org.jetbrains.kotlin.idea.codeInsight.gradle.assertNoAndroidSourceSetInfo
import org.jetbrains.kotlin.idea.codeInsight.gradle.getAndroidSourceSetInfoOrFail
@@ -29,10 +30,10 @@ import org.junit.Assume
import org.junit.Test
@TestMetadata("multiplatform/core/features/customImportTests")
class KotlinMppCustomImportingTests : AbstractKotlinMppGradleImportingTest(), CustomChecksDsl {
class KotlinMppCustomImportingTests : AbstractKotlinMppGradleImportingTest() {
override fun TestConfigurationDslScope.defaultTestConfiguration() {
// Disable all default checkers
onlyCheckers(CustomImportChecker)
onlyCheckers(KotlinMppTestHooks)
disableCheckers(HighlightingChecker)
// Those tests don't run proper import, so source files will be mistreated as not under content root
// We can't remove those sources because they are reused in other test runners (that actually check highlighting)
@@ -42,7 +43,7 @@ class KotlinMppCustomImportingTests : AbstractKotlinMppGradleImportingTest(), Cu
@Test
fun testKotlinGradlePluginVersionImporting() {
doTest(runImport = false) {
customChecks {
runAfterImport {
val builtGradleModel = buildKotlinMPPGradleModel()
val model = builtGradleModel.getNotNullByProjectPathOrThrow(":")
@@ -68,7 +69,7 @@ class KotlinMppCustomImportingTests : AbstractKotlinMppGradleImportingTest(), Cu
@Test
fun testPrepareKotlinIdeaImport() = doTest(runImport = false) {
customChecks {
runBeforeImport {
val builtGradleModel = buildGradleModel(PrepareKotlinIdeImportTaskModel::class)
assertNull(builtGradleModel.getByProjectPathOrThrow(":p3"))
@@ -122,7 +123,7 @@ class KotlinMppCustomImportingTests : AbstractKotlinMppGradleImportingTest(), Cu
@Test
fun `testPrepareKotlinIdeaImport-compositeBuild`() = doTest(runImport = false) {
customChecks {
runBeforeImport {
/* Only run against a single configuration */
Assume.assumeTrue(kotlinPluginVersion == KotlinGradlePluginVersions.latest)
@@ -139,7 +140,7 @@ class KotlinMppCustomImportingTests : AbstractKotlinMppGradleImportingTest(), Cu
@Test
fun testImportKotlinAndroidSourceSetInfo() = doTest(runImport = false) {
customChecks {
runBeforeImport {
val model = buildKotlinMPPGradleModel().getNotNullByProjectPathOrThrow(":")
val commonMain = model.getSourceSetOrFail("commonMain")
@@ -184,7 +185,7 @@ class KotlinMppCustomImportingTests : AbstractKotlinMppGradleImportingTest(), Cu
@Test
@TestMetadata("../misc/associateCompilationIntegrationTest")
fun testAssociateCompilationIntegrationTest() = doTest(runImport = false) {
customChecks {
runBeforeImport {
val model = buildKotlinMPPGradleModel().getNotNullByProjectPathOrThrow(":kmm")
/* Test all associate coordinates can be resolved */
@@ -9,14 +9,12 @@ import com.intellij.psi.PsiManager
import com.intellij.testFramework.runInEdtAndWait
import org.jetbrains.kotlin.gradle.multiplatformTests.AbstractKotlinMppGradleImportingTest
import org.jetbrains.kotlin.gradle.multiplatformTests.TestConfigurationDslScope
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.CustomChecksDsl
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.CustomGradlePropertiesTestFeature
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.highlighting.HighlightingChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.hooks.TestHooks
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.hooks.KotlinMppTestHooks
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.runConfigurations.ExecuteRunConfigurationsChecker
import org.jetbrains.kotlin.gradle.multiplatformTests.testFeatures.checkers.runConfigurations.RunConfigurationsChecker
import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginMode
import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginModeProvider
import org.jetbrains.kotlin.test.TestMetadata
import org.jetbrains.kotlin.tooling.core.compareTo
import org.jetbrains.plugins.gradle.tooling.annotation.PluginTargetVersions
@@ -24,14 +22,14 @@ import org.junit.Test
import javax.swing.Icon
@TestMetadata("multiplatform/core/features/runConfigurations")
class KotlinMppRunConfigurationsTest : AbstractKotlinMppGradleImportingTest(), CustomChecksDsl {
class KotlinMppRunConfigurationsTest : AbstractKotlinMppGradleImportingTest() {
override val pluginMode: KotlinPluginMode
get() = if (kotlinPluginVersion >= "2.0.20-dev-0") KotlinPluginMode.K2
else KotlinPluginMode.K1
override fun TestConfigurationDslScope.defaultTestConfiguration() {
onlyCheckers(RunConfigurationsChecker, ExecuteRunConfigurationsChecker, CustomGradlePropertiesTestFeature, TestHooks)
onlyCheckers(RunConfigurationsChecker, ExecuteRunConfigurationsChecker, CustomGradlePropertiesTestFeature, KotlinMppTestHooks)
disableCheckers(HighlightingChecker)
/* When executing tests, we do not care about deprecation warnings in our build output */
addCustomGradleProperty("org.gradle.warning.mode", "none")