|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Stream License; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/GetStream/stream-build-conventions-android/blob/main/LICENSE |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.getstream.android.coverage |
| 17 | + |
| 18 | +import com.android.build.api.dsl.ApplicationExtension |
| 19 | +import com.android.build.api.dsl.CommonExtension |
| 20 | +import com.android.build.api.dsl.LibraryExtension |
| 21 | +import io.getstream.android.StreamProjectExtension |
| 22 | +import io.getstream.android.requireStreamProjectExtension |
| 23 | +import kotlinx.kover.gradle.plugin.dsl.KoverProjectExtension |
| 24 | +import org.gradle.api.Project |
| 25 | +import org.gradle.kotlin.dsl.configure |
| 26 | +import org.sonarqube.gradle.SonarExtension |
| 27 | + |
| 28 | +private object SonarConstants { |
| 29 | + const val HOST_URL = "https://sonarcloud.io" |
| 30 | + const val ORGANIZATION = "getstream" |
| 31 | + |
| 32 | + val EXCLUSIONS = |
| 33 | + listOf( |
| 34 | + "**/test/**", |
| 35 | + "**/androidTest/**", |
| 36 | + "**/R.class", |
| 37 | + "**/R2.class", |
| 38 | + "**/R$*.class", |
| 39 | + "**/BuildConfig.*", |
| 40 | + "**/Manifest*.*", |
| 41 | + "**/*Test*.*", |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +object KoverConstants { |
| 46 | + const val VARIANT_NAME = "coverage" |
| 47 | + const val VARIANT_SUFFIX = "Coverage" |
| 48 | + const val TEST_TASK = "test$VARIANT_SUFFIX" |
| 49 | + val CLASS_EXCLUSIONS = listOf("*R", "*R$*", "*BuildConfig", "*Manifest*", "*Composable*") |
| 50 | + val ANNOTATION_EXCLUSIONS = arrayOf("androidx.compose.ui.tooling.preview.Preview") |
| 51 | +} |
| 52 | + |
| 53 | +internal fun Project.configureCoverageRoot() { |
| 54 | + pluginManager.apply("org.sonarqube") |
| 55 | + |
| 56 | + afterEvaluate { |
| 57 | + val projectExtension = requireStreamProjectExtension() |
| 58 | + val includedModules = projectExtension.coverage.includedModules.get() |
| 59 | + |
| 60 | + configureKover(projectExtension.coverage, isRoot = true) |
| 61 | + setupKoverDependencyOnModules(includedModules) |
| 62 | + configureSonar(projectExtension) |
| 63 | + registerAggregatedCoverageTask(includedModules) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +private fun Project.configureSonar(extension: StreamProjectExtension) { |
| 68 | + val repositoryName = extension.repositoryName.get() |
| 69 | + val exclusions = buildList { |
| 70 | + addAll(SonarConstants.EXCLUSIONS) |
| 71 | + addAll(extension.coverage.sonarCoverageExclusions.get()) |
| 72 | + } |
| 73 | + |
| 74 | + extensions.configure<SonarExtension> { |
| 75 | + properties { |
| 76 | + property("sonar.host.url", SonarConstants.HOST_URL) |
| 77 | + property("sonar.token", System.getenv("SONAR_TOKEN")) |
| 78 | + property("sonar.organization", SonarConstants.ORGANIZATION) |
| 79 | + property("sonar.projectKey", "GetStream_$repositoryName") |
| 80 | + property("sonar.projectName", repositoryName) |
| 81 | + property("sonar.java.coveragePlugin", "jacoco") |
| 82 | + property("sonar.sourceEncoding", "UTF-8") |
| 83 | + property("sonar.java.binaries", "$rootDir/**/build/tmp/kotlin-classes/debug") |
| 84 | + property("sonar.coverage.exclusions", exclusions) |
| 85 | + property( |
| 86 | + "sonar.coverage.jacoco.xmlReportPaths", |
| 87 | + layout.buildDirectory |
| 88 | + .file("/reports/kover/report${KoverConstants.VARIANT_SUFFIX}.xml") |
| 89 | + .get(), |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +private fun Project.setupKoverDependencyOnModules(includedModules: Set<String>) { |
| 96 | + subprojects.forEach { subproject -> |
| 97 | + if (subproject.name in includedModules) { |
| 98 | + dependencies.add("kover", subproject) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +internal fun Project.configureCoverageModule() { |
| 104 | + val coverageOptions = requireStreamProjectExtension().coverage |
| 105 | + |
| 106 | + // Only configure coverage for included modules |
| 107 | + if (name !in coverageOptions.includedModules.get()) { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + pluginManager.apply("org.sonarqube") |
| 112 | + |
| 113 | + // Configure Android test coverage if this is an Android module |
| 114 | + pluginManager.withPlugin("com.android.library") { configureAndroid<LibraryExtension>() } |
| 115 | + pluginManager.withPlugin("com.android.application") { configureAndroid<ApplicationExtension>() } |
| 116 | + |
| 117 | + configureKover(coverageOptions, isRoot = false) |
| 118 | + registerModuleCoverageTask() |
| 119 | +} |
| 120 | + |
| 121 | +private fun Project.registerAggregatedCoverageTask(includedModules: Set<String>) { |
| 122 | + tasks.register(KoverConstants.TEST_TASK) { |
| 123 | + group = "verification" |
| 124 | + description = "Run all tests in all modules and generate merged coverage report" |
| 125 | + |
| 126 | + // Depend on all module-specific testCoverage tasks |
| 127 | + val coverageModuleTasks = |
| 128 | + subprojects |
| 129 | + .filter { it.name in includedModules } |
| 130 | + .map { ":${it.name}:${KoverConstants.TEST_TASK}" } |
| 131 | + dependsOn(coverageModuleTasks) |
| 132 | + |
| 133 | + finalizedBy( |
| 134 | + "koverXmlReport${KoverConstants.VARIANT_SUFFIX}", |
| 135 | + "koverHtmlReport${KoverConstants.VARIANT_SUFFIX}", |
| 136 | + ) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +private fun Project.registerModuleCoverageTask() { |
| 141 | + // Determine the appropriate test task based on module type and plugins |
| 142 | + val hasPaparazziPlugin = pluginManager.hasPlugin("app.cash.paparazzi") |
| 143 | + val hasAndroidPlugin = |
| 144 | + pluginManager.hasPlugin("com.android.library") || |
| 145 | + pluginManager.hasPlugin("com.android.application") |
| 146 | + |
| 147 | + val testTaskName = |
| 148 | + when { |
| 149 | + hasPaparazziPlugin -> "verifyPaparazziDebug" |
| 150 | + hasAndroidPlugin -> "testDebugUnitTest" |
| 151 | + else -> "test" |
| 152 | + } |
| 153 | + |
| 154 | + tasks.register(KoverConstants.TEST_TASK) { |
| 155 | + group = "verification" |
| 156 | + description = "Run module-specific tests" |
| 157 | + dependsOn(testTaskName) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +private inline fun <reified E : CommonExtension<*, *, *, *, *, *>> Project.configureAndroid() { |
| 162 | + extensions.configure<E> { |
| 163 | + buildTypes { |
| 164 | + getByName("debug") { |
| 165 | + enableUnitTestCoverage = true |
| 166 | + enableAndroidTestCoverage = true |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +private fun Project.configureKover(options: CoverageOptions, isRoot: Boolean) { |
| 173 | + pluginManager.apply("org.jetbrains.kotlinx.kover") |
| 174 | + |
| 175 | + extensions.configure<KoverProjectExtension> { |
| 176 | + // Create custom variant in each project (including root) for coverage aggregation |
| 177 | + currentProject { |
| 178 | + createVariant(KoverConstants.VARIANT_NAME) { |
| 179 | + if (isRoot) { |
| 180 | + // Root variant is empty, it just aggregates from dependencies |
| 181 | + } else { |
| 182 | + add("jvm", "debug", optional = true) |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + reports { |
| 188 | + verify.warningInsteadOfFailure.set(true) |
| 189 | + |
| 190 | + filters.excludes { |
| 191 | + classes(KoverConstants.CLASS_EXCLUSIONS) |
| 192 | + classes(options.koverClassExclusions.get()) |
| 193 | + |
| 194 | + annotatedBy(*KoverConstants.ANNOTATION_EXCLUSIONS) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments