From 3f0ca71b7022a2c352cfc179ebd61ded3c48f465 Mon Sep 17 00:00:00 2001 From: Weimin Yu Date: Wed, 7 Aug 2019 11:21:04 -0400 Subject: [PATCH] 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. --- build.gradle | 2 + .../dependency-license/allowed_licenses.json | 3 ++ java_common.gradle | 46 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/build.gradle b/build.gradle index 2e09d5d59..beafc10c3 100644 --- a/build.gradle +++ b/build.gradle @@ -44,6 +44,8 @@ plugins { id 'idea' id 'com.diffplug.gradle.spotless' version '3.18.0' + + id 'jacoco' } wrapper { diff --git a/config/dependency-license/allowed_licenses.json b/config/dependency-license/allowed_licenses.json index aca7c1a6d..06f87a4d9 100644 --- a/config/dependency-license/allowed_licenses.json +++ b/config/dependency-license/allowed_licenses.json @@ -108,6 +108,9 @@ { "moduleLicense": "Eclipse Public License 1.0" }, + { + "moduleLicense": "Eclipse Public License v1.0" + }, { "moduleLicense": "Google App Engine Terms of Service" }, diff --git a/java_common.gradle b/java_common.gradle index 0decea3a9..a0ac49265 100644 --- a/java_common.gradle +++ b/java_common.gradle @@ -17,11 +17,17 @@ apply plugin: 'nebula.lint' apply plugin: 'net.ltgt.apt' apply plugin: 'net.ltgt.errorprone' apply plugin: 'checkstyle' +apply plugin: 'jacoco' // Checkstyle should run as part of the testing task tasks.test.dependsOn tasks.checkstyleMain 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 { // compatibility with Java 8 errorproneJavac("com.google.errorprone:javac:9+181-r4173-1") @@ -87,3 +93,43 @@ spotless { 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()) + } + } + } +}