From a4cfc11b8d788c0d3c2ac60dde784f4cd4081c3b Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:50:33 +0000 Subject: [PATCH 1/4] ci(replay): Test Session Replay against the latest Compose UI versions from Google Maven Replace the hardcoded composeUi112/composeUi113 build types with a Compose UI matrix workflow. Like the AGP matrix in sentry-android-gradle-plugin, a Kotlin script reads androidx.compose.ui:ui's maven-metadata.xml and picks the latest stable and, when newer, the latest pre-release. Each matrix job runs the replay unit tests with VERSION_COMPOSE_UI forcing that version on the test runtime classpath. The workflow also runs daily, so new Compose releases are tested without a version bump PR. Co-Authored-By: Roman Zavarnitsyn --- .github/workflows/compose-ui-matrix.yml | 80 +++++++++++++++++++++++++ scripts/compose-ui-versions.main.kts | 31 ++++++++++ sentry-android-replay/build.gradle.kts | 22 ++----- 3 files changed, 116 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/compose-ui-matrix.yml create mode 100644 scripts/compose-ui-versions.main.kts diff --git a/.github/workflows/compose-ui-matrix.yml b/.github/workflows/compose-ui-matrix.yml new file mode 100644 index 0000000000..b112c632f3 --- /dev/null +++ b/.github/workflows/compose-ui-matrix.yml @@ -0,0 +1,80 @@ +name: Compose UI Matrix + +on: + # Daily, to catch new Compose releases that break Session Replay without waiting for a PR. + schedule: + - cron: '0 4 * * *' + push: + branches: + - main + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + versions: + runs-on: ubuntu-latest + name: Compose UI versions + outputs: + versions: ${{ steps.versions.outputs.versions }} + + steps: + - name: Checkout Repo + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + sparse-checkout: scripts + + - name: Read latest Compose UI versions from Google Maven + id: versions + run: kotlin scripts/compose-ui-versions.main.kts + + compose-ui-matrix: + needs: versions + timeout-minutes: 30 + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + compose-ui: ${{ fromJSON(needs.versions.outputs.versions) }} + + name: Compose UI ${{ matrix.compose-ui }} + env: + VERSION_COMPOSE_UI: ${{ matrix.compose-ui }} + + 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 + + - name: Run Session Replay unit tests + run: ./gradlew :sentry-android-replay:testReleaseUnitTest + + - name: Upload test results + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 + with: + name: test-results-compose-ui-${{ matrix.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 ${{ matrix.compose-ui }} + path: | + sentry-android-replay/build/test-results/**/*.xml + reporter: java-junit + output-to: step-summary + fail-on-error: false diff --git a/scripts/compose-ui-versions.main.kts b/scripts/compose-ui-versions.main.kts new file mode 100644 index 0000000000..a50ba83168 --- /dev/null +++ b/scripts/compose-ui-versions.main.kts @@ -0,0 +1,31 @@ +#!/usr/bin/env kotlin +@file:DependsOn("io.github.z4kn4fein:semver-jvm:3.1.0") + +// Prints the androidx.compose.ui versions that the Compose UI matrix workflow runs the +// sentry-android-replay unit tests against: the latest stable, plus the latest pre-release when +// it is newer. Same approach as the AGP matrix in getsentry/sentry-android-gradle-plugin. + +import io.github.z4kn4fein.semver.Version +import java.io.File +import javax.xml.parsers.DocumentBuilderFactory + +val metadataUrl = + "https://dl.google.com/dl/android/maven2/androidx/compose/ui/ui/maven-metadata.xml" + +val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(metadataUrl) +val versionNodes = document.documentElement.getElementsByTagName("version") +val versions = + (0 until versionNodes.length).map { Version.parse(versionNodes.item(it).textContent, strict = false) } + +val latestStable = versions.filter { it.isStable }.max() +val latestPreRelease = versions.filter { it.isPreRelease }.max() + +val matrix = buildList { + add(latestStable) + if (latestPreRelease > latestStable) add(latestPreRelease) +} +val json = matrix.joinToString(separator = ",", prefix = "[", postfix = "]") { "\"$it\"" } + +println(json) +val githubOutput = System.getenv("GITHUB_OUTPUT") +if (githubOutput != null) File(githubOutput).appendText("versions=$json\n") diff --git a/sentry-android-replay/build.gradle.kts b/sentry-android-replay/build.gradle.kts index d0d101d190..684c76b437 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 @@ -18,10 +16,6 @@ plugins { id("io.sentry.robolectric") } -// 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 +37,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 +73,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,9 +102,12 @@ dependencies { testImplementation(libs.coil.compose) } -composeUiTestVersions.forEach { (buildType, version) -> +// Session Replay calls Compose internals, whose mangled JVM names change between Compose versions. +// .github/workflows/compose-ui-matrix.yml sets VERSION_COMPOSE_UI to run the unit tests against the +// latest stable and pre-release versions from Google Maven. +System.getenv("VERSION_COMPOSE_UI")?.let { version -> configurations - .matching { it.name == "${buildType}UnitTestRuntimeClasspath" } + .matching { it.name == "releaseUnitTestRuntimeClasspath" } .configureEach { resolutionStrategy.eachDependency { if (requested.group == "androidx.compose.ui") useVersion(version) From 320b506701840f7a6f2c760338fe432fb2195cc1 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:32:37 +0000 Subject: [PATCH 2/4] ci(replay): Open an issue when the scheduled Compose UI matrix fails GitHub only notifies the workflow's creator about scheduled runs, which is the bot that pushed it, so a failing daily run would go unnoticed. On a scheduled failure, open an issue, or comment on the open one so repeated failures stay in one place. Co-Authored-By: Roman Zavarnitsyn --- .github/workflows/compose-ui-matrix.yml | 21 +++++++++++++++++++++ scripts/report-compose-ui-matrix-failure.sh | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 scripts/report-compose-ui-matrix-failure.sh diff --git a/.github/workflows/compose-ui-matrix.yml b/.github/workflows/compose-ui-matrix.yml index b112c632f3..54cd63e989 100644 --- a/.github/workflows/compose-ui-matrix.yml +++ b/.github/workflows/compose-ui-matrix.yml @@ -78,3 +78,24 @@ jobs: reporter: java-junit output-to: step-summary fail-on-error: false + + report-failure: + needs: [versions, compose-ui-matrix] + # Nobody gets GitHub's notifications for scheduled runs, so open an issue instead. + if: failure() && github.event_name == 'schedule' + runs-on: ubuntu-latest + name: Report scheduled failure + permissions: + contents: read + issues: write + + steps: + - name: Checkout Repo + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + sparse-checkout: scripts + + - name: Open or update the failure issue + run: ./scripts/report-compose-ui-matrix-failure.sh + env: + GH_TOKEN: ${{ github.token }} diff --git a/scripts/report-compose-ui-matrix-failure.sh b/scripts/report-compose-ui-matrix-failure.sh new file mode 100755 index 0000000000..244faac2d1 --- /dev/null +++ b/scripts/report-compose-ui-matrix-failure.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# Reports a failed scheduled Compose UI matrix run: opens an issue, or comments on the open one so +# repeated daily failures stay in one place. Needs GH_TOKEN, GITHUB_REPOSITORY, GITHUB_SERVER_URL, +# and GITHUB_RUN_ID, which GitHub Actions sets. +set -eu + +ISSUE_TITLE='Session Replay: Compose UI matrix fails on the latest Compose UI versions' +export ISSUE_TITLE +run_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" +body="The scheduled [Compose UI matrix run]($run_url) failed. A new \`androidx.compose.ui:ui\` release may have changed Compose internals that Session Replay calls. Check the failing jobs for the version." + +# --search is fuzzy, so match the exact title afterwards. +existing=$(gh issue list --repo "$GITHUB_REPOSITORY" --state open --search "$ISSUE_TITLE in:title" \ + --json number,title --jq '[.[] | select(.title == env.ISSUE_TITLE)][0].number // empty') + +if [ -n "$existing" ]; then + gh issue comment "$existing" --repo "$GITHUB_REPOSITORY" --body "$body" +else + gh issue create --repo "$GITHUB_REPOSITORY" --title "$ISSUE_TITLE" --body "$body" \ + --label 'Session Replay' --label 'Jetpack Compose' +fi From 1e2c1acc6d97bd15c1ecea06d023c3948e5b1d1d Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:08:44 +0000 Subject: [PATCH 3/4] ci(replay): Read the Compose UI matrix versions from a checked-in file Like the Spring Boot matrix, keep the Compose UI versions in gradle/compose-ui-versions.json instead of looking them up on Google Maven on every run, so the same commit always tests the same versions. Drop the daily schedule and its failure issue, which only made sense with the live lookup. Co-Authored-By: Roman Zavarnitsyn --- .github/workflows/compose-ui-matrix.yml | 32 ++++----------------- gradle/compose-ui-versions.json | 1 + scripts/compose-ui-versions.main.kts | 31 -------------------- scripts/read-compose-ui-versions.sh | 8 ++++++ scripts/report-compose-ui-matrix-failure.sh | 21 -------------- sentry-android-replay/build.gradle.kts | 2 +- 6 files changed, 15 insertions(+), 80 deletions(-) create mode 100644 gradle/compose-ui-versions.json delete mode 100644 scripts/compose-ui-versions.main.kts create mode 100755 scripts/read-compose-ui-versions.sh delete mode 100755 scripts/report-compose-ui-matrix-failure.sh diff --git a/.github/workflows/compose-ui-matrix.yml b/.github/workflows/compose-ui-matrix.yml index 54cd63e989..f8f871ed65 100644 --- a/.github/workflows/compose-ui-matrix.yml +++ b/.github/workflows/compose-ui-matrix.yml @@ -1,9 +1,6 @@ name: Compose UI Matrix on: - # Daily, to catch new Compose releases that break Session Replay without waiting for a PR. - schedule: - - cron: '0 4 * * *' push: branches: - main @@ -24,11 +21,13 @@ jobs: - name: Checkout Repo uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - sparse-checkout: scripts + sparse-checkout: | + gradle + scripts - - name: Read latest Compose UI versions from Google Maven + - name: Read Compose UI versions id: versions - run: kotlin scripts/compose-ui-versions.main.kts + run: ./scripts/read-compose-ui-versions.sh compose-ui-matrix: needs: versions @@ -78,24 +77,3 @@ jobs: reporter: java-junit output-to: step-summary fail-on-error: false - - report-failure: - needs: [versions, compose-ui-matrix] - # Nobody gets GitHub's notifications for scheduled runs, so open an issue instead. - if: failure() && github.event_name == 'schedule' - runs-on: ubuntu-latest - name: Report scheduled failure - permissions: - contents: read - issues: write - - steps: - - name: Checkout Repo - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - sparse-checkout: scripts - - - name: Open or update the failure issue - run: ./scripts/report-compose-ui-matrix-failure.sh - env: - GH_TOKEN: ${{ github.token }} diff --git a/gradle/compose-ui-versions.json b/gradle/compose-ui-versions.json new file mode 100644 index 0000000000..a3e9fc5745 --- /dev/null +++ b/gradle/compose-ui-versions.json @@ -0,0 +1 @@ +["1.12.1", "1.13.0-rc01"] diff --git a/scripts/compose-ui-versions.main.kts b/scripts/compose-ui-versions.main.kts deleted file mode 100644 index a50ba83168..0000000000 --- a/scripts/compose-ui-versions.main.kts +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env kotlin -@file:DependsOn("io.github.z4kn4fein:semver-jvm:3.1.0") - -// Prints the androidx.compose.ui versions that the Compose UI matrix workflow runs the -// sentry-android-replay unit tests against: the latest stable, plus the latest pre-release when -// it is newer. Same approach as the AGP matrix in getsentry/sentry-android-gradle-plugin. - -import io.github.z4kn4fein.semver.Version -import java.io.File -import javax.xml.parsers.DocumentBuilderFactory - -val metadataUrl = - "https://dl.google.com/dl/android/maven2/androidx/compose/ui/ui/maven-metadata.xml" - -val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(metadataUrl) -val versionNodes = document.documentElement.getElementsByTagName("version") -val versions = - (0 until versionNodes.length).map { Version.parse(versionNodes.item(it).textContent, strict = false) } - -val latestStable = versions.filter { it.isStable }.max() -val latestPreRelease = versions.filter { it.isPreRelease }.max() - -val matrix = buildList { - add(latestStable) - if (latestPreRelease > latestStable) add(latestPreRelease) -} -val json = matrix.joinToString(separator = ",", prefix = "[", postfix = "]") { "\"$it\"" } - -println(json) -val githubOutput = System.getenv("GITHUB_OUTPUT") -if (githubOutput != null) File(githubOutput).appendText("versions=$json\n") diff --git a/scripts/read-compose-ui-versions.sh b/scripts/read-compose-ui-versions.sh new file mode 100755 index 0000000000..6e20e9e01f --- /dev/null +++ b/scripts/read-compose-ui-versions.sh @@ -0,0 +1,8 @@ +#!/bin/sh +# Writes the Compose UI versions from gradle/compose-ui-versions.json to the step's outputs, as a +# JSON list for the Compose UI matrix. +set -eu + +versions=$(jq -c -e 'if type == "array" and length > 0 then . else error("expected a non-empty list") end' \ + gradle/compose-ui-versions.json) +printf 'versions=%s\n' "$versions" >> "$GITHUB_OUTPUT" diff --git a/scripts/report-compose-ui-matrix-failure.sh b/scripts/report-compose-ui-matrix-failure.sh deleted file mode 100755 index 244faac2d1..0000000000 --- a/scripts/report-compose-ui-matrix-failure.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -# Reports a failed scheduled Compose UI matrix run: opens an issue, or comments on the open one so -# repeated daily failures stay in one place. Needs GH_TOKEN, GITHUB_REPOSITORY, GITHUB_SERVER_URL, -# and GITHUB_RUN_ID, which GitHub Actions sets. -set -eu - -ISSUE_TITLE='Session Replay: Compose UI matrix fails on the latest Compose UI versions' -export ISSUE_TITLE -run_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" -body="The scheduled [Compose UI matrix run]($run_url) failed. A new \`androidx.compose.ui:ui\` release may have changed Compose internals that Session Replay calls. Check the failing jobs for the version." - -# --search is fuzzy, so match the exact title afterwards. -existing=$(gh issue list --repo "$GITHUB_REPOSITORY" --state open --search "$ISSUE_TITLE in:title" \ - --json number,title --jq '[.[] | select(.title == env.ISSUE_TITLE)][0].number // empty') - -if [ -n "$existing" ]; then - gh issue comment "$existing" --repo "$GITHUB_REPOSITORY" --body "$body" -else - gh issue create --repo "$GITHUB_REPOSITORY" --title "$ISSUE_TITLE" --body "$body" \ - --label 'Session Replay' --label 'Jetpack Compose' -fi diff --git a/sentry-android-replay/build.gradle.kts b/sentry-android-replay/build.gradle.kts index 684c76b437..a6a82bdebd 100644 --- a/sentry-android-replay/build.gradle.kts +++ b/sentry-android-replay/build.gradle.kts @@ -104,7 +104,7 @@ dependencies { // Session Replay calls Compose internals, whose mangled JVM names change between Compose versions. // .github/workflows/compose-ui-matrix.yml sets VERSION_COMPOSE_UI to run the unit tests against the -// latest stable and pre-release versions from Google Maven. +// versions in gradle/compose-ui-versions.json. System.getenv("VERSION_COMPOSE_UI")?.let { version -> configurations .matching { it.name == "releaseUnitTestRuntimeClasspath" } From b567302a088e2c9b25cc45d0cbd838d7689a8e7a Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:47:34 +0000 Subject: [PATCH 4/4] ci(replay): Register a test task per Compose UI version with a dependency matrix plugin Adds io.sentry.dependency-matrix, which reads a versions file in gradle/ and registers Test tasks. Android modules get a build type per version, kept out of check; JVM modules reuse the compiled tests on a versioned runtime classpath. The workflow reads the file with jq inline, like the Spring Boot matrix workflows. Co-Authored-By: roman Co-Authored-By: Nelson Osacky --- .github/workflows/compose-ui-matrix.yml | 10 +- build-logic/build.gradle.kts | 4 + .../sentry/gradle/DependencyMatrixPlugin.kt | 123 ++++++++++++++++++ scripts/read-compose-ui-versions.sh | 8 -- sentry-android-replay/build.gradle.kts | 18 ++- 5 files changed, 139 insertions(+), 24 deletions(-) create mode 100644 build-logic/src/main/kotlin/io/sentry/gradle/DependencyMatrixPlugin.kt delete mode 100755 scripts/read-compose-ui-versions.sh diff --git a/.github/workflows/compose-ui-matrix.yml b/.github/workflows/compose-ui-matrix.yml index f8f871ed65..78c42cd24f 100644 --- a/.github/workflows/compose-ui-matrix.yml +++ b/.github/workflows/compose-ui-matrix.yml @@ -21,13 +21,11 @@ jobs: - name: Checkout Repo uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - sparse-checkout: | - gradle - scripts + sparse-checkout: gradle - name: Read Compose UI versions id: versions - run: ./scripts/read-compose-ui-versions.sh + run: echo "versions=$(jq -c . gradle/compose-ui-versions.json)" >> "$GITHUB_OUTPUT" compose-ui-matrix: needs: versions @@ -40,7 +38,7 @@ jobs: name: Compose UI ${{ matrix.compose-ui }} env: - VERSION_COMPOSE_UI: ${{ matrix.compose-ui }} + COMPOSE_UI_VERSION: ${{ matrix.compose-ui }} steps: - name: Checkout Repo @@ -56,7 +54,7 @@ jobs: uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1 - name: Run Session Replay unit tests - run: ./gradlew :sentry-android-replay:testReleaseUnitTest + run: ./gradlew ":sentry-android-replay:composeUiTest${COMPOSE_UI_VERSION//[.-]/_}" - name: Upload test results if: always() 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..321df80348 --- /dev/null +++ b/build-logic/src/main/kotlin/io/sentry/gradle/DependencyMatrixPlugin.kt @@ -0,0 +1,123 @@ +package io.sentry.gradle + +import com.android.build.api.dsl.LibraryExtension +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.SourceSetContainer +import org.gradle.api.tasks.TaskProvider +import org.gradle.api.tasks.testing.Test +import org.gradle.language.base.plugins.LifecycleBasePlugin + +/** + * Runs a module's unit tests on each version of a dependency listed in a checked-in JSON file, for + * Android library and JVM modules. + * + * The module compiles against the version catalog's version, and the tests for each version in the + * file run with that version on their runtime classpath, 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 + * [group] at that version. A CI workflow reads the same file and runs each version in its own + * job. In an Android module, call it after `android {}`. + */ + fun matrix(name: String, group: String, versionsFile: String) { + val file = project.layout.settingsDirectory.file("gradle/$versionsFile") + val versions = + (JsonSlurper().parseText(project.providers.fileContents(file).asText.get()) as List<*>).map { + it as String + } + check(versions.isNotEmpty()) { "gradle/$versionsFile lists no versions" } + val android = project.plugins.hasPlugin("com.android.library") + versions.forEach { version -> + val variant = "$name${suffix(version)}" + val test = if (android) androidUnitTest(variant) else jvmTest(variant) + project.configurations + .matching { it.name == runtimeClasspathName(variant, android) } + .configureEach { forceGroup(group, version) } + project.tasks.register("${name}Test${suffix(version)}") { + description = "Runs the unit tests with $group at $version." + this.group = LifecycleBasePlugin.VERIFICATION_GROUP + dependsOn(test) + } + } + } + + /** + * A build type of [name] based on the test build type, whose unit tests run on their own runtime + * classpath. Android needs a variant per version, because AGP builds the R class and resources + * the unit tests run with from the variant's dependencies. + */ + private fun androidUnitTest(name: String): String { + val android = project.extensions.getByType(LibraryExtension::class.java) + val base = android.testBuildType + android.buildTypes.create(name) { + initWith(android.buildTypes.getByName(base)) + matchingFallbacks += base + } + val components = project.extensions.getByType(LibraryAndroidComponentsExtension::class.java) + // 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 + } + 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 adds the dependency when it creates the variant's tasks, after the + // build script runs. + project.afterEvaluate { + project.tasks.named("test") { + setDependsOn(dependsOn.filterNot { it is TaskProvider<*> && it.name == unitTest }) + } + } + return unitTest + } + + /** + * A test task of [name] that reuses the compiled tests on its own runtime classpath, like + * [SpringBootMatrixPlugin] does. + */ + private fun jvmTest(name: String): TaskProvider { + val sourceSets = project.extensions.getByType(SourceSetContainer::class.java) + val test = sourceSets.getByName("test") + val configurations = project.configurations + val runtimeClasspath = + configurations.resolvable(runtimeClasspathName(name, android = false)) { + extendsFrom( + configurations.getByName(test.implementationConfigurationName), + configurations.getByName(test.runtimeOnlyConfigurationName), + ) + attributes.addAllLater( + configurations.getByName(test.runtimeClasspathConfigurationName).attributes + ) + } + val taskName = "test${name.replaceFirstChar(Char::uppercase)}" + return project.tasks.register(taskName, Test::class.java) { + testClassesDirs = test.output.classesDirs + classpath = test.output + sourceSets.getByName("main").output + runtimeClasspath.get() + useJUnit() + } + } + + private fun runtimeClasspathName(name: String, android: Boolean) = + if (android) "${name}UnitTestRuntimeClasspath" else "${name}TestRuntimeClasspath" + + private fun Configuration.forceGroup(group: String, version: String) { + resolutionStrategy.eachDependency { if (requested.group == group) useVersion(version) } + } + + private fun suffix(version: String) = version.replace('.', '_').replace('-', '_') +} diff --git a/scripts/read-compose-ui-versions.sh b/scripts/read-compose-ui-versions.sh deleted file mode 100755 index 6e20e9e01f..0000000000 --- a/scripts/read-compose-ui-versions.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -# Writes the Compose UI versions from gradle/compose-ui-versions.json to the step's outputs, as a -# JSON list for the Compose UI matrix. -set -eu - -versions=$(jq -c -e 'if type == "array" and length > 0 then . else error("expected a non-empty list") end' \ - gradle/compose-ui-versions.json) -printf 'versions=%s\n' "$versions" >> "$GITHUB_OUTPUT" diff --git a/sentry-android-replay/build.gradle.kts b/sentry-android-replay/build.gradle.kts index a6a82bdebd..d311139d21 100644 --- a/sentry-android-replay/build.gradle.kts +++ b/sentry-android-replay/build.gradle.kts @@ -14,6 +14,7 @@ plugins { id("io.sentry.publish") id("io.sentry.api-validation") id("io.sentry.robolectric") + id("io.sentry.dependency-matrix") } android { @@ -103,16 +104,13 @@ dependencies { } // Session Replay calls Compose internals, whose mangled JVM names change between Compose versions. -// .github/workflows/compose-ui-matrix.yml sets VERSION_COMPOSE_UI to run the unit tests against the -// versions in gradle/compose-ui-versions.json. -System.getenv("VERSION_COMPOSE_UI")?.let { version -> - configurations - .matching { it.name == "releaseUnitTestRuntimeClasspath" } - .configureEach { - resolutionStrategy.eachDependency { - if (requested.group == "androidx.compose.ui") useVersion(version) - } - } +// .github/workflows/compose-ui-matrix.yml runs each version's task. +dependencyMatrix { + matrix( + name = "composeUi", + group = "androidx.compose.ui", + versionsFile = "compose-ui-versions.json", + ) } tasks.withType().configureEach {