Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / build-logic / src / main / kotlin / TestCoverage.kt
TestCoverage.kt118 lines · 5.4 KiBRaw
| 1 | /** |
| 2 | * Shared test-coverage infrastructure for `common-conventions-app` and |
| 3 | * `common-conventions-library`, kept here so the two convention plugins cannot drift: |
| 4 | * - [MERGED_COVERAGE_EXCLUDES]: class-file patterns stripped from every merged coverage |
| 5 | * report. AGP's own `createDevUnitTestCoverageReport` / `createDebugUnitTestCoverageReport` |
| 6 | * tasks are AGP-internal `JacocoReportTask`s with **no excludes support** (verified against |
| 7 | * AGP 9.5.0-alpha09: the task exposes the class file collection as-is), so the conventions |
| 8 | * register their own Gradle `JacocoReport` task ([MERGED_COVERAGE_TASK]) which does. |
| 9 | * - [ROBOLECTRIC_DEFAULT_SDK]: documented next to the properties file that carries it. |
| 10 | * - [mergedCoverageClassDirs]: the class files the merged report analyzes. |
| 11 | */ |
| 12 | |
| 13 | /** Task name of the per-module merged (unit + instrumented) JaCoCo report. */ |
| 14 | const val MERGED_COVERAGE_TASK = "createMergedCoverageReport" |
| 15 | |
| 16 | /** |
| 17 | * Unit-test variant each convention's merged report is built from: apps test `dev` |
| 18 | * (`testDevUnitTest`, AGP `testBuildType = "dev"`), libraries test `debug` |
| 19 | * (`testDebugUnitTest`). |
| 20 | */ |
| 21 | const val APP_UNIT_TEST_VARIANT = "dev" |
| 22 | const val LIBRARY_UNIT_TEST_VARIANT = "debug" |
| 23 | |
| 24 | /** Stable output location of the merged report, relative to the module build directory. */ |
| 25 | const val MERGED_COVERAGE_XML_PATH = "reports/coverage/merged/report.xml" |
| 26 | const val MERGED_COVERAGE_HTML_PATH = "reports/coverage/merged/html" |
| 27 | |
| 28 | /** |
| 29 | * JaCoCo version used to *render* merged reports. Must read the .exec/.ec execution data |
| 30 | * written by AGP 9.5's coverage agent (JaCoCo 0.8.14); report rendering within the same |
| 31 | * 0.8.x family is compatible. |
| 32 | */ |
| 33 | const val MERGED_COVERAGE_JACOCO_VERSION = "0.8.14" |
| 34 | |
| 35 | /** |
| 36 | * Robolectric default SDK, mirrored in `build-logic/shared-test-resources/robolectric.properties` |
| 37 | * (which is the mechanism that actually applies it; this constant only documents the intent |
| 38 | * next to the coverage/task wiring). |
| 39 | */ |
| 40 | const val ROBOLECTRIC_DEFAULT_SDK = 34 |
| 41 | |
| 42 | /** |
| 43 | * Class-file excludes applied to every merged coverage report. |
| 44 | * |
| 45 | * JaCoCo file-tree excludes are purely name-based, which settles two of the requested cases: |
| 46 | * - `*$Companion` is **not** excluded: companion objects hold real code (e.g. factory methods |
| 47 | * and default noop implementations), and there is no way to exclude "only empty" companions |
| 48 | * with a name pattern. They count as normal code. |
| 49 | * - `*Preview*` composables are **not** excluded by annotation: JaCoCo cannot filter by |
| 50 | * annotation. This is moot in practice — screenshot previews live in the `screenshotTest` |
| 51 | * source set, which never enters the report's class directories. Keep `@Preview` |
| 52 | * functions out of `src/main` and they stay out of coverage. |
| 53 | */ |
| 54 | val MERGED_COVERAGE_EXCLUDES = listOf( |
| 55 | // Generated Android code: R, BuildConfig, Manifest. |
| 56 | "**/R.class", |
| 57 | "**/R\$*.class", |
| 58 | "**/BuildConfig.class", |
| 59 | "**/BuildConfig\$*.class", |
| 60 | "**/Manifest.class", |
| 61 | "**/Manifest\$*.class", |
| 62 | // View/Data Binding (Compose-first repo, but harmless where present). |
| 63 | "**/databinding/**", |
| 64 | "**/*Binding.class", |
| 65 | "**/*Binding\$*.class", |
| 66 | "**/DataBinderMapperImpl.class", |
| 67 | // Room/KSP generated implementations. |
| 68 | "**/*_Impl.class", |
| 69 | "**/*_Impl\$*.class", |
| 70 | // kotlinx.serialization generated serializers. |
| 71 | "**/*\$\$serializer.class", |
| 72 | "**/*\$\$serializer\$*.class", |
| 73 | // Compose compiler singletons (remember/composable caches, no coverable logic). |
| 74 | "**/ComposableSingletons\$*.class", |
| 75 | // Hilt/Dagger generated code (no Hilt/Dagger in the repo today; future-proofing). |
| 76 | "**/Hilt_*.class", |
| 77 | "**/*_Hilt*.class", |
| 78 | "**/Dagger*.class", |
| 79 | "**/*_Factory.class", |
| 80 | "**/*_MembersInjector.class", |
| 81 | // protobuf-lite generated outer classes. |
| 82 | "**/*OuterClass*.class", |
| 83 | // AIDL-generated stubs. |
| 84 | "**/*\$Stub*.class", |
| 85 | ) |
| 86 | |
| 87 | /** |
| 88 | * This module's own compiled classes for [variant], before AGP's offline JaCoCo |
| 89 | * instrumentation (JaCoCo can only render from original classes), minus |
| 90 | * [MERGED_COVERAGE_EXCLUDES]. Covers Kotlin and javac output; dependency classes never |
| 91 | * appear here, so no package filter is needed. |
| 92 | */ |
| 93 | fun org.gradle.api.Project.mergedCoverageClassDirs(variant: String): org.gradle.api.file.FileTree { |
| 94 | val cap = variant.replaceFirstChar { it.uppercase() } |
| 95 | return files( |
| 96 | layout.buildDirectory.dir("intermediates/built_in_kotlinc/$variant/compile${cap}Kotlin/classes"), |
| 97 | layout.buildDirectory.dir("intermediates/javac/$variant/compile${cap}JavaWithJavac/classes"), |
| 98 | ).builtBy("compile${cap}Kotlin", "compile${cap}JavaWithJavac").asFileTree.matching { |
| 99 | MERGED_COVERAGE_EXCLUDES.forEach { exclude(it) } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Robolectric defines classes in its sandbox loader without a code-source location, and |
| 105 | * JaCoCo skips such classes by default, so Robolectric-run code reported 0% coverage. AGP |
| 106 | * attaches Gradle's JacocoTaskExtension to unit Test tasks; flip it on there. |
| 107 | * `jdk.internal.*` must stay excluded or JaCoCo instruments JDK internals and the JVM crashes. |
| 108 | */ |
| 109 | fun org.gradle.api.Project.includeRobolectricClassesInCoverage() { |
| 110 | afterEvaluate { |
| 111 | tasks.withType(org.gradle.api.tasks.testing.Test::class.java).configureEach { |
| 112 | extensions.findByType(org.gradle.testing.jacoco.plugins.JacocoTaskExtension::class.java)?.apply { |
| 113 | isIncludeNoLocationClasses = true |
| 114 | excludes = (excludes.orEmpty() + "jdk.internal.*").distinct() |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |