Generate code coverage report (#216)

* Generate code coverage report

Enable jacoco, the official Gradle code coverage plugin.

The 'build' task will write a code coverage report to
build/reports/jacoco for each subproject that has tests.
We should consider publish periodical reports to a well known
location.

This change also defines a minimum coverage verification task.
The task is for experiment only, and is not added to the build
process yet.
This commit is contained in:
Weimin Yu 2019-08-07 11:21:04 -04:00 committed by GitHub
parent 8d289dbb54
commit 3f0ca71b70
3 changed files with 51 additions and 0 deletions

View file

@ -44,6 +44,8 @@ plugins {
id 'idea' id 'idea'
id 'com.diffplug.gradle.spotless' version '3.18.0' id 'com.diffplug.gradle.spotless' version '3.18.0'
id 'jacoco'
} }
wrapper { wrapper {

View file

@ -108,6 +108,9 @@
{ {
"moduleLicense": "Eclipse Public License 1.0" "moduleLicense": "Eclipse Public License 1.0"
}, },
{
"moduleLicense": "Eclipse Public License v1.0"
},
{ {
"moduleLicense": "Google App Engine Terms of Service" "moduleLicense": "Google App Engine Terms of Service"
}, },

View file

@ -17,11 +17,17 @@ apply plugin: 'nebula.lint'
apply plugin: 'net.ltgt.apt' apply plugin: 'net.ltgt.apt'
apply plugin: 'net.ltgt.errorprone' apply plugin: 'net.ltgt.errorprone'
apply plugin: 'checkstyle' apply plugin: 'checkstyle'
apply plugin: 'jacoco'
// Checkstyle should run as part of the testing task // Checkstyle should run as part of the testing task
tasks.test.dependsOn tasks.checkstyleMain tasks.test.dependsOn tasks.checkstyleMain
tasks.test.dependsOn tasks.checkstyleTest tasks.test.dependsOn tasks.checkstyleTest
// Generate per-subproject reports in build/reports/jacoco directory.
// TODO(weiminyu): publish periodical reports to well known location.
// TODO(weiminyu): investigate incremental coverage change calculation and alert.
tasks.test.finalizedBy jacocoTestReport
dependencies { dependencies {
// compatibility with Java 8 // compatibility with Java 8
errorproneJavac("com.google.errorprone:javac:9+181-r4173-1") errorproneJavac("com.google.errorprone:javac:9+181-r4173-1")
@ -87,3 +93,43 @@ spotless {
endWithNewline() endWithNewline()
} }
} }
// Initialize for coverage minimum determination for each sub project.
task initCoverageMinimums {
// Use current coverage ratio of each subproject as placeholder value.
// Long-term plan is to calculate incremental coverage on the fly.
rootProject.ext.coverageMinimums = [
'core' : 0.6,
'proxy' : 0.52,
'util' : 0.57
].asImmutable()
rootProject.ext.getMinCoverage = { key ->
if (rootProject.ext.coverageMinimums.containsKey(key)) {
return rootProject.ext.coverageMinimums.get(key)
}
return 0.0
}
}
// Alerts for coverage violation. Note that,
// - This task is FYI only and needs to be invoked explicitly.
// - This task does not address incremental coverage.
jacocoTestCoverageVerification {
dependsOn initCoverageMinimums
violationRules {
rule {
// Each limit consists of the following properties:
// - An 'element' type: BUNDLE (default), PACKAGE, CLASS, SOURCEFILE, or METHOD.
// - A 'counter' type: INSTRUCTION (default), LINE, BRANCH, COMPLEXITY, METHOD, or CLASS
// - A 'value' type: TOTALCOUNT, COVEREDCOUNT, MISSEDCOUNT, COVEREDRATIO (default),
// or MISSEDRATIO
// - The 'minimum' threshold, given as a fraction or a percentage (including '%')
limit {
minimum = rootProject.ext.getMinCoverage(project.getName())
}
}
}
}