Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added jacocoAggregatedCoverageVerification verification task for enforcing metrics #73

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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