Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Add support to generate code coverage report with JaCoCo #1236

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ apply from: 'gradle/formatting.gradle'
apply from: 'gradle/local-distribution.gradle'
apply from: 'gradle/fips.gradle'
apply from: 'gradle/run.gradle'
apply from: 'gradle/code-coverage.gradle'

// common maven publishing configuration
allprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class BuildPlugin implements Plugin<Project> {
configureLicenseAndNotice(project)
project.pluginManager.apply('opensearch.publish')
project.pluginManager.apply(DependenciesInfoPlugin)
project.pluginManager.apply('jacoco')

PrecommitTasks.create(project, true)
}
Expand Down
93 changes: 93 additions & 0 deletions gradle/code-coverage.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

apply plugin: 'jacoco'

repositories {
mavenCentral()
}

allprojects {
plugins.withId('jacoco') {
// The default JaCoCo version in Gradle 6.6.1 is 0.8.5, but at least version 0.8.6 officially supports Java 14
jacoco.toolVersion = '0.8.7'
}
}

def codeCoverageReportTask = tasks.register("codeCoverageReport", JacocoReport) {
description = 'Generates aggregate report from all subprojects.'
executionData.setFrom fileTree(dir: '.', include: '**/build/jacoco/*.exec')
}

tasks.register("codeCoverageReportForUnitTest", JacocoReport) {
description = 'Generates aggregate report from all subprojects for unit test.'
executionData.setFrom fileTree(dir: '.', include: '**/build/jacoco/test.exec')
}

tasks.register("codeCoverageReportForIntegrationTest", JacocoReport) {
description = 'Generates aggregate report from all subprojects for integration test.'
// These kinds of tests are integration test, and the tests can be ran by Gradle tasks with the same name
def integrationTestExecPathList = ['**/build/jacoco/integTest.exec',
'**/build/jacoco/internalClusterTest.exec',
'**/build/jacoco/javaRestTest.exec',
'**/build/jacoco/yamlRestTest.exec' ]
executionData.setFrom fileTree(dir: '.', include: integrationTestExecPathList)
}

tasks.withType(JacocoReport).configureEach {
group = JavaBasePlugin.VERIFICATION_GROUP

// Select projects with corresponding tests in order to run proper tests and select proper classes to generate the report
def projectsWithJavaPlugin = subprojects.findAll { it.pluginManager.hasPlugin('java') }
def projectsWithUnitTest = projectsWithJavaPlugin.findAll { it.tasks.findByName('test').enabled }
def projectsWithIntegTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('integTest')}
def projectsWithInternalClusterTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('internalClusterTest')}
def projectsWithJavaRestTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('javaRestTest')}
def projectsWithYamlRestTest = projectsWithJavaPlugin.findAll {it.tasks.findByName('yamlRestTest')}
def projectsWithIntegrationTest = projectsWithIntegTest + projectsWithInternalClusterTest + projectsWithJavaRestTest + projectsWithYamlRestTest
def projectsWithTest = projectsWithUnitTest + projectsWithIntegrationTest

def selectedProjects
switch (name) {
case "codeCoverageReportForUnitTest":
dependsOn projectsWithUnitTest.test
selectedProjects = projectsWithUnitTest
break
case "codeCoverageReportForIntegrationTest":
dependsOn projectsWithIntegTest.integTest
dependsOn projectsWithInternalClusterTest.internalClusterTest
dependsOn projectsWithJavaRestTest.javaRestTest
dependsOn projectsWithYamlRestTest.yamlRestTest
selectedProjects = projectsWithIntegrationTest
break
default:
dependsOn projectsWithUnitTest.test
dependsOn projectsWithIntegTest.integTest
dependsOn projectsWithInternalClusterTest.internalClusterTest
dependsOn projectsWithJavaRestTest.javaRestTest
dependsOn projectsWithYamlRestTest.yamlRestTest
selectedProjects = projectsWithTest
break
}
sourceDirectories.setFrom files(selectedProjects.sourceSets.main.allSource.srcDirs)
classDirectories.setFrom files(selectedProjects.sourceSets.main.output)

reports {
// Code coverage report in HTML and CSV formats are on demand, in case they take extra disk space.
xml.enabled System.getProperty('tests.coverage.report.xml', 'true').toBoolean()
html.enabled System.getProperty('tests.coverage.report.html', 'false').toBoolean()
csv.enabled System.getProperty('tests.coverage.report.csv', 'false').toBoolean()
}
}

if (System.getProperty("tests.coverage")) {
// Attach code coverage report task to Gradle check task
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure {
dependsOn codeCoverageReportTask
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ public class BootstrapForTesting {
if (Strings.hasLength(System.getProperty("tests.config"))) {
FilePermissionUtils.addSingleFilePath(perms, PathUtils.get(System.getProperty("tests.config")), "read,readlink");
}
// jacoco coverage output file
final boolean testsCoverage =
Booleans.parseBoolean(System.getProperty("tests.coverage", "false"));
if (testsCoverage) {
Path coverageDir = PathUtils.get(System.getProperty("tests.coverage.dir"));
FilePermissionUtils.addSingleFilePath(perms, coverageDir.resolve("jacoco.exec"), "read,write");
// in case we get fancy and use the -integration goals later:
FilePermissionUtils.addSingleFilePath(perms, coverageDir.resolve("jacoco-it.exec"), "read,write");
}
// intellij hack: intellij test runner wants setIO and will
// screw up all test logging without it!
if (System.getProperty("tests.gradle") == null) {
Expand Down