|
| 1 | +package io.sentry.gradle |
| 2 | + |
| 3 | +import com.android.build.api.variant.HasHostTests |
| 4 | +import com.android.build.api.variant.HasHostTestsBuilder |
| 5 | +import com.android.build.api.variant.HostTestBuilder |
| 6 | +import com.android.build.api.variant.LibraryAndroidComponentsExtension |
| 7 | +import groovy.json.JsonSlurper |
| 8 | +import javax.inject.Inject |
| 9 | +import org.gradle.api.Plugin |
| 10 | +import org.gradle.api.Project |
| 11 | +import org.gradle.api.artifacts.Configuration |
| 12 | +import org.gradle.api.tasks.TaskProvider |
| 13 | +import org.gradle.language.base.plugins.LifecycleBasePlugin |
| 14 | + |
| 15 | +/** |
| 16 | + * Runs an Android library module's unit tests on each version of a dependency listed in a |
| 17 | + * checked-in JSON file. |
| 18 | + * |
| 19 | + * The module's main code compiles against the version catalog's version, and the tests for each |
| 20 | + * version in the file run with that version, which is how users run our published artifacts on a |
| 21 | + * newer or older version of the dependency. |
| 22 | + */ |
| 23 | +class DependencyMatrixPlugin : Plugin<Project> { |
| 24 | + override fun apply(project: Project) { |
| 25 | + project.extensions.create("dependencyMatrix", DependencyMatrixExtension::class.java) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +abstract class DependencyMatrixExtension @Inject constructor(private val project: Project) { |
| 30 | + /** |
| 31 | + * Registers `<name>Test<version>` for each version in `gradle/<versionsFile>`, a JSON list such |
| 32 | + * as `["1.12.1", "1.13.0-rc01"]`. Each task runs the module's unit tests with every dependency in |
| 33 | + * [groups] at that version. An entry can also be an object such as `{"version": "1.8.3", "pin": |
| 34 | + * {"androidx.compose.material3": "1.3.2"}}`, which also forces each group in `pin` to its |
| 35 | + * version, for test dependencies whose current version needs a newer [groups] version. |
| 36 | + * `<name>Test` runs every version. |
| 37 | + */ |
| 38 | + fun matrix(name: String, groups: List<String>, versionsFile: String) { |
| 39 | + val file = project.layout.settingsDirectory.file("gradle/$versionsFile") |
| 40 | + val entries = |
| 41 | + JsonSlurper().parseText(project.providers.fileContents(file).asText.get()) as List<*> |
| 42 | + check(entries.isNotEmpty()) { "gradle/$versionsFile lists no versions" } |
| 43 | + val all = |
| 44 | + project.tasks.register("${name}Test") { |
| 45 | + description = "Runs the unit tests with every version in gradle/$versionsFile." |
| 46 | + group = LifecycleBasePlugin.VERIFICATION_GROUP |
| 47 | + } |
| 48 | + var previous: String? = null |
| 49 | + entries.forEach { entry -> |
| 50 | + val version = if (entry is Map<*, *>) entry["version"] as String else entry as String |
| 51 | + val pin = (entry as? Map<*, *>)?.get("pin") as Map<*, *>? |
| 52 | + val forced = |
| 53 | + groups.associateWith { version } + |
| 54 | + pin.orEmpty().entries.associate { (group, pinned) -> group as String to pinned as String } |
| 55 | + val variant = "$name${suffix(version)}" |
| 56 | + val test = androidUnitTest(variant, forced) |
| 57 | + // One version at a time: each starts its own Robolectric test JVM, and the configuration |
| 58 | + // cache would run them all at once, which ran out of memory with 16 GB. |
| 59 | + previous?.let { before -> |
| 60 | + project.tasks.named { it == test }.configureEach { mustRunAfter(before) } |
| 61 | + } |
| 62 | + previous = test |
| 63 | + val versionTest = |
| 64 | + project.tasks.register("${name}Test${suffix(version)}") { |
| 65 | + description = "Runs the unit tests with ${groups.joinToString()} at $version." |
| 66 | + group = LifecycleBasePlugin.VERIFICATION_GROUP |
| 67 | + dependsOn(test) |
| 68 | + } |
| 69 | + all.configure { dependsOn(versionTest) } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * A build type of [name] based on the test build type, whose unit tests compile and run with the |
| 75 | + * [forced] versions. Android needs a variant per version, because AGP builds the R class and |
| 76 | + * resources the unit tests run with from the variant's dependencies. The tests also compile |
| 77 | + * against the [forced] versions: Kotlin compiler plugins such as the Compose compiler emit calls |
| 78 | + * into the library's runtime that older versions don't have. |
| 79 | + * |
| 80 | + * Revisit this once the modules can use AGP test suites: a test suite per version could replace |
| 81 | + * the extra build types. |
| 82 | + */ |
| 83 | + private fun androidUnitTest(name: String, forced: Map<String, String>): String { |
| 84 | + val components = project.extensions.getByType(LibraryAndroidComponentsExtension::class.java) |
| 85 | + components.finalizeDsl { android -> |
| 86 | + val base = android.testBuildType |
| 87 | + android.buildTypes.create(name) { |
| 88 | + initWith(android.buildTypes.getByName(base)) |
| 89 | + matchingFallbacks += base |
| 90 | + } |
| 91 | + } |
| 92 | + // AGP only generates unit tests for the test build type. |
| 93 | + components.beforeVariants(components.selector().withBuildType(name)) { |
| 94 | + (it as HasHostTestsBuilder).hostTests[HostTestBuilder.UNIT_TEST_TYPE]?.enable = true |
| 95 | + } |
| 96 | + components.onVariants(components.selector().withBuildType(name)) { |
| 97 | + val unitTest = (it as HasHostTests).hostTests.getValue(HostTestBuilder.UNIT_TEST_TYPE) |
| 98 | + unitTest.compileConfiguration.force(forced) |
| 99 | + unitTest.runtimeConfiguration.force(forced) |
| 100 | + } |
| 101 | + val unitTest = "test${name.replaceFirstChar(Char::uppercase)}UnitTest" |
| 102 | + // Keeps them out of `test` and `check`, which run the test build type's unit tests only, like |
| 103 | + // without a matrix. AGP creates `test` and makes it depend on every variant's unit tests after |
| 104 | + // the build script runs, so the dependency can only be removed in afterEvaluate. |
| 105 | + project.afterEvaluate { |
| 106 | + project.tasks.named("test") { |
| 107 | + setDependsOn(dependsOn.filterNot { it is TaskProvider<*> && it.name == unitTest }) |
| 108 | + } |
| 109 | + } |
| 110 | + return unitTest |
| 111 | + } |
| 112 | + |
| 113 | + private fun Configuration.force(versions: Map<String, String>) { |
| 114 | + resolutionStrategy.eachDependency { versions[requested.group]?.let { useVersion(it) } } |
| 115 | + } |
| 116 | + |
| 117 | + private fun suffix(version: String) = version.replace('.', '_').replace('-', '_') |
| 118 | +} |
0 commit comments