Skip to content

Commit

Permalink
Added DSL to copy one report variant
Browse files Browse the repository at this point in the history
In order to create different filters for the same set of report content at the same time, it is convenient to copy an already created and configured report variant and filter this copy separately

Co-authored-by: Leonid Startsev <[email protected]>

PR #620
  • Loading branch information
shanshin authored Jun 7, 2024
1 parent 798b972 commit 87a8da8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions kover-gradle-plugin/api/kover-gradle-plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public abstract interface class kotlinx/kover/gradle/plugin/dsl/KoverBinaryTaskC
}

public abstract interface class kotlinx/kover/gradle/plugin/dsl/KoverCurrentProjectVariantsConfig : kotlinx/kover/gradle/plugin/dsl/KoverVariantConfig {
public abstract fun copyVariant (Ljava/lang/String;Ljava/lang/String;)V
public abstract fun createVariant (Ljava/lang/String;Lorg/gradle/api/Action;)V
public abstract fun instrumentation (Lorg/gradle/api/Action;)V
public abstract fun providedVariant (Ljava/lang/String;Lorg/gradle/api/Action;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kotlinx.kover.gradle.plugin.commons.ReportVariantType
import kotlinx.kover.gradle.plugin.commons.TOTAL_VARIANT_NAME
import kotlinx.kover.gradle.plugin.dsl.internal.KoverReportSetConfigImpl
import kotlinx.kover.gradle.plugin.dsl.internal.KoverVariantCreateConfigImpl
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.newInstance


Expand Down Expand Up @@ -59,13 +58,6 @@ internal fun KoverContext.finalizing(origins: AllVariantOrigins) {
jvmVariant?.let { variantArtifacts[JVM_VARIANT_NAME] = it }
androidVariants.forEach { variantArtifacts[it.variantName] = it }

val availableVariants = variantArtifacts.keys + projectExtension.current.customVariants.keys
projectExtension.reports.byName.forEach { (requestedVariant, _) ->
if (requestedVariant !in availableVariants) {
throw KoverIllegalConfigException("It is not possible to configure the '$requestedVariant' variant because it does not exist")
}
}

val totalVariant =
TotalVariantArtifacts(project, toolProvider, koverBucketConfiguration, variantConfig(TOTAL_VARIANT_NAME), projectExtension)
variantArtifacts.values.forEach { totalVariant.mergeWith(it) }
Expand Down Expand Up @@ -103,6 +95,8 @@ internal fun KoverContext.finalizing(origins: AllVariantOrigins) {
}
}

variantArtifacts[name] = customVariant

VariantReportsSet(
project,
name,
Expand All @@ -112,7 +106,21 @@ internal fun KoverContext.finalizing(origins: AllVariantOrigins) {
reporterClasspath,
projectExtension.koverDisabled
).assign(customVariant)
}

projectExtension.current.variantsToCopy.forEach { (name, originVariantName) ->
val originalVariant = variantArtifacts[originVariantName]
?: throw KoverIllegalConfigException("Cannot create a variant '$name': the original variant '$originVariantName' does not exist.")

VariantReportsSet(
project,
name,
ReportVariantType.CUSTOM,
toolProvider,
reportsConfig(name, project.path),
reporterClasspath,
projectExtension.koverDisabled
).assign(originalVariant)
}

androidVariants.forEach { androidVariant ->
Expand All @@ -126,6 +134,12 @@ internal fun KoverContext.finalizing(origins: AllVariantOrigins) {
projectExtension.koverDisabled
).assign(androidVariant)
}

projectExtension.reports.byName.forEach { (requestedVariant, _) ->
if (requestedVariant !in variantArtifacts && requestedVariant !in projectExtension.current.variantsToCopy) {
throw KoverIllegalConfigException("It is not possible to configure the '$requestedVariant' variant because it does not exist")
}
}
}

private fun KoverContext.variantConfig(variantName: String): KoverVariantCreateConfigImpl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public interface KoverProjectExtension {
* // ...
* }
*
* // copy report variant with different name but the same content
* copyVariant("copyName", "custom")
*
* // Configure the variant that is automatically created in the current project
* // For example, "jvm" for JVM target or "debug" for Android build variant
* providedVariant("jvm") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public interface KoverCurrentProjectVariantsConfig: KoverVariantConfig {
*/
public fun createVariant(variantName: String, block: Action<KoverVariantCreateConfig>)

/**
* Create custom report variant as a copy of the [originalVariantName] variant, however, with the new name [variantName].
*
* Copying variants is useful when you have a single variant with coverage information but want to apply different filters for it:
* You cannot configure the copied variant with [KoverVariantCreateConfig], i.e., add information from other variants to the new one.
* However, you can define report filters and rules for it using `reports { variant(variantName) { ... }}`.
*/
public fun copyVariant(variantName: String, originalVariantName: String)

/**
* Configure the variant with name [variantName] that is automatically created in the current project.
* For example, `"jvm"` for JVM target or `"debug"` for Android build variant.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import kotlinx.kover.gradle.plugin.commons.TOTAL_VARIANT_NAME
import kotlinx.kover.gradle.plugin.dsl.*
import org.gradle.api.Action
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.newInstance
import javax.inject.Inject

internal abstract class KoverCurrentProjectVariantsConfigImpl @Inject constructor(val objects: ObjectFactory) :
KoverVariantConfigImpl(objects), KoverCurrentProjectVariantsConfig {
internal val customVariants: MutableMap<String, KoverVariantCreateConfigImpl> = mutableMapOf()
internal val providedVariants: MutableMap<String, KoverVariantConfigImpl> = mutableMapOf()
internal val variantsToCopy: MutableMap<String, String> = mutableMapOf()
internal val instrumentation: KoverProjectInstrumentation = objects.newInstance()

init {
Expand All @@ -42,6 +42,13 @@ internal abstract class KoverCurrentProjectVariantsConfigImpl @Inject constructo
block.execute(variantConfig)
}

override fun copyVariant(variantName: String, originalVariantName: String) {
if (variantName in variantsToCopy) {
throw KoverIllegalConfigException("The copy of custom variant with name $variantName already created.")
}
variantsToCopy[variantName] = originalVariantName
}

override fun providedVariant(variantName: String, block: Action<KoverVariantConfig>) {
if (variantName == TOTAL_VARIANT_NAME) {
throw KoverIllegalConfigException("The provided variant name cannot be empty.")
Expand Down

0 comments on commit 87a8da8

Please sign in to comment.