[mpp] Update the IdeImportPropertiesConsistencyTest

#KT-48554 Fixed
This commit is contained in:
Stanislav Erokhin
2023-07-28 17:39:32 +02:00
committed by teamcity
parent a58dd84cdc
commit a3eb472e5f
@@ -9,92 +9,52 @@ package org.jetbrains.kotlin.gradle.regressionTests
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder import org.gradle.testfixtures.ProjectBuilder
import org.jetbrains.kotlin.gradle.targets.metadata.isKotlinGranularMetadataEnabled
import org.jetbrains.kotlin.gradle.targets.native.internal.isNativeDependencyPropagationEnabled
import org.jetbrains.kotlin.gradle.util.applyMultiplatformPlugin import org.jetbrains.kotlin.gradle.util.applyMultiplatformPlugin
import org.jetbrains.kotlin.gradle.util.enableHierarchicalStructureByDefault
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
class IdeImportPropertiesConsistencyTest { class IdeImportPropertiesConsistencyTest {
/* /*
IDE IMPORT SOURCE CODE: Since 233+ IDEA even bundled kotlin plugin do not relying on the "kotlin.mpp.enableGranularSourceSetsMetadata" property
https://youtrack.jetbrains.com/issue/KTIJ-19551 and HMPP enabled in IDE by default. But for previous IDE versions with bundled Kotlin plugin we have the code that
will disable the HMPP if this property is not set to true.
As described in the ticket above, we have a separate implementation running in the IDE import that To mitigate that we should pass the "kotlin.mpp.enableGranularSourceSetsMetadata=true" for cases where KGP 1.9.20+ trying to
will resolve properties on the project. Unfortunately, this implementation will choose its own defaults. open in the old IDE.
Changing defaults for such properties therefore can't be done easily in the KGP, without taking This compatibility trick could be safely removed in 2.1, but probably we should keep it in 2.0.
extra precaution ensuring that the IDE plugin would still receive the correct property.
Until the implementation is changed and IDEs with old implementation are out of support, Related issue: https://youtrack.jetbrains.com/issue/KTIJ-19551
this code is copied into this test here and checked for consistency!
*/ */
private fun Project.getProperty(property: GradleImportProperties): Boolean { private fun Project.getProperty(propertyId: String): Boolean? = try {
val explicitValueIfAny = try { (findProperty(propertyId) as? String)?.toBoolean()
(findProperty(property.id) as? String)?.toBoolean() } catch (e: Exception) {
} catch (e: Exception) { logger.error("Error while trying to read property $propertyId from project $project", e)
logger.error("Error while trying to read property $property from project $project", e) null
null
}
return explicitValueIfAny ?: property.defaultValue
} }
private enum class GradleImportProperties(val id: String, val defaultValue: Boolean) {
IS_HMPP_ENABLED("kotlin.mpp.enableGranularSourceSetsMetadata", false),
ENABLE_NATIVE_DEPENDENCY_PROPAGATION("kotlin.native.enableDependencyPropagation", true),
/* IDE only relevant cases omitted */
;
}
/*
END OF IDE IMPORT SOURCE CODE!
*/
@Test @Test
fun `test simple project`() { fun `test simple project`() {
val project = ProjectBuilder.builder().build() val project = ProjectBuilder.builder().build()
project.applyMultiplatformPlugin() project.applyMultiplatformPlugin()
project.assertPropertiesMatch() project.assertKotlinGranularMetadataEnabled()
} }
@Test @Test
fun `test simple project with new hmpp flag`() { fun `test HMPP enabled for all projects`() {
val project = ProjectBuilder.builder().build()
project.enableHierarchicalStructureByDefault()
project.applyMultiplatformPlugin()
project.assertPropertiesMatch()
}
@Test
fun `test sub project with new hmpp flag`() {
val rootProject = ProjectBuilder.builder().build() val rootProject = ProjectBuilder.builder().build()
val project = ProjectBuilder.builder().withParent(rootProject).build() val project = ProjectBuilder.builder().withParent(rootProject).build()
rootProject.enableHierarchicalStructureByDefault()
project.applyMultiplatformPlugin() project.applyMultiplatformPlugin()
project.assertPropertiesMatch() project.assertKotlinGranularMetadataEnabled()
rootProject.assertKotlinGranularMetadataEnabled()
} }
private fun Project.assertPropertiesMatch() { private fun Project.assertKotlinGranularMetadataEnabled() {
val isHmppValueUsedInCli = project.isKotlinGranularMetadataEnabled val granularMetadataEnabled = project.getProperty("kotlin.mpp.enableGranularSourceSetsMetadata")
val isHmppValueUsedInIde = project.getProperty(GradleImportProperties.IS_HMPP_ENABLED)
assertEquals( assertEquals(
isHmppValueUsedInCli, isHmppValueUsedInIde, true, granularMetadataEnabled,
""" "kotlin.mpp.enableGranularSourceSetsMetadata: $granularMetadataEnabled"
project.isKotlinGranularMetadataEnabled: $isHmppValueUsedInCli
GradleImportProperties.IS_HMPP_ENABLED: $isHmppValueUsedInIde
""".trimIndent()
)
val dependencyPropagationEnabledInCli = project.isNativeDependencyPropagationEnabled
val dependencyPropagationEnabledInIde = project.getProperty(GradleImportProperties.ENABLE_NATIVE_DEPENDENCY_PROPAGATION)
assertEquals(
dependencyPropagationEnabledInCli, dependencyPropagationEnabledInIde,
"""
propertiesProvider.nativeDependencyPropagation: $dependencyPropagationEnabledInCli
GradleImportProperties.ENABLE_NATIVE_DEPENDENCY_PROPAGATION: $dependencyPropagationEnabledInIde
""".trimIndent()
) )
} }
} }