diff --git a/.github/workflows/compose-ui-matrix.yml b/.github/workflows/compose-ui-matrix.yml new file mode 100644 index 0000000000..6f1ac6f216 --- /dev/null +++ b/.github/workflows/compose-ui-matrix.yml @@ -0,0 +1,54 @@ +name: Compose UI Matrix + +on: + push: + branches: + - main + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + compose-ui-matrix: + timeout-minutes: 30 + runs-on: ubuntu-latest + name: Compose UI Matrix + + steps: + - name: Checkout Repo + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Setup Java Version + uses: actions/setup-java@77dd5b2ce225ea11469928ac317850245a74ae78 # v6.2.0 + with: + distribution: 'temurin' + java-version: '21' + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1 + + # Runs every version in gradle/compose-ui-versions.json; --continue reports each failing one. + - name: Run Session Replay unit tests + run: ./gradlew :sentry-android-replay:composeUiTest --continue + + - name: Upload test results + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 + with: + name: test-results-compose-ui + path: | + sentry-android-replay/build/reports/* + sentry-android-replay/build/test-results/**/*.xml + + - name: Test Report + uses: phoenix-actions/test-reporting@7317eea6e13c47348dd0bb318669485157c518d6 # pin@v16 + if: always() + with: + name: JUnit Compose UI + path: | + sentry-android-replay/build/test-results/**/*.xml + reporter: java-junit + output-to: step-summary + fail-on-error: false diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index db40bf7443..532537feb1 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -50,6 +50,10 @@ gradlePlugin { id = "io.sentry.spring-boot-matrix" implementationClass = "io.sentry.gradle.SpringBootMatrixPlugin" } + register("sentryDependencyMatrix") { + id = "io.sentry.dependency-matrix" + implementationClass = "io.sentry.gradle.DependencyMatrixPlugin" + } register("sentryOtelAgentTests") { id = "io.sentry.otel-agent-tests" implementationClass = "io.sentry.gradle.OtelAgentTestsPlugin" diff --git a/build-logic/src/main/kotlin/io/sentry/gradle/DependencyMatrixPlugin.kt b/build-logic/src/main/kotlin/io/sentry/gradle/DependencyMatrixPlugin.kt new file mode 100644 index 0000000000..1d2a06f3d8 --- /dev/null +++ b/build-logic/src/main/kotlin/io/sentry/gradle/DependencyMatrixPlugin.kt @@ -0,0 +1,118 @@ +package io.sentry.gradle + +import com.android.build.api.variant.HasHostTests +import com.android.build.api.variant.HasHostTestsBuilder +import com.android.build.api.variant.HostTestBuilder +import com.android.build.api.variant.LibraryAndroidComponentsExtension +import groovy.json.JsonSlurper +import javax.inject.Inject +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration +import org.gradle.api.tasks.TaskProvider +import org.gradle.language.base.plugins.LifecycleBasePlugin + +/** + * Runs an Android library module's unit tests on each version of a dependency listed in a + * checked-in JSON file. + * + * The module's main code compiles against the version catalog's version, and the tests for each + * version in the file run with that version, which is how users run our published artifacts on a + * newer or older version of the dependency. + */ +class DependencyMatrixPlugin : Plugin { + override fun apply(project: Project) { + project.extensions.create("dependencyMatrix", DependencyMatrixExtension::class.java) + } +} + +abstract class DependencyMatrixExtension @Inject constructor(private val project: Project) { + /** + * Registers `Test` for each version in `gradle/`, a JSON list such + * as `["1.12.1", "1.13.0-rc01"]`. Each task runs the module's unit tests with every dependency in + * [groups] at that version. An entry can also be an object such as `{"version": "1.8.3", "pin": + * {"androidx.compose.material3": "1.3.2"}}`, which also forces each group in `pin` to its + * version, for test dependencies whose current version needs a newer [groups] version. + * `Test` runs every version. + */ + fun matrix(name: String, groups: List, versionsFile: String) { + val file = project.layout.settingsDirectory.file("gradle/$versionsFile") + val entries = + JsonSlurper().parseText(project.providers.fileContents(file).asText.get()) as List<*> + check(entries.isNotEmpty()) { "gradle/$versionsFile lists no versions" } + val all = + project.tasks.register("${name}Test") { + description = "Runs the unit tests with every version in gradle/$versionsFile." + group = LifecycleBasePlugin.VERIFICATION_GROUP + } + var previous: String? = null + entries.forEach { entry -> + val version = if (entry is Map<*, *>) entry["version"] as String else entry as String + val pin = (entry as? Map<*, *>)?.get("pin") as Map<*, *>? + val forced = + groups.associateWith { version } + + pin.orEmpty().entries.associate { (group, pinned) -> group as String to pinned as String } + val variant = "$name${suffix(version)}" + val test = androidUnitTest(variant, forced) + // One version at a time: each starts its own Robolectric test JVM, and the configuration + // cache would run them all at once, which ran out of memory with 16 GB. + previous?.let { before -> + project.tasks.named { it == test }.configureEach { mustRunAfter(before) } + } + previous = test + val versionTest = + project.tasks.register("${name}Test${suffix(version)}") { + description = "Runs the unit tests with ${groups.joinToString()} at $version." + group = LifecycleBasePlugin.VERIFICATION_GROUP + dependsOn(test) + } + all.configure { dependsOn(versionTest) } + } + } + + /** + * A build type of [name] based on the test build type, whose unit tests compile and run with the + * [forced] versions. Android needs a variant per version, because AGP builds the R class and + * resources the unit tests run with from the variant's dependencies. The tests also compile + * against the [forced] versions: Kotlin compiler plugins such as the Compose compiler emit calls + * into the library's runtime that older versions don't have. + * + * Revisit this once the modules can use AGP test suites: a test suite per version could replace + * the extra build types. + */ + private fun androidUnitTest(name: String, forced: Map): String { + val components = project.extensions.getByType(LibraryAndroidComponentsExtension::class.java) + components.finalizeDsl { android -> + val base = android.testBuildType + android.buildTypes.create(name) { + initWith(android.buildTypes.getByName(base)) + matchingFallbacks += base + } + } + // AGP only generates unit tests for the test build type. + components.beforeVariants(components.selector().withBuildType(name)) { + (it as HasHostTestsBuilder).hostTests[HostTestBuilder.UNIT_TEST_TYPE]?.enable = true + } + components.onVariants(components.selector().withBuildType(name)) { + val unitTest = (it as HasHostTests).hostTests.getValue(HostTestBuilder.UNIT_TEST_TYPE) + unitTest.compileConfiguration.force(forced) + unitTest.runtimeConfiguration.force(forced) + } + val unitTest = "test${name.replaceFirstChar(Char::uppercase)}UnitTest" + // Keeps them out of `test` and `check`, which run the test build type's unit tests only, like + // without a matrix. AGP creates `test` and makes it depend on every variant's unit tests after + // the build script runs, so the dependency can only be removed in afterEvaluate. + project.afterEvaluate { + project.tasks.named("test") { + setDependsOn(dependsOn.filterNot { it is TaskProvider<*> && it.name == unitTest }) + } + } + return unitTest + } + + private fun Configuration.force(versions: Map) { + resolutionStrategy.eachDependency { versions[requested.group]?.let { useVersion(it) } } + } + + private fun suffix(version: String) = version.replace('.', '_').replace('-', '_') +} diff --git a/gradle/compose-ui-versions.json b/gradle/compose-ui-versions.json new file mode 100644 index 0000000000..2f75c2c794 --- /dev/null +++ b/gradle/compose-ui-versions.json @@ -0,0 +1,5 @@ +[ + { "version": "1.8.3", "pin": { "androidx.compose.material3": "1.3.2" } }, + "1.12.1", + "1.13.0-rc01" +] diff --git a/sentry-android-replay/build.gradle.kts b/sentry-android-replay/build.gradle.kts index d0d101d190..ebc5770544 100644 --- a/sentry-android-replay/build.gradle.kts +++ b/sentry-android-replay/build.gradle.kts @@ -1,5 +1,3 @@ -import com.android.build.api.variant.HasHostTestsBuilder -import com.android.build.api.variant.HostTestBuilder import io.gitlab.arturbosch.detekt.Detekt import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.KotlinVersion @@ -16,12 +14,9 @@ plugins { id("io.sentry.publish") id("io.sentry.api-validation") id("io.sentry.robolectric") + id("io.sentry.dependency-matrix") } -// Session Replay calls Compose internals, whose mangled JVM names change between Compose versions. -// Creating extra build types to run tests against the latest stable, and latest preview version. -val composeUiTestVersions = mapOf("composeUi112" to "1.12.1", "composeUi113" to "1.13.0-rc01") - android { compileSdk = libs.versions.compileSdk.get().toInt() compileSdkMinor = libs.versions.compileSdkMinor.get().toInt() @@ -43,12 +38,6 @@ android { buildTypes { getByName("debug") { consumerProguardFiles("proguard-rules.pro") } getByName("release") { consumerProguardFiles("proguard-rules.pro") } - composeUiTestVersions.keys.forEach { name -> - create(name) { - initWith(getByName("release")) - matchingFallbacks += "release" - } - } } // AGP 9 only generates unit tests for the testBuildType. The debug variant is @@ -85,9 +74,6 @@ android { androidComponents.beforeVariants { it.enable = !Config.Android.shouldSkipDebugVariant(it.buildType) - if (it.buildType in composeUiTestVersions) { - (it as HasHostTestsBuilder).hostTests[HostTestBuilder.UNIT_TEST_TYPE]?.enable = true - } } } @@ -117,14 +103,21 @@ dependencies { testImplementation(libs.coil.compose) } -composeUiTestVersions.forEach { (buildType, version) -> - configurations - .matching { it.name == "${buildType}UnitTestRuntimeClasspath" } - .configureEach { - resolutionStrategy.eachDependency { - if (requested.group == "androidx.compose.ui") useVersion(version) - } - } +// Session Replay calls Compose internals, whose mangled JVM names change between Compose versions. +// .github/workflows/compose-ui-matrix.yml runs each version's task. The oldest entry pins material3 +// to a version that runs on that Compose UI, because the catalog's material3 needs a newer one. +dependencyMatrix { + matrix( + name = "composeUi", + groups = + listOf( + "androidx.compose.ui", + "androidx.compose.foundation", + "androidx.compose.animation", + "androidx.compose.runtime", + ), + versionsFile = "compose-ui-versions.json", + ) } tasks.withType().configureEach {