From 05138af321b4e39adc7cb98c634a1a9e3950f082 Mon Sep 17 00:00:00 2001 From: Marcel Schnelle Date: Thu, 1 Aug 2024 09:26:55 +0900 Subject: [PATCH] Use correct bundled version of instrumentation libraries for the plugin --- .circleci/config.yml | 18 +++------ build-logic/build.gradle.kts | 6 +++ build-logic/src/main/kotlin/Environment.kt | 2 +- build-logic/src/main/kotlin/Tasks.kt | 39 ++++++++++++++++++- .../java/FindInstrumentationVersionTests.java | 38 ++++++++++++++++++ instrumentation/build.gradle.kts | 15 ------- instrumentation/sample/build.gradle.kts | 5 ++- instrumentation/settings.gradle.kts | 29 ++++++++++++++ plugin/CHANGELOG.md | 1 + plugin/android-junit5/build.gradle.kts | 24 ++---------- 10 files changed, 127 insertions(+), 50 deletions(-) create mode 100644 build-logic/src/test/java/FindInstrumentationVersionTests.java diff --git a/.circleci/config.yml b/.circleci/config.yml index d94686f8..fbcd6d03 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -8,7 +8,7 @@ defaults: &defaults GRADLE_OPTS: -Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.daemon=false -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false cache_key: &cache_key - key: jars-{{ checksum "plugin/build.gradle.kts" }}-{{ checksum "plugin/android-junit5/build.gradle.kts" }}-{{ checksum "plugin/gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "instrumentation/build.gradle.kts" }}-{{ checksum "instrumentation/core/build.gradle.kts" }}-{{ checksum "instrumentation/compose/build.gradle.kts" }}-{{ checksum "instrumentation/extensions/build.gradle.kts" }}-{{ checksum "instrumentation/runner/build.gradle.kts" }}-{{ checksum "instrumentation/sample/build.gradle.kts" }}-{{ checksum "instrumentation/gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "build-logic/src/main/kotlin/Environment.kt" }}-{{ checksum "build-logic/src/main/kotlin/Dependencies.kt" }} + key: jars-{{ checksum "build-logic/build.gradle.kts" }}-{{ checksum "plugin/build.gradle.kts" }}-{{ checksum "plugin/android-junit5/build.gradle.kts" }}-{{ checksum "plugin/gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "instrumentation/build.gradle.kts" }}-{{ checksum "instrumentation/core/build.gradle.kts" }}-{{ checksum "instrumentation/compose/build.gradle.kts" }}-{{ checksum "instrumentation/extensions/build.gradle.kts" }}-{{ checksum "instrumentation/runner/build.gradle.kts" }}-{{ checksum "instrumentation/sample/build.gradle.kts" }}-{{ checksum "instrumentation/gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "build-logic/src/main/kotlin/Environment.kt" }}-{{ checksum "build-logic/src/main/kotlin/Dependencies.kt" }} commands: construct_signing_key: @@ -24,14 +24,12 @@ jobs: - checkout - restore_cache: <<: *cache_key - - run: name: (Plugin) Build command: cd plugin && ./gradlew :android-junit5:assemble --stacktrace - run: name: (Plugin) Test - command: cd plugin && ./gradlew :android-junit5:check --stacktrace - + command: cd plugin && ./gradlew :build-logic:test :android-junit5:check --stacktrace - run: name: (Instrumentation) Download Dependencies command: cd instrumentation && ./gradlew androidDependencies @@ -48,13 +46,11 @@ jobs: - run: name: (Instrumentation) Test command: cd instrumentation && ./gradlew :core:check :extensions:check :runner:check :compose:check --stacktrace - - save_cache: <<: *cache_key paths: - ~/.gradle/caches - ~/.gradle/wrapper - - run: name: Store Google Service Account command: echo $GCLOUD_SERVICE_KEY > ${HOME}/gcloud-service-key.json @@ -63,7 +59,6 @@ jobs: command: | sudo gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json sudo gcloud --quiet config set project ${GOOGLE_PROJECT_ID} - - run: name: Test with Firebase Test Lab command: > @@ -82,7 +77,6 @@ jobs: command: | mkdir test-lab-results sudo gsutil -m cp -r -U `sudo gsutil ls gs://cloud-test-${GOOGLE_PROJECT_ID} | tail -1` test-lab-results/ | true - - persist_to_workspace: root: ~/root paths: @@ -115,14 +109,14 @@ jobs: <<: *cache_key - construct_signing_key - run: - name: (Plugin) Build & Deploy + name: (Instrumentation) Build & Deploy command: | - cd plugin + cd instrumentation ./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository --stacktrace - run: - name: (Instrumentation) Build & Deploy + name: (Plugin) Build & Deploy command: | - cd instrumentation + cd plugin ./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository --stacktrace - store_artifacts: path: plugin/android-junit5/build/publications diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index a6217e0b..85ba6c55 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -1,7 +1,13 @@ plugins { `kotlin-dsl` + java } repositories { mavenCentral() } + +dependencies { + implementation(gradleApi()) + testImplementation("junit:junit:+") +} diff --git a/build-logic/src/main/kotlin/Environment.kt b/build-logic/src/main/kotlin/Environment.kt index e9a3fa86..371ec6a0 100644 --- a/build-logic/src/main/kotlin/Environment.kt +++ b/build-logic/src/main/kotlin/Environment.kt @@ -100,7 +100,7 @@ object Artifacts { object Instrumentation { const val groupId = "de.mannodermaus.junit5" private const val currentVersion = "1.5.1-SNAPSHOT" - const val latestStableVersion = "1.5.0" + private const val latestStableVersion = "1.5.0" val Core = Deployed( platform = Android(minSdk = 19), diff --git a/build-logic/src/main/kotlin/Tasks.kt b/build-logic/src/main/kotlin/Tasks.kt index e92f0d6f..3411ce43 100644 --- a/build-logic/src/main/kotlin/Tasks.kt +++ b/build-logic/src/main/kotlin/Tasks.kt @@ -30,7 +30,7 @@ fun Project.configureTestResources() { "KOTLIN_VERSION" to libs.versions.kotlin, "JUNIT_JUPITER_VERSION" to libs.versions.junitJupiter, - "JUNIT5_ANDROID_LIBS_VERSION" to Artifacts.Instrumentation.latestStableVersion, + "JUNIT5_ANDROID_LIBS_VERSION" to Artifacts.Instrumentation.Core.latestStableVersion, // Collect all supported AGP versions into a single string. // This string is delimited with semicolons, and each of the separated values itself is a 3-tuple. @@ -134,6 +134,43 @@ fun Project.configureTestResources() { } } +fun findInstrumentationVersion( + pluginVersion: String = Artifacts.Plugin.currentVersion, + currentInstrumentationVersion: String = Artifacts.Instrumentation.Core.currentVersion, + stableInstrumentationVersion: String = Artifacts.Instrumentation.Core.latestStableVersion + ): String { + return when { + pluginVersion.endsWith("-SNAPSHOT") -> currentInstrumentationVersion + currentInstrumentationVersion.endsWith("-SNAPSHOT") -> stableInstrumentationVersion + else -> currentInstrumentationVersion + } +} + +fun Copy.configureCreateVersionClassTask( + instrumentationVersion: String = findInstrumentationVersion(), + fromPath: String = "src/main/templates/Libraries.kt", + intoPath: String = "build/generated/sources/plugin/de/mannodermaus", +) { + from(fromPath) + into(intoPath) + filter( + mapOf( + "tokens" to mapOf( + "INSTRUMENTATION_GROUP" to Artifacts.Instrumentation.groupId, + "INSTRUMENTATION_COMPOSE" to Artifacts.Instrumentation.Compose.artifactId, + "INSTRUMENTATION_CORE" to Artifacts.Instrumentation.Core.artifactId, + "INSTRUMENTATION_EXTENSIONS" to Artifacts.Instrumentation.Extensions.artifactId, + "INSTRUMENTATION_RUNNER" to Artifacts.Instrumentation.Runner.artifactId, + + // Find an appropriate version of the instrumentation library, + // depending on the version of how the plugin is configured + "INSTRUMENTATION_VERSION" to instrumentationVersion, + ) + ), ReplaceTokens::class.java + ) + outputs.upToDateWhen { false } +} + /** * Helper Task class for generating an up-to-date version of the project's README.md. * Using a template file, the plugin's version constants & other dependency versions diff --git a/build-logic/src/test/java/FindInstrumentationVersionTests.java b/build-logic/src/test/java/FindInstrumentationVersionTests.java new file mode 100644 index 00000000..5e2cae10 --- /dev/null +++ b/build-logic/src/test/java/FindInstrumentationVersionTests.java @@ -0,0 +1,38 @@ +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class FindInstrumentationVersionTests { + @Test + public void findCorrectVersionForSnapshotPlugin() { + String actual = TasksKt.findInstrumentationVersion( + "plugin-1.0-SNAPSHOT", + "instrumentation-2.0-SNAPSHOT", + "instrumentation-1.0" + ); + + assertEquals("instrumentation-2.0-SNAPSHOT", actual); + } + + @Test + public void findCorrectVersionForStablePluginAndStableInstrumentation() { + String actual = TasksKt.findInstrumentationVersion( + "plugin-1.0", + "instrumentation-2.0", + "instrumentation-1.0" + ); + + assertEquals("instrumentation-2.0", actual); + } + + @Test + public void findCorrectVersionForStablePluginAndSnapshotInstrumentation() { + String actual = TasksKt.findInstrumentationVersion( + "plugin-1.0", + "instrumentation-2.0-SNAPSHOT", + "instrumentation-1.0" + ); + + assertEquals("instrumentation-1.0", actual); + } +} diff --git a/instrumentation/build.gradle.kts b/instrumentation/build.gradle.kts index fc1f0b4e..2e129af3 100644 --- a/instrumentation/build.gradle.kts +++ b/instrumentation/build.gradle.kts @@ -4,13 +4,6 @@ plugins { } buildscript { - repositories { - google() - mavenCentral() - gradlePluginPortal() - jitpack() - } - dependencies { classpath(libs.plugins.kotlin) classpath(libs.plugins.dokka) @@ -18,14 +11,6 @@ buildscript { } } -allprojects { - repositories { - google() - mavenCentral() - sonatypeSnapshots() - } -} - apiValidation { ignoredPackages.add("de.mannodermaus.junit5.internal") ignoredPackages.add("de.mannodermaus.junit5.compose.internal") diff --git a/instrumentation/sample/build.gradle.kts b/instrumentation/sample/build.gradle.kts index 03cf17e3..697dcb82 100644 --- a/instrumentation/sample/build.gradle.kts +++ b/instrumentation/sample/build.gradle.kts @@ -4,7 +4,7 @@ plugins { id("com.android.application") kotlin("android") id("jacoco") - id("de.mannodermaus.android-junit5").version(Artifacts.Plugin.latestStableVersion) + id("de.mannodermaus.android-junit5").version(Artifacts.Plugin.currentVersion) } val javaVersion = JavaVersion.VERSION_11 @@ -22,6 +22,9 @@ android { // Make sure to use the AndroidJUnitRunner (or a sub-class) in order to hook in the JUnit 5 Test Builder testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + + // These two lines are not needed for a normal integration; + // this sample project disables the automatic integration, so it must be done manually testInstrumentationRunnerArguments["runnerBuilder"] = "de.mannodermaus.junit5.AndroidJUnit5Builder" testInstrumentationRunnerArguments["configurationParameters"] = "junit.jupiter.execution.parallel.enabled=true,junit.jupiter.execution.parallel.mode.default=concurrent" diff --git a/instrumentation/settings.gradle.kts b/instrumentation/settings.gradle.kts index a14fc660..15d437d8 100644 --- a/instrumentation/settings.gradle.kts +++ b/instrumentation/settings.gradle.kts @@ -1,4 +1,7 @@ +@file:Suppress("UnstableApiUsage") + rootProject.name = "android-junit5-instrumentation" + includeBuild("../build-logic") include(":core") include(":compose") @@ -7,3 +10,29 @@ include(":runner") include(":sample") include(":testutil") include(":testutil-reflect") + +pluginManagement { + repositories { + google() + mavenCentral() + gradlePluginPortal() + maven { + setUrl("https://jitpack.io") + } + maven { + setUrl("https://oss.sonatype.org/content/repositories/snapshots") + mavenContent { snapshotsOnly() } + } + } +} + +dependencyResolutionManagement { + repositories { + google() + mavenCentral() + maven { + setUrl("https://oss.sonatype.org/content/repositories/snapshots") + mavenContent { snapshotsOnly() } + } + } +} diff --git a/plugin/CHANGELOG.md b/plugin/CHANGELOG.md index 8b1f4d89..83407d8a 100644 --- a/plugin/CHANGELOG.md +++ b/plugin/CHANGELOG.md @@ -6,6 +6,7 @@ Change Log - Updates to the `jacocoOptions` DSL - Change the return type of each report type to match Jacoco expectations (html -> Directory; csv & xml -> File) - Turn off generation of csv & xml reports by default, matching Jacoco default configuration +- Fix: Use the correct version of the instrumentation libraries with the plugin (#345) ## 1.10.2.0 (2024-07-25) - JUnit 5.10.2 diff --git a/plugin/android-junit5/build.gradle.kts b/plugin/android-junit5/build.gradle.kts index fed0c14d..0ce841ba 100644 --- a/plugin/android-junit5/build.gradle.kts +++ b/plugin/android-junit5/build.gradle.kts @@ -1,8 +1,5 @@ -import org.apache.tools.ant.filters.ReplaceTokens -import org.gradle.api.internal.classpath.ModuleRegistry import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestLogEvent -import org.gradle.kotlin.dsl.support.serviceOf import org.jetbrains.dokka.gradle.DokkaTask import org.jetbrains.kotlin.gradle.tasks.KotlinCompile @@ -81,23 +78,10 @@ project.configureTestResources() // Generate a file with the latest versions of the plugin & instrumentation val genFolder = "build/generated/sources/plugin" -val versionClassTask = tasks.register("createVersionClass") { - from("src/main/templates/Libraries.kt") - into("$genFolder/de/mannodermaus") - filter( - mapOf( - "tokens" to mapOf( - "INSTRUMENTATION_GROUP" to Artifacts.Instrumentation.groupId, - "INSTRUMENTATION_COMPOSE" to Artifacts.Instrumentation.Compose.artifactId, - "INSTRUMENTATION_CORE" to Artifacts.Instrumentation.Core.artifactId, - "INSTRUMENTATION_EXTENSIONS" to Artifacts.Instrumentation.Extensions.artifactId, - "INSTRUMENTATION_RUNNER" to Artifacts.Instrumentation.Runner.artifactId, - "INSTRUMENTATION_VERSION" to Artifacts.Instrumentation.latestStableVersion, - ) - ), ReplaceTokens::class.java - ) - outputs.upToDateWhen { false } -} +val versionClassTask = tasks.register( + "createVersionClass", + Copy::configureCreateVersionClassTask, +) sourceSets { main { kotlin.srcDir(genFolder)