Fix kotlin-test variants for correct resolution in *DependenciesMetadata

Make Gradle choose the `commonVariant` when a capability-less dependency
on kotlin-test is resolved for configurations requesting the common
artifacts.

Issue #KTIJ-6098 Fixed
This commit is contained in:
Sergey Igushkin
2021-06-21 17:37:01 +03:00
committed by Space
parent 84f8d4d315
commit cf2c686630
7 changed files with 66 additions and 1 deletions
+18 -1
View File
@@ -119,11 +119,14 @@ val rootComponent = componentFactory.adhoc("root").apply {
}
val kotlinTestCapability = "$group:kotlin-test:$version" // add to variants with explicit capabilities when the default one is needed, too
val baseCapability = "$group:kotlin-test-framework:$version"
val implCapability = "$group:kotlin-test-framework-impl:$version"
val jvmTestFrameworks = listOf("junit", "junit5", "testng")
val frameworkCapabilities = mutableSetOf<String>()
jvmTestFrameworks.forEach { framework ->
val (apiVariant, runtimeVariant) = listOf("api", "runtime").map { usage ->
configurations.create("${framework}${usage.capitalize()}Variant") {
@@ -134,7 +137,9 @@ jvmTestFrameworks.forEach { framework ->
attribute(KotlinPlatformType.attribute, KotlinPlatformType.jvm)
}
outgoing.capability(baseCapability) // C0
outgoing.capability("$group:kotlin-test-framework-$framework:$version") // C0
outgoing.capability(
"$group:kotlin-test-framework-$framework:$version".also { frameworkCapabilities.add(it) }
) // C0
}
}
runtimeVariant.extendsFrom(apiVariant)
@@ -188,6 +193,18 @@ jvmTestFrameworks.forEach { framework ->
}.let { components.add(it) }
}
/**
* When a consumer's dependency requires a specific test framework (like with auto framework selection), their configurations requesting
* "common" artifacts (such as `*DependenciesMetadata` in MPP) should choose this variant anyway. Otherwise, choosing this variant
* (from a "pure", capability-less dependency on `kotlin-test` appearing transitively in the dependency graph) along with some
* capability-providing *platform* variant leads to incompatible variants being chosen together, causing dependency resolution errors,
* see KTIJ-6098
*/
commonVariant.apply {
frameworkCapabilities.forEach(outgoing::capability)
outgoing.capability(kotlinTestCapability)
}
val (jsApi, jsRuntime) = listOf("api", "runtime").map { usage ->
configurations.create("js${usage.capitalize()}") {
isCanBeConsumed = true
@@ -163,6 +163,21 @@ class KotlinSpecificDependenciesIT : BaseGradleIT() {
"jvmAndJsTestCompileOnlyDependenciesMetadata" to listOf("kotlin-test-common"),
"jvmAndJsTestImplementationDependenciesMetadata" to listOf("!kotlin-test-common"),
)
),
TestCase(
/**
* Check that in a single-platform project the common metadata configurations resolve the
* framework-specific dependency (inferred as JUnit) correctly to the common artifact
* KTIJ-6098
*/
Project("mpp-single-jvm-target"),
listOf("commonTestImplementation"),
mapOf(
"compileTestKotlinJvm" to listOf("kotlin-test-junit"),
),
mapOf(
"commonTestImplementationDependenciesMetadata" to listOf("kotlin-test-common", "kotlin-test-annotations-common")
)
)
).forEach { testCase ->
with(testCase) {
@@ -0,0 +1,12 @@
plugins {
kotlin("multiplatform")
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
jvm()
}
@@ -0,0 +1 @@
kotlin.mpp.enableGranularSourceSetsMetadata=true
@@ -0,0 +1,10 @@
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
}
val kotlin_version: String by settings
plugins {
kotlin("multiplatform").version(kotlin_version)
}
}
@@ -0,0 +1,5 @@
package com.example.thirdparty
expect fun thirdPartyFun(): String
private fun useStdlibInCommonMain() = listOf(1, 2, 3).joinToString()
@@ -0,0 +1,5 @@
package com.example.thirdparty
actual fun thirdPartyFun(): String = "thirdPartyFun @ jvm"
fun thirdPartyJvmFun() { }