Skip to content

Commit 4557ed1

Browse files
authored
add code coverage badge (#171)
1 parent d36924d commit 4557ed1

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<img src="https://opensearch.org/assets/img/opensearch-logo-themed.svg" height="64px">
2+
[![codecov](https://codecov.io/gh/opensearch-project/common-utils/branch/main/graph/badge.svg?token=CQ01D9MNQL)](https://codecov.io/gh/opensearch-project/common-utils)
23

34
- [OpenSearch Common Utils](#opensearch-common-utils)
45
- [Contributing](#contributing)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/**
7+
* OpenSearch Plugin build tools don't work with the Gradle Jacoco Plugin to report coverage out of the box.
8+
* https://github.com/elastic/elasticsearch/issues/28867.
9+
*
10+
* This code sets up coverage reporting manually for OpenSearch plugin tests. This is complicated because:
11+
* 1. The OpenSearch integTest Task doesn't implement Gradle's JavaForkOptions so we have to manually start the jacoco agent with the test JVM
12+
* 2. The cluster nodes are stopped using 'kill -9' which means jacoco can't dump it's execution output to a file on VM shutdown
13+
* 3. The Java Security Manager prevents JMX from writing execution output to the file.
14+
*
15+
* To workaround these we start the cluster with jmx enabled and then use Jacoco's JMX MBean to get the execution data before the
16+
* cluster is stopped and dump it to a file. Luckily our current security policy seems to allow this. This will also probably
17+
* break if there are multiple nodes in the integTestCluster. But for now... it sorta works.
18+
*/
19+
apply plugin: 'jacoco'
20+
21+
// Get gradle to generate the required jvm agent arg for us using a dummy tasks of type Test. Unfortunately Elastic's
22+
// testing tasks don't derive from Test so the jacoco plugin can't do this automatically.
23+
def jacocoDir = "${buildDir}/jacoco"
24+
task dummyTest(type: Test) {
25+
enabled = false
26+
workingDir = file("/") // Force absolute path to jacoco agent jar
27+
jacoco {
28+
destinationFile = file("${jacocoDir}/test.exec")
29+
destinationFile.parentFile.mkdirs()
30+
jmx = true
31+
}
32+
}
33+
34+
jacocoTestReport {
35+
dependsOn test
36+
executionData dummyTest.jacoco.destinationFile
37+
getSourceDirectories().from(sourceSets.main.allSource)
38+
getClassDirectories().from(sourceSets.main.output)
39+
reports {
40+
html.enabled = true // human readable
41+
xml.enabled = true // for coverlay
42+
}
43+
}
44+
45+
project.gradle.projectsEvaluated {
46+
jacocoTestReport.dependsOn test
47+
}
48+
49+
check.dependsOn jacocoTestReport

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ apply plugin: 'io.gitlab.arturbosch.detekt'
6363
apply plugin: 'org.jetbrains.kotlin.jvm'
6464
apply plugin: 'org.jetbrains.kotlin.plugin.allopen'
6565
apply plugin: 'opensearch.repositories'
66+
apply from: 'build-tools/opensearchplugin-coverage.gradle'
6667

6768
configurations {
6869
ktlint

0 commit comments

Comments
 (0)