Modern Apps git

← Modern-Apps.git

build-logic: exclude protoc-generated classes from coverage (#tests)

Commit
734854e389d6e249db5b7ba7b058498440f15c8c
Author
Vayun Mathur <[email protected]>
When
2026-11-15 16:41 UTC (20 days ago)
Parents
69dea9b
MERGED_COVERAGE_EXCLUDES only matched protobuf *OuterClass* files, so
protoc message/builder/or-builder/grpc classes counted toward merged
coverage and drowned the hand-written signal. Measured at main,
586,548 missed + 74,758 covered instructions repo-wide were protoc
output (communicate signal/whatsapp protos, auto/protocol GAL protos,
appstore protos). The CI zero-coverage fallback in
location_share_server scripts/test-run.sh already drops every class
whose top-level type has a generated .java source; the merged report
now applies the same rule so tested and untested modules agree.

Implementation (build-logic only): mergedCoverageClassDirs now filters
its class tree through ProtoGeneratedClassFilter, a serializable Spec
that drops a class file when its top-level type has a generated source
under build/generated/source/proto or
build/generated/java/generate<Variant>Proto (java/grpc/grpckt source
sets; .java and .kt). Only the build-dir path and variant are captured
at configuration time; the generated sources are scanned lazily at
report execution, so the task stays configuration-cache compatible.
Name patterns (*Protos*, *Proto$*, proto-package globs) were rejected:
communicate hand-written *Protocol classes sit one suffix from *Proto*,
hand-written code may live in a proto package later, and GAL/grpc types
match no proto name pattern. The *OuterClass* pattern stays as
belt-and-braces.

Verified with ./gradlew :communicate:createMergedCoverageReport
:auto:protocol:createMergedCoverageReport
:appstore:createMergedCoverageReport -I /tmp/ci-ignore.gradle
(INSTRUCTION counters, before -> after):
- communicate: missed 399,293 -> 112,730; covered 158,649 -> 124,008;
  total 557,942 -> 236,738; coverage 28.43% -> 52.38%
  (1,009 protoc classes removed; missed drop 286,563, matching the
  ~270k CI single-variant estimate)
- auto:protocol: missed 23,401 -> 811; covered 22,923 -> 8,762;
  total 46,324 -> 9,573; coverage 49.48% -> 91.53% (379 classes)
- appstore: missed 14,225 -> 9,305; covered 63,136 -> 60,042;
  total 77,361 -> 69,347; coverage 81.61% -> 86.58% (90 classes)
Three-module totals: 35.90% -> 61.08%. Repo-wide CI coverage
(66.5% -> ~80.5% expected) follows on the next CI run.
Removed top-level types equal the generated-source sets exactly
(41/187/41); hand-written SignalProtocol, WhatsAppProtocol* and
GalConnection remain counted, so no hand-written class is caught.
Known leftover: appstore AppServiceGrpcKt (grpckt file
AppstoreGrpcKt.kt) still counts, as under the CI file-name rule.
Second run reuses the configuration cache (all three report tasks
UP-TO-DATE in 2s). build-logic has no detekt/lint tasks; its
compileKotlin passes and detekt/lint inputs are untouched.

Diff

build-logic/src/main/kotlin/TestCoverage.kt
diff --git a/build-logic/src/main/kotlin/TestCoverage.kt b/build-logic/src/main/kotlin/TestCoverage.ktindex 16e7dcd40f..15359cdd84 100644--- a/build-logic/src/main/kotlin/TestCoverage.kt+++ b/build-logic/src/main/kotlin/TestCoverage.kt@@ -10,6 +10,11 @@  * - [mergedCoverageClassDirs]: the class files the merged report analyzes.  */ +import java.io.File+import java.io.Serializable+import org.gradle.api.file.FileCollection+import org.gradle.api.specs.Spec+ /** Task name of the per-module merged (unit + instrumented) JaCoCo report. */ const val MERGED_COVERAGE_TASK = "createMergedCoverageReport" @@ -78,26 +83,120 @@ val MERGED_COVERAGE_EXCLUDES = listOf(     "**/Dagger*.class",     "**/*_Factory.class",     "**/*_MembersInjector.class",-    // protobuf-lite generated outer classes.+    // protobuf-lite generated outer classes. Kept as belt-and-braces: the precise+    // protoc rule is ProtoGeneratedClassFilter below (every class whose top-level+    // type has a generated source); this pattern still catches an outer class if+    // its generated-source root was not scanned (e.g. a future variant layout).     "**/*OuterClass*.class",     // AIDL-generated stubs.     "**/*\$Stub*.class", ) +/**+ * Keeps protoc output out of merged coverage: a class file is dropped when its+ * top-level type has a generated source under the module's proto output roots+ * (`build/generated/source/proto` and+ * `build/generated/java/generate<Variant>Proto`). This is the same rule the+ * CI zero-coverage fallback applies (location_share_server+ * `scripts/test-run.sh`: "drop every class whose top-level type has a generated+ * .java source"), so modules with tests and modules without agree on what+ * counts. Protoc message/builder/or-builder/grpc classes are pure generated+ * plumbing with no hand-written logic; counting them drowned real coverage+ * (repo-wide ~587k missed + ~75k covered instructions at the time of writing).+ *+ * Why not name patterns (`*Protos*`, `*Proto$*`, proto-package globs)? They are both+ * too broad and too narrow: `communicate` has hand-written `*Protocol` classes+ * (`SignalProtocol`, `WhatsAppProtocol*`) one suffix away from `*Proto*`, and+ * hand-written code may legitimately live in a `proto` package later, while+ * GAL/grpc types (`AbsoluteEvent`, `AppServiceGrpc`) match no proto name+ * pattern at all. Source presence is exact: only types protoc actually emitted+ * are excluded, so no hand-written class can be caught. Verified on+ * `communicate`, `auto/protocol` and `appstore`: the excluded top-level set is+ * exactly the generated-source set, and no `src/main` class is in it.+ *+ * Laziness / configuration cache: the filter stores only the build directory+ * and variant (both serializable) and scans the generated sources on first use,+ * i.e. at report execution time, after the compile tasks have produced them.+ * Nothing is read from disk while configuring, and the [Spec] itself is+ * serializable, so the report task stays configuration-cache compatible+ * (verified by running the report twice: the second run reuses the cache).+ * The generated roots contain one source-set directory (`java`, plus `grpc` /+ * `grpckt` for grpc modules); the class relative path is the source path minus+ * that segment and its extension, so `.../java/com/foo/Bar.java` (and+ * `.../grpc/com/foo/BarGrpc.java`, `.../grpckt/... .kt`) excludes+ * `com/foo/Bar.class` and `com/foo/Bar$*.class`. Unknown class-dir layouts keep+ * the file: exclusion must never guess.+ */+class ProtoGeneratedClassFilter(+    private val buildDir: File,+    private val variantCap: String,+) : Spec<File>, Serializable {++    @Transient+    private var generatedTopLevels: Set<String>? = null++    override fun isSatisfiedBy(element: File): Boolean {+        if (!element.name.endsWith(".class")) return true+        val path = element.path.replace('\\', '/')+        val marker = "/classes/"+        val idx = path.lastIndexOf(marker)+        if (idx < 0) return true+        val rel = path.substring(idx + marker.length).removeSuffix(".class")+        val topLevel = rel.substringBefore('$')+        return topLevel !in generatedTopLevels()+    }++    private fun generatedTopLevels(): Set<String> {+        generatedTopLevels?.let { return it }+        val found = mutableSetOf<String>()+        val roots = listOf(+            File(buildDir, "generated/source/proto"),+            File(buildDir, "generated/java/generate${variantCap}Proto"),+        )+        for (root in roots) {+            if (!root.isDirectory) continue+            root.walkTopDown().forEach { file ->+                if (!file.isFile) return@forEach+                if (file.extension != "java" && file.extension != "kt") return@forEach+                val rel = file.toRelativeString(root).replace('\\', '/')+                val parts = rel.split('/')+                if (parts.size < 2) return@forEach+                val stripped = if (parts[0] == "java" || parts[0] == "grpc" || parts[0] == "grpckt") {+                    parts.drop(1).joinToString("/")+                } else {+                    rel+                }+                found += stripped.removeSuffix(".java").removeSuffix(".kt")+            }+        }+        generatedTopLevels = found+        return found+    }++    companion object {+        private const val serialVersionUID = 1L+    }+}+ /**  * This module's own compiled classes for [variant], before AGP's offline JaCoCo  * instrumentation (JaCoCo can only render from original classes), minus- * [MERGED_COVERAGE_EXCLUDES]. Covers Kotlin and javac output; dependency classes never- * appear here, so no package filter is needed.+ * [MERGED_COVERAGE_EXCLUDES] and minus protoc-generated classes+ * ([ProtoGeneratedClassFilter]). Covers Kotlin and javac output; dependency+ * classes never appear here, so no package filter is needed.  */-fun org.gradle.api.Project.mergedCoverageClassDirs(variant: String): org.gradle.api.file.FileTree {+fun org.gradle.api.Project.mergedCoverageClassDirs(variant: String): FileCollection {     val cap = variant.replaceFirstChar { it.uppercase() }-    return files(+    val tree = files(         layout.buildDirectory.dir("intermediates/built_in_kotlinc/$variant/compile${cap}Kotlin/classes"),         layout.buildDirectory.dir("intermediates/javac/$variant/compile${cap}JavaWithJavac/classes"),     ).builtBy("compile${cap}Kotlin", "compile${cap}JavaWithJavac").asFileTree.matching {         MERGED_COVERAGE_EXCLUDES.forEach { exclude(it) }     }+    // Only the build-dir path is captured here (configuration time); the+    // generated-source scan happens lazily when JaCoCo resolves the files.+    val buildDirFile = layout.buildDirectory.asFile.get()+    return tree.filter(ProtoGeneratedClassFilter(buildDirFile, cap)) }  /**