Gradle plugin for computing code coverage on modified code
Diff coverage
is JaCoCo extension that computes code coverage of new/modified code based on a provided diff.
The diff content can be provided via path to patch file, URL or using embedded git(see parameters description).
Why should I use it?
buildscript {
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.github.form-com.diff-coverage-gradle:diff-coverage:0.9.4'
}
}
buildscript {
repositories {
maven("https://jitpack.io")
}
dependencies {
classpath("com.github.form-com.diff-coverage-gradle:diff-coverage:0.9.4")
}
}
JaCoCo
and Diff Coverage
pluginsJaCoCo
is used to collect coverage dataDiff Coverage
is used to generate diff reportapply plugin: 'jacoco'
apply plugin: 'com.form.diff-coverage'
plugins {
jacoco
}
apply(plugin = "com.form.diff-coverage")
diffCoverageReport {
diffSource.file = ${PATH_TO_DIFF_FILE}
violationRules.failIfCoverageLessThan 0.9
reports {
html = true
}
}
configure<com.form.coverage.gradle.ChangesetCoverageConfiguration> {
diffSource.file = ${PATH_TO_DIFF_FILE}
violationRules.failIfCoverageLessThan(0.9)
reports {
html = true
}
}
buildscript {
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.github.form-com.diff-coverage-gradle:diff-coverage:0.9.4'
}
}
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'com.form.diff-coverage'
diffCoverageReport {
diffSource {
git.compareWith 'refs/remotes/origin/develop'
}
violationRules.failIfCoverageLessThan 0.9
reports {
html = true
xml = true
csv = true
}
}
diffCoverage.dependsOn += check
./gradlew check diffCoverage
diffCoverageReport {
diffSource { // Required. Only one of `file`, `url` or git must be spesified
file = 'path/to/file.diff' // Path to diff file
url = 'http://domain.com/file.diff' // URL to retrieve diff by
git.compareWith 'refs/remotes/origin/develop' // Compares current HEAD and all uncommited with provided branch, revision or tag
}
jacocoExecFiles = files('/path/to/jacoco/exec/file.exec') // Required. By default exec files are taken from jacocoTestReport configuration if any
srcDirs = files('/path/to/sources') // Required. By default sources are taken from jacocoTestReport configuration if any
classesDirs = files('/path/to/compiled/classes') // Required. By default classes are taken from jacocoTestReport configuration if any
reports {
html = true // Optional. default `false`
xml = true // Optional. default `false`
csv = true // Optional. default `false`
baseReportDir = 'base/dir/to/store/reports' // Optional. Default 'build/reports/jacoco/'
}
violationRules.failIfCoverageLessThan 0.9 // Optional. The function sets all coverage metrics to a single value, sets failOnViolation to true
// configuration below is equivalent to the configuration above
violationRules {
minBranches = 0.9 // Optional. Default `0.0`
minLines = 0.9 // Optional. Default `0.0`
minInstructions = 0.9 // Optional. Default `0.0`
failOnViolation = true // Optional. Default `false`
}
}
The plugin adds a task diffCoverage
that has no dependencies
loads code coverage data specified by diffCoverageReport.jacocoExecFiles
analyzes the coverage data and filters according to diffSource.url
/diffSource.file
generates html report(if enabled: reports.html = true
) to directory reports.baseReportsDir
checks coverage ratio if violationRules
is specified.
Violations check is enabled if any of minBranches
, minLines
, minInstructions
is greater than 0.0
.
Fails the execution if the violation check is enabled and violationRules.failOnViolation = true
Passed:
>Task :diffCoverage
Fail on violations: true. Found violations: 0.
Failed:
> Task :diffCoverage FAILED
Fail on violations: true. Found violations: 2.
FAILURE: Build failed with an exception.
...
> java.lang.Exception: Rule violated for bundle diff-coverage-gradle: instructions covered ratio is 0.5, but expected minimum is 0.9
Rule violated for bundle diff-coverage-gradle: lines covered ratio is 0.0, but expected minimum is 0.9
Diff Coverage
plugin generates standard JaCoCo HTML report, but highlights only modified code