Skip to content

Commit 47f8e75

Browse files
committed
binder: add a jacocoTestReport task and include it with "all" coverage
1 parent b38df6c commit 47f8e75

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

all/build.gradle

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ description = "gRPC: All"
1010
def subprojects = [
1111
project(':grpc-api'),
1212
project(':grpc-auth'),
13+
findProject(':grpc-binder'), // Optional, to tolerate -PskipAndroid.
1314
project(':grpc-core'),
1415
project(':grpc-grpclb'),
1516
project(':grpc-gcp-csm-observability'),
@@ -27,7 +28,7 @@ def subprojects = [
2728
project(':grpc-testing'),
2829
project(':grpc-util'),
2930
project(':grpc-xds'),
30-
]
31+
].findAll { it != null }
3132

3233
for (subproject in subprojects) {
3334
if (subproject == project) {
@@ -38,7 +39,7 @@ for (subproject in subprojects) {
3839
evaluationDependsOn(':grpc-interop-testing')
3940

4041
dependencies {
41-
api subprojects.minus([project(':grpc-protobuf-lite')])
42+
api subprojects.minus([project(':grpc-protobuf-lite'), findProject(':grpc-binder')].findAll { it != null })
4243
implementation libraries.guava.jre // JRE required by transitive protobuf-java-util
4344
}
4445

@@ -73,7 +74,17 @@ tasks.named("jacocoTestReport").configure {
7374
}
7475

7576
coveralls {
76-
sourceDirs = subprojects.sourceSets.main.allSource.srcDirs.flatten()
77+
// Collect source directories from all subprojects to report to Coveralls.
78+
// Android and Java projects have different ways of exposing their source directories.
79+
sourceDirs = subprojects.collectMany { subproject ->
80+
if (subproject.plugins.hasPlugin("java")) {
81+
subproject.sourceSets.main.allSource.srcDirs
82+
} else if (subproject.plugins.hasPlugin("com.android.library")) {
83+
subproject.android.sourceSets.main.java.srcDirs
84+
} else {
85+
[]
86+
}
87+
}
7788
}
7889

7990
tasks.named("coveralls").configure { dependsOn tasks.named("jacocoTestReport") }

binder/build.gradle

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ android {
2020
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2121
}
2222
lintOptions { abortOnError = false }
23+
buildTypes {
24+
debug {
25+
testCoverageEnabled true // For robolectric unit tests.
26+
enableUnitTestCoverage true // For tests that run on an emulator.
27+
}
28+
}
29+
2330
publishing {
2431
singleVariant('release') {
2532
withSourcesJar()
@@ -133,3 +140,31 @@ afterEvaluate {
133140
components.release.withVariantsFromConfiguration(configurations.releaseTestFixturesVariantReleaseApiPublication) { skip() }
134141
components.release.withVariantsFromConfiguration(configurations.releaseTestFixturesVariantReleaseRuntimePublication) { skip() }
135142
}
143+
144+
tasks.withType(Test) {
145+
// Robolectric modifies classes in memory at runtime, so they lack a java.security.CodeSource
146+
// URL to their on-disk location. By default, JaCoCo ignores classes without this property.
147+
// Overriding this allows Robolectric tests to be instrumented.
148+
jacoco.includeNoLocationClasses = true
149+
// Don't instrument certain JDK internals protected from modification by JEP 403's "strong
150+
// encapsulation." Avoids IllegalAccessError, InvalidClassException and similar at runtime.
151+
jacoco.excludes = ["jdk.internal.**"]
152+
}
153+
154+
// Android projects don't automatically get a coverage report task. We must
155+
// register one manually here and wire it up to AGP's test tasks.
156+
tasks.register("jacocoTestReport", JacocoReport) {
157+
dependsOn "testDebugUnitTest"
158+
159+
reports {
160+
// For codecov.io and coveralls.
161+
xml.required = true
162+
// Use the same output location as the other subprojects.
163+
html.outputLocation = layout.buildDirectory.dir("reports/jacoco/test/html")
164+
}
165+
166+
sourceDirectories.from = android.sourceSets.main.java.srcDirs
167+
classDirectories.from = fileTree(dir: layout.buildDirectory.dir("intermediates/javac/debug/classes"),
168+
excludes: ['**/R.class', '**/R$*.class', '**/BuildConfig.class', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*'])
169+
executionData.from = tasks.named("testDebugUnitTest").map { it.jacoco.destinationFile }
170+
}

0 commit comments

Comments
 (0)