From f9c70758a2a99f227fb0d975d0257807efa16a02 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Tue, 28 Dec 2021 15:32:47 +0100 Subject: [PATCH] [MPP] Add CInteropMetadataDependencyTransformationTaskTest --- ...etadataDependencyTransformationTaskTest.kt | 111 ++++++++++++++++++ .../gradle/MultiplatformExtensionTest.kt | 5 +- 2 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/CInteropMetadataDependencyTransformationTaskTest.kt diff --git a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/CInteropMetadataDependencyTransformationTaskTest.kt b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/CInteropMetadataDependencyTransformationTaskTest.kt new file mode 100644 index 00000000000..b078985c46a --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/CInteropMetadataDependencyTransformationTaskTest.kt @@ -0,0 +1,111 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +@file:Suppress("FunctionName", "DuplicatedCode") + +package org.jetbrains.kotlin.gradle + +import org.gradle.api.internal.TaskInternal +import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet +import org.jetbrains.kotlin.gradle.targets.native.internal.locateOrRegisterCInteropMetadataDependencyTransformationTask +import org.jetbrains.kotlin.gradle.targets.native.internal.locateOrRegisterCInteropMetadataDependencyTransformationTaskForIde +import kotlin.test.* + +class CInteropMetadataDependencyTransformationTaskTest : MultiplatformExtensionTest() { + + @Test + fun `task not registered when cinterop commonization is disabled`() { + project.enableCInteropCommonization(false) + + kotlin.linuxArm64() + kotlin.linuxX64() + + val commonMain = kotlin.sourceSets.getByName("commonMain") + val linuxArm64Main = kotlin.sourceSets.getByName("linuxArm64Main") + val linuxX64Main = kotlin.sourceSets.getByName("linuxX64Main") + val linuxMain = kotlin.sourceSets.create("linuxMain") as DefaultKotlinSourceSet + + linuxMain.dependsOn(commonMain) + linuxArm64Main.dependsOn(linuxMain) + linuxX64Main.dependsOn(linuxMain) + + /* Expect no tasks being registered without the cinterop commonization feature flag */ + assertNull(project.locateOrRegisterCInteropMetadataDependencyTransformationTask(linuxMain)) + assertNull(project.locateOrRegisterCInteropMetadataDependencyTransformationTaskForIde(linuxMain)) + } + + @Test + fun `test task ordering`() { + project.enableCInteropCommonization(true) + kotlin.linuxX64() + kotlin.linuxArm64() + + val commonMain = kotlin.sourceSets.getByName("commonMain") + val commonTest = kotlin.sourceSets.getByName("commonTest") + val linuxArm64Main = kotlin.sourceSets.getByName("linuxArm64Main") + val linuxArm64Test = kotlin.sourceSets.getByName("linuxArm64Test") + val linuxX64Main = kotlin.sourceSets.getByName("linuxX64Main") + val linuxX64Test = kotlin.sourceSets.getByName("linuxX64Test") + val nativeMain = kotlin.sourceSets.create("nativeMain") as DefaultKotlinSourceSet + val nativeTest = kotlin.sourceSets.create("nativeTest") as DefaultKotlinSourceSet + + nativeMain.dependsOn(commonMain) + linuxX64Main.dependsOn(nativeMain) + linuxArm64Main.dependsOn(nativeMain) + + nativeTest.dependsOn(commonTest) + linuxX64Test.dependsOn(nativeTest) + linuxArm64Test.dependsOn(nativeTest) + + val nativeTestTransformationTask = project.locateOrRegisterCInteropMetadataDependencyTransformationTask(nativeTest) + + assertNotNull(nativeTestTransformationTask, "Expected transformation task registered for 'nativeTest'") + assertEquals( + listOf(commonMain, commonTest, nativeMain).map { sourceSet -> + project.locateOrRegisterCInteropMetadataDependencyTransformationTask(sourceSet as DefaultKotlinSourceSet)?.get() + ?: fail("Expected transformation task registered for '${sourceSet.name}'") + }.toSet(), + nativeTestTransformationTask.get().mustRunAfter.getDependencies(null).toSet() + ) + } + + @Test + fun `test task disabled for non shared-native source sets`() { + project.enableCInteropCommonization(true) + kotlin.linuxArm64() + kotlin.linuxX64() + kotlin.jvm() + + val commonMain = kotlin.sourceSets.getByName("commonMain") + val linuxArm64Main = kotlin.sourceSets.getByName("linuxArm64Main") + val linuxX64Main = kotlin.sourceSets.getByName("linuxX64Main") + val linuxMain = kotlin.sourceSets.create("linuxMain") as DefaultKotlinSourceSet + + linuxMain.dependsOn(commonMain) + linuxArm64Main.dependsOn(linuxMain) + linuxX64Main.dependsOn(linuxMain) + + + listOf( + "commonMain", "jvmMain", "linuxArm64Main", "linuxX64Main" + ).map { sourceSetName -> kotlin.sourceSets.getByName(sourceSetName) }.forEach { sourceSet -> + val task = project.locateOrRegisterCInteropMetadataDependencyTransformationTask(sourceSet as DefaultKotlinSourceSet) + ?: return@forEach + + assertFalse( + task.get().onlyIf.isSatisfiedBy(task.get() as TaskInternal), + "Expected task ${task.name} to be disabled (not a shared native source set)" + ) + } + + val linuxMainTask = project.locateOrRegisterCInteropMetadataDependencyTransformationTaskForIde(linuxMain) + ?: fail("Expected transformation task registered for 'linuxMain'") + + assertTrue( + linuxMainTask.get().onlyIf.isSatisfiedBy(linuxMainTask.get() as TaskInternal), + "Expected task ${linuxMainTask.name} to be enabled" + ) + } +} diff --git a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/MultiplatformExtensionTest.kt b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/MultiplatformExtensionTest.kt index 0b094715dad..965d8973144 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/MultiplatformExtensionTest.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/MultiplatformExtensionTest.kt @@ -14,6 +14,7 @@ import org.gradle.api.plugins.ExtraPropertiesExtension import org.gradle.testfixtures.ProjectBuilder import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet +import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_MPP_ENABLE_CINTEROP_COMMONIZATION import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_MPP_HIERARCHICAL_STRUCTURE_BY_DEFAULT import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinSharedNativeCompilation @@ -93,8 +94,8 @@ fun Project.enableGranularSourceSetsMetadata() { propertiesExtension.set("kotlin.mpp.enableGranularSourceSetsMetadata", "true") } -fun Project.enableCInteropCommonization() { - propertiesExtension.set("kotlin.mpp.enableCInteropCommonization", "true") +fun Project.enableCInteropCommonization(enabled: Boolean = true) { + propertiesExtension.set(KOTLIN_MPP_ENABLE_CINTEROP_COMMONIZATION, enabled.toString()) } fun Project.enableHierarchicalStructureByDefault() {