|
| 1 | +package com.getcode.buildlogic.robolectric |
| 2 | + |
| 3 | +import org.gradle.api.Project |
| 4 | +import org.gradle.api.artifacts.VersionCatalogsExtension |
| 5 | +import org.gradle.api.file.FileCollection |
| 6 | +import org.gradle.api.tasks.InputFiles |
| 7 | +import org.gradle.api.tasks.Internal |
| 8 | +import org.gradle.api.tasks.PathSensitive |
| 9 | +import org.gradle.api.tasks.PathSensitivity |
| 10 | +import org.gradle.api.tasks.testing.Test |
| 11 | +import org.gradle.kotlin.dsl.getByType |
| 12 | +import org.gradle.kotlin.dsl.withType |
| 13 | +import org.gradle.process.CommandLineArgumentProvider |
| 14 | +import java.io.File |
| 15 | +import java.util.Properties |
| 16 | + |
| 17 | +private val SDK_ALIASES = listOf("robolectric-sdk24", "robolectric-sdk32", "robolectric-sdk34", "robolectric-sdk35", "robolectric-sdk36") |
| 18 | + |
| 19 | +/** |
| 20 | + * Feeds Robolectric its android-all jars from Gradle instead of letting each test JVM fetch them. |
| 21 | + * |
| 22 | + * Left alone, Robolectric's MavenDependencyResolver downloads a 150-220 MB jar per SDK level into |
| 23 | + * `~/.m2` from inside the test JVM. Neither CI workflow caches `~/.m2`, so every run downloads |
| 24 | + * again, and a failed fetch fails the whole test class in `classMethod` with |
| 25 | + * `AssertionError at MavenArtifactFetcher.java:129`. Resolving the jars as Gradle dependencies puts |
| 26 | + * them in the Gradle dependency cache, which CI already restores, and moves the download out of the |
| 27 | + * tests. |
| 28 | + * |
| 29 | + * Robolectric reads the jars' locations from the file named by the `robolectric-deps.properties` |
| 30 | + * system property. An SDK missing from that file fails with "no artifacts found for |
| 31 | + * DependencyJar{...}" rather than falling back to a download, so a Robolectric bump that renames |
| 32 | + * its jars fails loudly. |
| 33 | + */ |
| 34 | +fun Project.useGradleResolvedRobolectricSdks(testTaskFilter: (Test) -> Boolean = { true }) { |
| 35 | + // A module can apply more than one convention that calls this. |
| 36 | + if (configurations.findByName("robolectricSdk36") != null) return |
| 37 | + val libs = extensions.getByType<VersionCatalogsExtension>().named("libs") |
| 38 | + // One configuration per SDK: the jars are versions of one module, so a shared configuration |
| 39 | + // would resolve the conflict to the newest and drop the rest. |
| 40 | + val sdks = files( |
| 41 | + SDK_ALIASES.map { alias -> |
| 42 | + configurations.create(alias.replace("-s", "S")) { |
| 43 | + isCanBeConsumed = false |
| 44 | + isCanBeResolved = true |
| 45 | + isTransitive = false |
| 46 | + }.also { dependencies.add(it.name, libs.findLibrary(alias).get()) } |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + tasks.withType<Test>().configureEach { |
| 51 | + if (!testTaskFilter(this)) return@configureEach |
| 52 | + val depsFile = File(temporaryDir, "robolectric-deps.properties") |
| 53 | + val args = RobolectricSdkArgs(sdks, depsFile) |
| 54 | + jvmArgumentProviders.add(args) |
| 55 | + doFirst { args.writeDepsFile() } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +internal class RobolectricSdkArgs( |
| 60 | + // The jar names carry the version and the content is fixed per version, so where the Gradle |
| 61 | + // cache lives on a machine must not affect the test task's cache key. |
| 62 | + @get:InputFiles |
| 63 | + @get:PathSensitive(PathSensitivity.NAME_ONLY) |
| 64 | + val jars: FileCollection, |
| 65 | + @get:Internal |
| 66 | + val depsFile: File, |
| 67 | +) : CommandLineArgumentProvider { |
| 68 | + |
| 69 | + override fun asArguments(): Iterable<String> = |
| 70 | + listOf("-Drobolectric-deps.properties=${depsFile.absolutePath}") |
| 71 | + |
| 72 | + fun writeDepsFile() { |
| 73 | + val props = Properties() |
| 74 | + jars.files.forEach { jar -> |
| 75 | + val version = jar.name.removePrefix("$ARTIFACT-").removeSuffix(".jar") |
| 76 | + props.setProperty("org.robolectric:$ARTIFACT:$version", jar.absolutePath) |
| 77 | + } |
| 78 | + depsFile.parentFile.mkdirs() |
| 79 | + // Properties.store escapes the ':' in the coordinates, which load() would otherwise read as |
| 80 | + // the key/value separator. |
| 81 | + depsFile.writer().use { props.store(it, null) } |
| 82 | + } |
| 83 | + |
| 84 | + private companion object { |
| 85 | + const val ARTIFACT = "android-all-instrumented" |
| 86 | + } |
| 87 | +} |
0 commit comments