diff --git a/.github/workflows/compose-ui-matrix.yml b/.github/workflows/compose-ui-matrix.yml new file mode 100644 index 0000000000..78c42cd24f --- /dev/null +++ b/.github/workflows/compose-ui-matrix.yml @@ -0,0 +1,77 @@ +name: Compose UI Matrix + +on: + 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: gradle + + - name: Read Compose UI versions + id: versions + run: echo "versions=$(jq -c . gradle/compose-ui-versions.json)" >> "$GITHUB_OUTPUT" + + 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: + COMPOSE_UI_VERSION: ${{ 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:composeUiTest${COMPOSE_UI_VERSION//[.-]/_}" + + - 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/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/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/sentry-android-replay/build.gradle.kts b/sentry-android-replay/build.gradle.kts index d0d101d190..d311139d21 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,14 @@ 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. +dependencyMatrix { + matrix( + name = "composeUi", + group = "androidx.compose.ui", + versionsFile = "compose-ui-versions.json", + ) } tasks.withType().configureEach {