Skip to content

Commit 623ab1e

Browse files
granthenkegwenshap
authored andcommitted
KAFKA-3451: Add basic HTML coverage report generation to gradle
Author: Grant Henke <[email protected]> Reviewers: Gwen Shapira, Ismael Juma, Ewen Cheslack-Postava Closes #1121 from granthenke/coverage
1 parent 9f6a6f9 commit 623ab1e

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Change the log4j setting in either `clients/src/test/resources/log4j.properties`
4747

4848
./gradlew -i -Dtest.single=RequestResponseSerializationTest core:test
4949

50+
### Generating test coverage reports ###
51+
./gradlew reportCoverage
52+
5053
### Building a binary release gzipped tar ball ###
5154
./gradlew clean
5255
./gradlew releaseTarGz

build.gradle

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ buildscript {
2626
// For Apache Rat plugin to ignore non-Git files
2727
classpath "org.ajoberstar:grgit:1.5.0"
2828
classpath 'com.github.ben-manes:gradle-versions-plugin:0.12.0'
29+
classpath 'org.scoverage:gradle-scoverage:2.0.1'
2930
}
3031
}
3132

3233
allprojects {
3334
apply plugin: 'idea'
3435
apply plugin: 'eclipse'
36+
apply plugin: "jacoco"
37+
3538
repositories {
3639
mavenCentral()
3740
}
@@ -249,8 +252,50 @@ subprojects {
249252
configProperties = [importControlFile: "$rootDir/checkstyle/import-control.xml"]
250253
}
251254
test.dependsOn('checkstyleMain', 'checkstyleTest')
255+
256+
// Ignore core since its a scala project
257+
if (it.path != ':core') {
258+
// NOTE: Gradles Jacoco plugin does not support "offline instrumentation" this means that classes mocked by PowerMock
259+
// may report 0 coverage, since the source was modified after initial instrumentation.
260+
// See https://github.com/jacoco/jacoco/issues/51
261+
jacocoTestReport {
262+
dependsOn tasks.test
263+
sourceSets sourceSets.main
264+
reports {
265+
html.enabled = true
266+
xml.enabled = true
267+
csv.enabled = false
268+
}
269+
}
270+
}
271+
}
272+
273+
// Aggregates all jacoco results into the root project directory
274+
task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
275+
def javaProjects = subprojects.findAll { it.path != ':core' }
276+
277+
description = 'Generates an aggregate report from all subprojects'
278+
dependsOn(javaProjects.test)
279+
280+
additionalSourceDirs = files(javaProjects.sourceSets.main.allSource.srcDirs)
281+
sourceDirectories = files(javaProjects.sourceSets.main.allSource.srcDirs)
282+
classDirectories = files(javaProjects.sourceSets.main.output)
283+
executionData = files(javaProjects.jacocoTestReport.executionData)
284+
285+
reports {
286+
html.enabled = true
287+
xml.enabled = true
288+
}
289+
290+
// workaround to ignore projects that don't have any tests at all
291+
onlyIf = { true }
292+
doFirst {
293+
executionData = files(executionData.findAll { it.exists() })
294+
}
252295
}
253296

297+
task reportCoverage(dependsOn: ['jacocoRootReport', 'core:reportScoverage'])
298+
254299
for ( sv in ['2_10', '2_11'] ) {
255300
String svInDot = sv.replaceAll( "_", ".")
256301

@@ -320,6 +365,7 @@ project(':core') {
320365
println "Building project 'core' with Scala version ${versions.scala}"
321366

322367
apply plugin: 'scala'
368+
apply plugin: "org.scoverage"
323369
archivesBaseName = "kafka_${versions.baseScala}"
324370

325371
dependencies {
@@ -351,7 +397,20 @@ project(':core') {
351397
testCompile libs.apachedsJdbmPartition
352398
testCompile libs.junit
353399
testCompile libs.scalaTest
400+
401+
scoverage libs.scoveragePlugin
402+
scoverage libs.scoverageRuntime
403+
}
404+
405+
jacocoTestReport.enabled = false
406+
scoverage {
407+
reportDir = file("${rootProject.buildDir}/scoverage")
408+
highlighting = false
409+
}
410+
checkScoverage {
411+
minimumRate = 0.0
354412
}
413+
checkScoverage.shouldRunAfter('test')
355414

356415
configurations {
357416
// manually excludes some unnecessary dependencies

core/src/main/scala/kafka/utils/ZkUtils.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,13 @@ class ZkUtils(val zkClient: ZkClient,
452452
} catch {
453453
case e1: ZkBadVersionException =>
454454
optionalChecker match {
455-
case Some(checker) => return checker(this, path, data)
456-
case _ => debug("Checker method is not passed skipping zkData match")
455+
case Some(checker) => checker(this, path, data)
456+
case _ =>
457+
debug("Checker method is not passed skipping zkData match")
458+
warn("Conditional update of path %s with data %s and expected version %d failed due to %s"
459+
.format(path, data,expectVersion, e1.getMessage))
460+
(false, -1)
457461
}
458-
warn("Conditional update of path %s with data %s and expected version %d failed due to %s".format(path, data,
459-
expectVersion, e1.getMessage))
460-
(false, -1)
461462
case e2: Exception =>
462463
warn("Conditional update of path %s with data %s and expected version %d failed due to %s".format(path, data,
463464
expectVersion, e2.getMessage))

gradle/dependencies.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ versions += [
4141
rocksDB: "4.1.0",
4242
scalaTest: "2.2.6",
4343
scalaParserCombinators: "1.0.4",
44+
scoverage: "1.1.1",
4445
slf4j: "1.7.18",
4546
snappy: "1.1.2.1",
4647
zkclient: "0.8",
@@ -96,6 +97,8 @@ libs += [
9697
scalaCompiler: "org.scala-lang:scala-compiler:$versions.scala",
9798
scalaTest: "org.scalatest:scalatest_$versions.baseScala:$versions.scalaTest",
9899
scalaParserCombinators: "org.scala-lang.modules:scala-parser-combinators_$versions.baseScala:$versions.scalaParserCombinators",
100+
scoveragePlugin: "org.scoverage:scalac-scoverage-plugin_$versions.baseScala:$versions.scoverage",
101+
scoverageRuntime: "org.scoverage:scalac-scoverage-runtime_$versions.baseScala:$versions.scoverage",
99102
slf4jApi: "org.slf4j:slf4j-api:$versions.slf4j",
100103
slf4jlog4j: "org.slf4j:slf4j-log4j12:$versions.slf4j",
101104
snappy: "org.xerial.snappy:snappy-java:$versions.snappy",

0 commit comments

Comments
 (0)