Skip to content

Commit

Permalink
Added jacocoAggregatedCoverageVerification verification task for en…
Browse files Browse the repository at this point in the history
…forcing metrics
  • Loading branch information
gmazzo committed Oct 29, 2023
1 parent 1b5087e commit f00b1d1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,33 @@ run to produce the report. All dependent `test` tasks will be run too to produce
The same for `:testAggregatedReport`:
![Aggregated Test Report example](README-aggregated-test-report.png)

## Enforcing aggregated code coverage metrics
The same as `JaCoCo Plugin` supports [Enforcing code coverage metrics](https://docs.gradle.org/current/userguide/jacoco_plugin.html#ex-configuring-violation-rules)
this plugin adds a ':jacocoAggregatedCoverageVerification' to provide the same feature, but with the aggregated metrics:
```kotlin
tasks.jacocoAggregatedCoverageVerification {
violationRules {
rule {
limit {
minimum = "0.5".toBigDecimal()
}
}

rule {
isEnabled = false
element = "CLASS"
includes = listOf("org.gradle.*")

limit {
counter = "LINE"
value = "TOTALCOUNT"
maximum = "0.3".toBigDecimal()
}
}
}
}
```

## The `aggregateTestCoverage` DSL extension
This is an opt-in/out switch meant to be used when having `productFlavors`.

Expand Down
18 changes: 18 additions & 0 deletions demo-project/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ testAggregation {
exclude("**/ContentMainBinding*")
}
}

tasks.jacocoAggregatedCoverageVerification {
violationRules {
rule {
limit {// current 19%
minimum = "0.19".toBigDecimal()
}
limit {// desired 80%
minimum = "0.8".toBigDecimal()
isFailOnViolation = false
}
}
}
}

tasks.check {
dependsOn(tasks.jacocoAggregatedCoverageVerification)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.the
import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.gradle.testing.jacoco.plugins.JacocoCoverageReport
import org.gradle.testing.jacoco.tasks.JacocoCoverageVerification

class TestCoverageAggregationPlugin : Plugin<Project> {

Expand All @@ -38,6 +40,16 @@ class TestCoverageAggregationPlugin : Plugin<Project> {
}
}

tasks.register<JacocoCoverageVerification>("jacocoAggregatedCoverageVerification") {
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = "Verifies code coverage metrics for the aggregated report"

val reportTask = provider { jacocoReport.reportTask.get() } // breaks dependency
executionData(reportTask.map { it.executionData })
classDirectories.from(reportTask.map { it.classDirectories })
sourceDirectories.from(reportTask.map { it.sourceDirectories })
}

val jacocoAggregation by configurations

allprojects {
Expand Down

0 comments on commit f00b1d1

Please sign in to comment.