Skip to content

Commit 42bbff2

Browse files
prudhvigodithineuenfeldttj
authored andcommitted
Ability to run Code Coverage locally with Gradle (opensearch-project#18509)
* Project Code Coverage Signed-off-by: Prudhvi Godithi <[email protected]> * Project Code Coverage Signed-off-by: Prudhvi Godithi <[email protected]> * Update gradle logic Signed-off-by: Prudhvi Godithi <[email protected]> --------- Signed-off-by: Prudhvi Godithi <[email protected]>
1 parent 2ea1e3a commit 42bbff2

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5252
- Approximation Framework Enhancement: Update the BKD traversal logic to improve the performance on skewed data ([#18439](https://github.com/opensearch-project/OpenSearch/issues/18439))
5353
- Support system generated ingest pipelines for bulk update operations ([#18277](https://github.com/opensearch-project/OpenSearch/pull/18277)))
5454
- Added FS Health Check Failure metric ([#18435](https://github.com/opensearch-project/OpenSearch/pull/18435))
55+
- Ability to run Code Coverage with Gradle and produce the jacoco reports locally ([#18509](https://github.com/opensearch-project/OpenSearch/issues/18509))
5556

5657
### Changed
5758
- Create generic DocRequest to better categorize ActionRequests ([#18269](https://github.com/opensearch-project/OpenSearch/pull/18269)))

TESTING.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,25 +518,48 @@ Example:
518518

519519
# Test coverage analysis
520520

521-
The code coverage report can be generated through Gradle with [JaCoCo plugin](https://docs.gradle.org/current/userguide/jacoco_plugin.html).
521+
The code coverage report can be generated through Gradle with [JaCoCo plugin](https://docs.gradle.org/current/userguide/jacoco_plugin.html). Following are some of the ways to generate the code coverage reports locally.
522522

523523
For unit test:
524524

525-
./gradlew codeCoverageReportForUnitTest
525+
./gradlew test
526+
./gradlew jacocoTestReport
527+
528+
For unit test inside a specific module:
529+
530+
./gradlew :server:test
531+
./gradlew :server:jacocoTestReport
532+
533+
For specific unit test inside a specific module:
534+
535+
./gradlew :server:test --tests "org.opensearch.search.approximate.ApproximatePointRangeQueryTests.testNycTaxiDataDistribution"
536+
./gradlew :server:jacocoTestReport -Dtests.coverage.report.html=true
526537

527538
For integration test:
528539

529-
./gradlew codeCoverageReportForIntegrationTest
540+
./gradlew internalClusterTest
541+
./gradlew jacocoTestReport
542+
543+
For integration test inside a specific module:
544+
545+
./gradlew :server:internalClusterTest
546+
./gradlew :server:jacocoTestReport
547+
548+
For specific integration test inside a specific module:
549+
550+
./gradlew :server:internalClusterTest --tests "org.opensearch.action.admin.ClientTimeoutIT.testNodesInfoTimeout"
551+
./gradlew :server:jacocoTestReport
530552

531-
For the combined tests (unit and integration):
553+
For modules with javaRestTest:
532554

533-
./gradlew codeCoverageReport
555+
./gradlew :qa:die-with-dignity:javaRestTest
556+
./gradlew :qa:die-with-dignity:jacocoTestReport -Dtests.coverage.report.html=true
534557

535558
To generate coverage report for the combined tests after `check` task:
536559

537560
./gradlew check -Dtests.coverage=true
538561

539-
The code coverage report will be generated in `build/codeCoverageReport`, `build/codeCoverageReportForUnitTest` or `build/codeCoverageReportForIntegrationTest` correspondingly.
562+
The code coverage report will be generated in `$buildDir/build/reports/jacoco/test/html/`.
540563

541564
The report will be in XML format only by default, but you can add the following parameter for HTML and CSV format.
542565

@@ -546,7 +569,7 @@ The report will be in XML format only by default, but you can add the following
546569

547570
For example, to generate code coverage report in HTML format and not in XML format:
548571

549-
./gradlew codeCoverageReport -Dtests.coverage.report.html=true -Dtests.coverage.report.xml=false
572+
./gradlew internalClusterTest -Dtests.coverage.report.html=true -Dtests.coverage.report.xml=false
550573

551574
Apart from using Gradle, it is also possible to gain insight in code coverage using IntelliJ’s built-in coverage analysis tool that can measure coverage upon executing specific tests. Eclipse may also be able to do the same using the EclEmma plugin.
552575

gradle/code-coverage.gradle

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,54 @@ tasks.withType(JacocoReport).configureEach {
3434
}
3535
}
3636

37+
// Enhance jacocoTestReport task to include all test types
38+
allprojects {
39+
plugins.withId('jacoco') {
40+
tasks.matching { it.name == 'jacocoTestReport' }.configureEach {
41+
def executionDataFiles = []
42+
def sourceSetsList = []
43+
if (tasks.findByName('test')) {
44+
executionDataFiles.add("$buildDir/jacoco/test.exec")
45+
sourceSetsList.add(sourceSets.test)
46+
}
47+
if (tasks.findByName('internalClusterTest')) {
48+
executionDataFiles.add("$buildDir/jacoco/internalClusterTest.exec")
49+
sourceSetsList.add(sourceSets.internalClusterTest)
50+
}
51+
if (tasks.findByName('javaRestTest')) {
52+
executionDataFiles.add("$buildDir/jacoco/javaRestTest.exec")
53+
sourceSetsList.add(sourceSets.javaRestTest)
54+
}
55+
if (!executionDataFiles.isEmpty()) {
56+
executionData.setFrom(files(executionDataFiles).filter { it.exists() })
57+
sourceSets(*sourceSetsList)
58+
}
59+
onlyIf {
60+
file("$buildDir/jacoco/test.exec").exists() ||
61+
file("$buildDir/jacoco/internalClusterTest.exec").exists() ||
62+
file("$buildDir/jacoco/javaRestTest.exec").exists()
63+
}
64+
}
65+
}
66+
}
67+
3768
if (System.getProperty("tests.coverage")) {
3869
reporting {
3970
reports {
4071
testCodeCoverageReport(JacocoCoverageReport) {
4172
testSuiteName = "test"
4273
}
74+
testCodeCoverageReportJavaRestTest(JacocoCoverageReport) {
75+
testSuiteName = "javaRestTest"
76+
}
4377
}
4478
}
4579

4680
// Attach code coverage report task to Gradle check task
4781
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure {
48-
dependsOn tasks.named('testCodeCoverageReport', JacocoReport)
82+
dependsOn(
83+
tasks.named('testCodeCoverageReport', JacocoReport),
84+
tasks.named('testCodeCoverageReportJavaRestTest', JacocoReport)
85+
)
4986
}
5087
}

0 commit comments

Comments
 (0)