From ed63e4e7dd7f355de878032e671ba681bd27e3ca Mon Sep 17 00:00:00 2001 From: Marcel Schnelle Date: Tue, 9 Apr 2024 15:21:48 +0200 Subject: [PATCH] Use Provider API for Jacoco & instrumentation test DSLs in the plugin (#321) --- plugin/CHANGELOG.md | 2 + plugin/android-junit5/api/android-junit5.api | 28 +++-------- plugin/android-junit5/build.gradle.kts | 1 + .../dsl/AndroidJUnitPlatformExtension.kt | 25 ++++++++-- .../junit5/dsl/InstrumentationTestOptions.kt | 39 +++------------ .../plugins/junit5/dsl/JacocoOptions.kt | 50 +++++++------------ .../junit5/internal/configureJUnit5.kt | 21 +++++--- .../junit5/tasks/AndroidJUnit5JacocoReport.kt | 4 +- .../src/main/templates/Libraries.kt | 6 ++- .../junit5/plugin/AgpJacocoBaseTests.kt | 16 +++--- .../plugin/AgpJacocoExclusionRuleTests.kt | 5 +- .../junit5/plugin/AgpJacocoVariantTests.kt | 11 ++-- .../plugin/InstrumentationSupportTests.kt | 18 +++++-- .../test-projects/build.gradle.kts.template | 33 +++++------- 14 files changed, 121 insertions(+), 138 deletions(-) diff --git a/plugin/CHANGELOG.md b/plugin/CHANGELOG.md index e98a0aa7..491fcbb3 100644 --- a/plugin/CHANGELOG.md +++ b/plugin/CHANGELOG.md @@ -5,6 +5,8 @@ Change Log - JUnit 5.10.2 - Raise minimum supported versions for AGP and Gradle to 8.0.x and 8.0, respectively - Allow overriding the version of the instrumentation libraries applied with the plugin +- Update Jacoco & instrumentation test DSLs of the plugin to use Gradle Providers for their input parameters (e.g. `instrumentationTests.enabled.set(true)` instead of `instrumentationTests.enabled = true`) +- Removed deprecated `integrityCheckEnabled` flag from the plugin DSL's instrumentation test options ## 1.10.0.0 (2023-11-05) - JUnit 5.10.0 diff --git a/plugin/android-junit5/api/android-junit5.api b/plugin/android-junit5/api/android-junit5.api index f7fc1982..e37b6f8f 100644 --- a/plugin/android-junit5/api/android-junit5.api +++ b/plugin/android-junit5/api/android-junit5.api @@ -32,38 +32,26 @@ public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/FiltersExtension public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/InstrumentationTestOptions { public fun ()V - public final fun enabled (Z)V - public final fun getEnabled ()Z - public final fun getIntegrityCheckEnabled ()Z - public final fun integrityCheckEnabled (Z)V - public final fun setEnabled (Z)V - public final fun setIntegrityCheckEnabled (Z)V + public abstract fun getEnabled ()Lorg/gradle/api/provider/Property; + public abstract fun getIncludeExtensions ()Lorg/gradle/api/provider/Property; + public abstract fun getVersion ()Lorg/gradle/api/provider/Property; } public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions { public fun (Lorg/gradle/api/model/ObjectFactory;)V - public final fun excludedClasses ([Ljava/lang/String;)V public final fun getCsv ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report; - public final fun getExcludedClasses ()Ljava/util/List; + public abstract fun getExcludedClasses ()Lorg/gradle/api/provider/ListProperty; public final fun getHtml ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report; - public final fun getOnlyGenerateTasksForVariants ()Ljava/util/Set; - public final fun getTaskGenerationEnabled ()Z + public abstract fun getOnlyGenerateTasksForVariants ()Lorg/gradle/api/provider/SetProperty; + public abstract fun getTaskGenerationEnabled ()Lorg/gradle/api/provider/Property; public final fun getXml ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report; - public final fun onlyGenerateTasksForVariants ([Ljava/lang/String;)V - public final fun setExcludedClasses (Ljava/util/List;)V - public final fun setTaskGenerationEnabled (Z)V - public final fun taskGenerationEnabled (Z)V } public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report { public fun ()V - public final fun destination (Ljava/io/File;)V - public final fun enabled (Z)V - public final fun getDestination ()Ljava/io/File; - public final fun getEnabled ()Z + public abstract fun getDestination ()Lorg/gradle/api/file/RegularFileProperty; + public abstract fun getEnabled ()Lorg/gradle/api/provider/Property; public final fun invoke (Lkotlin/jvm/functions/Function1;)V - public final fun setDestination (Ljava/io/File;)V - public final fun setEnabled (Z)V } public abstract class de/mannodermaus/gradle/plugins/junit5/tasks/AndroidJUnit5JacocoReport : org/gradle/testing/jacoco/tasks/JacocoReport { diff --git a/plugin/android-junit5/build.gradle.kts b/plugin/android-junit5/build.gradle.kts index 675f87f9..de02d9e2 100644 --- a/plugin/android-junit5/build.gradle.kts +++ b/plugin/android-junit5/build.gradle.kts @@ -87,6 +87,7 @@ val versionClassTask = tasks.register("createVersionClass") { "tokens" to mapOf( "INSTRUMENTATION_GROUP" to Artifacts.Instrumentation.groupId, "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, ) diff --git a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/AndroidJUnitPlatformExtension.kt b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/AndroidJUnitPlatformExtension.kt index c0d22188..1e07fbeb 100644 --- a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/AndroidJUnitPlatformExtension.kt +++ b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/AndroidJUnitPlatformExtension.kt @@ -1,5 +1,6 @@ package de.mannodermaus.gradle.plugins.junit5.dsl +import de.mannodermaus.Libraries import de.mannodermaus.gradle.plugins.junit5.internal.config.EXTENSION_NAME import groovy.lang.Closure import groovy.lang.GroovyObjectSupport @@ -9,6 +10,7 @@ import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.MapProperty import org.gradle.api.tasks.Input import org.junit.platform.commons.util.Preconditions +import java.io.File import javax.inject.Inject public abstract class AndroidJUnitPlatformExtension @Inject constructor( @@ -16,7 +18,7 @@ public abstract class AndroidJUnitPlatformExtension @Inject constructor( ) : GroovyObjectSupport() { internal companion object { - fun Project.createJUnit5Extension() = + fun Project.createJUnit5Extension(): AndroidJUnitPlatformExtension = extensions.create(EXTENSION_NAME, AndroidJUnitPlatformExtension::class.java) } @@ -85,14 +87,18 @@ public abstract class AndroidJUnitPlatformExtension @Inject constructor( return null } - + /* Android Instrumentation Test support */ /** * Options for controlling instrumentation test execution with JUnit 5 */ public val instrumentationTests: InstrumentationTestOptions = - objects.newInstance(InstrumentationTestOptions::class.java) + objects.newInstance(InstrumentationTestOptions::class.java).apply { + enabled.convention(true) + version.convention(Libraries.instrumentationVersion) + includeExtensions.convention(false) + } public fun instrumentationTests(action: Action) { action.execute(instrumentationTests) @@ -103,8 +109,19 @@ public abstract class AndroidJUnitPlatformExtension @Inject constructor( /** * Options for controlling Jacoco reporting */ + @Suppress("CAST_NEVER_SUCCEEDS") public val jacocoOptions: JacocoOptions = - objects.newInstance(JacocoOptions::class.java) + objects.newInstance(JacocoOptions::class.java).apply { + taskGenerationEnabled.convention(true) + onlyGenerateTasksForVariants.convention(emptySet()) + excludedClasses.set(listOf("**/R.class", "**/R$*.class", "**/BuildConfig.*")) + html.enabled.convention(true) + html.destination.set(null as? File) + csv.enabled.convention(true) + csv.destination.set(null as? File) + xml.enabled.convention(true) + xml.destination.set(null as? File) + } public fun jacocoOptions(action: Action) { action.execute(jacocoOptions) diff --git a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/InstrumentationTestOptions.kt b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/InstrumentationTestOptions.kt index 17c07acf..144593a5 100644 --- a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/InstrumentationTestOptions.kt +++ b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/InstrumentationTestOptions.kt @@ -1,5 +1,6 @@ package de.mannodermaus.gradle.plugins.junit5.dsl +import org.gradle.api.provider.Property import org.gradle.api.tasks.Input /** @@ -12,42 +13,18 @@ public abstract class InstrumentationTestOptions { * when junit-jupiter-api is added as an androidTestImplementation dependency. */ @get:Input - public var enabled: Boolean = true + public abstract val enabled: Property /** - * Whether to configure JUnit 5 instrumentation tests automatically - * when junit-jupiter-api is added as an androidTestImplementation dependency. - */ - public fun enabled(state: Boolean) { - this.enabled = state - } - - /** - * Whether to check if the instrumentation tests - * are correctly set up. If this is disabled, the plugin - * won't raise an error during evaluation if the instrumentation - * libraries or the test runner are missing. + * The version of the instrumentation libraries to autoconfigure. */ @get:Input - @Deprecated( - message = "Starting with android-junit5 1.9.0.0, instrumentation tests are automatically configured correctly " + - "when the junit-jupiter-api dependency is added as an androidTestImplementation dependency; " + - "this flag no longer does anything and can be safely removed. " + - "If you don't want to automatically configure JUnit 5 instrumentation tests, use the `enabled` flag." - ) - public var integrityCheckEnabled: Boolean = false + public abstract val version: Property /** - * Whether to check if the instrumentation tests - * are correctly set up. If this is disabled, the plugin - * won't raise an error during evaluation if the instrumentation - * libraries or the test runner are missing. + * Whether to include a dependency on the android-test-extensions library + * on top of the main instrumentation artifacts. */ - @Deprecated( - message = "Starting with android-junit5 1.9.0.0, instrumentation tests are automatically configured correctly " + - "when the junit-jupiter-api dependency is added as an androidTestImplementation dependency; " + - "this flag no longer does anything and can be safely removed. " + - "If you don't want to automatically configure JUnit 5 instrumentation tests, use the `enabled` flag." - ) - public fun integrityCheckEnabled(state: Boolean) {} + @get:Input + public abstract val includeExtensions: Property } diff --git a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions.kt b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions.kt index 444c345d..6b9395fd 100644 --- a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions.kt +++ b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions.kt @@ -1,8 +1,12 @@ package de.mannodermaus.gradle.plugins.junit5.dsl +import org.gradle.api.file.RegularFileProperty import org.gradle.api.model.ObjectFactory +import org.gradle.api.provider.ListProperty +import org.gradle.api.provider.Property +import org.gradle.api.provider.SetProperty import org.gradle.api.tasks.Input -import java.io.File +import org.gradle.internal.enterprise.test.FileProperty import javax.inject.Inject /** @@ -16,27 +20,17 @@ public abstract class JacocoOptions @Inject constructor( * Whether to enable Jacoco task integration */ @get:Input - public var taskGenerationEnabled: Boolean = true - - public fun taskGenerationEnabled(state: Boolean) { - this.taskGenerationEnabled = state - } - - private val _onlyGenerateTasksForVariants = mutableSetOf() - - @get:Input - public val onlyGenerateTasksForVariants: Set - get() = _onlyGenerateTasksForVariants + public abstract val taskGenerationEnabled: Property /** * Filter the generated Jacoco tasks, * so that only the given build variants are provided with a companion task. * Make sure to add the full product flavor name if necessary - * (i.e. "paidDebug" if you use a "paid" product flavor and the "debug" build type) + * (i.e. "paidDebug" if you use a "paid" product flavor and the "debug" build type). + * By default, this set is empty, meaning that tasks are generated for all variants. */ - public fun onlyGenerateTasksForVariants(vararg variants: String) { - _onlyGenerateTasksForVariants.addAll(variants) - } + @get:Input + public abstract val onlyGenerateTasksForVariants: SetProperty /** * Options for controlling the HTML Report generated by Jacoco @@ -57,12 +51,8 @@ public abstract class JacocoOptions @Inject constructor( * List of class name patterns that should be excluded from being processed by Jacoco. * By default, this will exclude R.class & BuildConfig.class */ - public var excludedClasses: MutableList = - mutableListOf("**/R.class", "**/R$*.class", "**/BuildConfig.*") - - public fun excludedClasses(vararg classes: String) { - excludedClasses.addAll(classes) - } + @get:Input + public abstract val excludedClasses: ListProperty public abstract class Report { @@ -71,23 +61,17 @@ public abstract class JacocoOptions @Inject constructor( } /** - * Whether or not this report should be generated + * Whether this report should be generated */ - public var enabled: Boolean = true - - public fun enabled(state: Boolean) { - this.enabled = state - } + @get:Input + public abstract val enabled: Property /** * Name of the file to be generated; note that * due to the variant-aware nature of the plugin, * each variant will be assigned a distinct folder if necessary */ - public var destination: File? = null - - public fun destination(file: File?) { - this.destination = file - } + @get:Input + public abstract val destination: RegularFileProperty } } diff --git a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/internal/configureJUnit5.kt b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/internal/configureJUnit5.kt index 77cb455c..42bca48e 100644 --- a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/internal/configureJUnit5.kt +++ b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/internal/configureJUnit5.kt @@ -106,7 +106,7 @@ private fun prepareUnitTests(project: Project, android: AndroidExtension) { } private fun AndroidJUnitPlatformExtension.prepareInstrumentationTests(project: Project, android: AndroidExtension) { - if (!instrumentationTests.enabled) return + if (!instrumentationTests.enabled.get()) return // Automatically configure instrumentation tests when JUnit 5 is detected in that configuration val hasJupiterApi = project.configurations @@ -124,14 +124,23 @@ private fun AndroidJUnitPlatformExtension.prepareInstrumentationTests(project: P .joinToString(",") } + val version = instrumentationTests.version.get() + project.dependencies.add( "androidTestImplementation", - Libraries.instrumentationCore + "${Libraries.instrumentationCore}:$version" ) project.dependencies.add( "androidTestRuntimeOnly", - Libraries.instrumentationRunner + "${Libraries.instrumentationRunner}:$version" ) + + if (instrumentationTests.includeExtensions.get()) { + project.dependencies.add( + "androidTestImplementation", + "${Libraries.instrumentationExtensions}:$version" + ) + } } private fun AndroidJUnitPlatformExtension.configureUnitTests(project: Project, variant: Variant) { @@ -163,11 +172,11 @@ private fun AndroidJUnitPlatformExtension.configureJacoco( variant: Variant ) { // Connect a Code Coverage report to it if Jacoco is enabled - if (jacocoOptions.taskGenerationEnabled && config.hasJacocoPlugin) { + if (jacocoOptions.taskGenerationEnabled.get() && config.hasJacocoPlugin) { val taskName = variant.getTaskName(prefix = UNIT_TEST_PREFIX, suffix = UNIT_TEST_SUFFIX) project.tasks.namedOrNull(taskName)?.get()?.let { testTask -> // Create a Jacoco friend task - val enabledVariants = jacocoOptions.onlyGenerateTasksForVariants + val enabledVariants = jacocoOptions.onlyGenerateTasksForVariants.get() if (enabledVariants.isEmpty() || variant.name in enabledVariants) { val directoryProviders = config.directoryProvidersOf(variant) val registered = AndroidJUnit5JacocoReport.register(project, variant, testTask, directoryProviders) @@ -184,7 +193,7 @@ private fun AndroidJUnitPlatformExtension.configureInstrumentationTests( config: PluginConfig, variant: Variant, ) { - if (!instrumentationTests.enabled) return + if (!instrumentationTests.enabled.get()) return config.instrumentationTestVariantOf(variant)?.let { instrumentationTestVariant -> AndroidJUnit5WriteFilters.register(project, variant, instrumentationTestVariant) diff --git a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/tasks/AndroidJUnit5JacocoReport.kt b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/tasks/AndroidJUnit5JacocoReport.kt index 51da0cc9..ebf23dd1 100644 --- a/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/tasks/AndroidJUnit5JacocoReport.kt +++ b/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/tasks/AndroidJUnit5JacocoReport.kt @@ -98,7 +98,7 @@ public abstract class AndroidJUnit5JacocoReport : JacocoReport() { allReports.forEach { (from, to) -> to.required.set(from.enabled) - from.destination?.let { to.outputLocationFile.set(it) } + to.outputLocationFile.fileProvider(from.destination.asFile) } // Task-level Configuration @@ -110,7 +110,7 @@ public abstract class AndroidJUnit5JacocoReport : JacocoReport() { // Apply exclusion rules to both class & source directories for Jacoco, // using the sum of all DirectoryProviders' outputs as a foundation: reportTask.classDirectories.setFrom( - directoryProviders.mainClassDirectories().toFileCollectionExcluding(junit5Jacoco.excludedClasses) + directoryProviders.mainClassDirectories().toFileCollectionExcluding(junit5Jacoco.excludedClasses.get()) ) reportTask.sourceDirectories.setFrom( directoryProviders.mainSourceDirectories() diff --git a/plugin/android-junit5/src/main/templates/Libraries.kt b/plugin/android-junit5/src/main/templates/Libraries.kt index f31a03b6..6986fe57 100644 --- a/plugin/android-junit5/src/main/templates/Libraries.kt +++ b/plugin/android-junit5/src/main/templates/Libraries.kt @@ -1,6 +1,8 @@ package de.mannodermaus internal object Libraries { - const val instrumentationCore = "@INSTRUMENTATION_GROUP@:@INSTRUMENTATION_CORE@:@INSTRUMENTATION_VERSION@" - const val instrumentationRunner = "@INSTRUMENTATION_GROUP@:@INSTRUMENTATION_RUNNER@:@INSTRUMENTATION_VERSION@" + const val instrumentationVersion = "@INSTRUMENTATION_VERSION@" + const val instrumentationCore = "@INSTRUMENTATION_GROUP@:@INSTRUMENTATION_CORE@" + const val instrumentationExtensions = "@INSTRUMENTATION_GROUP@:@INSTRUMENTATION_EXTENSIONS@" + const val instrumentationRunner = "@INSTRUMENTATION_GROUP@:@INSTRUMENTATION_RUNNER@" } diff --git a/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoBaseTests.kt b/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoBaseTests.kt index 3eab842f..56e86b5b 100644 --- a/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoBaseTests.kt +++ b/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoBaseTests.kt @@ -47,7 +47,7 @@ interface AgpJacocoBaseTests : AgpVariantAwareTests { fun `acknowledge disabling of jacoco task generation`(): List { val project = createProject().applyJacocoPlugin().build() project.junitPlatform.jacocoOptions { - it.taskGenerationEnabled = false + it.taskGenerationEnabled.set(false) } project.evaluate() @@ -65,9 +65,9 @@ interface AgpJacocoBaseTests : AgpVariantAwareTests { fun `acknowledge custom report folders`() { val project = createProject().applyJacocoPlugin().build() project.junitPlatform.jacocoOptions { - it.xml.destination(project.file("build/other-jacoco-folder/xml")) - it.csv.destination(project.file("build/CSVISDABEST")) - it.html.destination(project.file("build/html-reports/jacoco")) + it.xml.destination.set(project.file("build/other-jacoco-folder/xml")) + it.csv.destination.set(project.file("build/CSVISDABEST")) + it.html.destination.set(project.file("build/html-reports/jacoco")) } project.evaluate() @@ -87,9 +87,9 @@ interface AgpJacocoBaseTests : AgpVariantAwareTests { fun `acknowledge status of report tasks`(required: Boolean) { val project = createProject().applyJacocoPlugin().build() project.junitPlatform.jacocoOptions { - it.xml.enabled(required) - it.csv.enabled(required) - it.html.enabled(required) + it.xml.enabled.set(required) + it.csv.enabled.set(required) + it.html.enabled.set(required) } project.evaluate() @@ -107,5 +107,5 @@ interface AgpJacocoBaseTests : AgpVariantAwareTests { /* Private */ private val ConfigurableReport.outputLocationFilePath get() = - outputLocationFile?.asFile?.get()?.absolutePath + outputLocationFile.asFile.orNull?.absolutePath } diff --git a/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoExclusionRuleTests.kt b/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoExclusionRuleTests.kt index 8fc804b6..587b5b09 100644 --- a/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoExclusionRuleTests.kt +++ b/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoExclusionRuleTests.kt @@ -7,6 +7,7 @@ import de.mannodermaus.gradle.plugins.junit5.tasks.JACOCO_TASK_NAME import de.mannodermaus.gradle.plugins.junit5.util.evaluate import de.mannodermaus.gradle.plugins.junit5.util.get import org.gradle.api.Project +import org.gradle.configurationcache.extensions.capitalized import org.junit.jupiter.api.DynamicTest import org.junit.jupiter.api.DynamicTest.dynamicTest import org.junit.jupiter.api.TestFactory @@ -76,11 +77,11 @@ interface AgpJacocoExclusionRuleTests : AgpVariantAwareTests { beforeEvaluate = { project -> project.createFakeFiles() project.junitPlatform.jacocoOptions { - it.excludedClasses = mutableListOf() + it.excludedClasses.set(emptyList()) } } ) { project, buildType -> - val name = "${JACOCO_TASK_NAME}${buildType.capitalize()}" + val name = "${JACOCO_TASK_NAME}${buildType.capitalized()}" val fileNames = project.tasks.get(name) .classDirectories!!.asFileTree.files .map { it.name } diff --git a/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoVariantTests.kt b/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoVariantTests.kt index 159fcd45..82936ef5 100644 --- a/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoVariantTests.kt +++ b/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/AgpJacocoVariantTests.kt @@ -10,6 +10,7 @@ import de.mannodermaus.gradle.plugins.junit5.util.assertAll import de.mannodermaus.gradle.plugins.junit5.util.evaluate import de.mannodermaus.gradle.plugins.junit5.util.get import de.mannodermaus.gradle.plugins.junit5.util.getDependentTaskNames +import org.gradle.configurationcache.extensions.capitalized import org.gradle.testing.jacoco.tasks.JacocoReport import org.junit.jupiter.api.DynamicTest import org.junit.jupiter.api.DynamicTest.dynamicTest @@ -45,7 +46,7 @@ interface AgpJacocoVariantTests : AgpVariantAwareTests { }, beforeEvaluate = { it.junitPlatform.jacocoOptions { - it.taskGenerationEnabled = false + it.taskGenerationEnabled.set(false) } } ) { project, buildType -> @@ -66,7 +67,7 @@ interface AgpJacocoVariantTests : AgpVariantAwareTests { task.group = "TEST MARKER" } it.junitPlatform.jacocoOptions { - it.taskGenerationEnabled = false + it.taskGenerationEnabled.set(false) } } ) { project, buildType -> @@ -142,7 +143,7 @@ interface AgpJacocoVariantTests : AgpVariantAwareTests { "Mismatch! Actual dirs: $sourceDirs", { assertThat(sourceDirs).doesNotContain("src/test/java") }, { - assertThat(sourceDirs).doesNotContain("src/test${buildType.capitalize()}/java") + assertThat(sourceDirs).doesNotContain("src/test${buildType.capitalized()}/java") } ) } @@ -163,7 +164,7 @@ interface AgpJacocoVariantTests : AgpVariantAwareTests { fun `only generate jacoco task for debug builds`(): List { val project = createProject().applyJacocoPlugin().build() project.junitPlatform.jacocoOptions { - it.onlyGenerateTasksForVariants("debug") + it.onlyGenerateTasksForVariants.add("debug") } project.evaluate() @@ -189,7 +190,7 @@ interface AgpJacocoVariantTests : AgpVariantAwareTests { val project = createProject().applyJacocoPlugin().build() project.registerProductFlavors() project.junitPlatform.jacocoOptions { - it.onlyGenerateTasksForVariants("paidDebug", "freeRelease") + it.onlyGenerateTasksForVariants.addAll("paidDebug", "freeRelease") } project.evaluate() diff --git a/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/InstrumentationSupportTests.kt b/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/InstrumentationSupportTests.kt index 0c9761ba..95031bdf 100644 --- a/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/InstrumentationSupportTests.kt +++ b/plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/InstrumentationSupportTests.kt @@ -51,7 +51,7 @@ class InstrumentationSupportTests { @Test fun `do not add the RunnerBuilder when disabled`() { project.addJUnitJupiterApi() - project.junitPlatform.instrumentationTests.enabled = false + project.junitPlatform.instrumentationTests.enabled.set(false) project.evaluate() assertThat(project.android.defaultConfig.testInstrumentationRunnerArguments["runnerBuilder"]).isNull() @@ -72,9 +72,19 @@ class InstrumentationSupportTests { project.evaluate() assertThat(project.dependencyNamed("androidTestImplementation", "android-test-core")) - .isEqualTo(Libraries.instrumentationCore) + .isEqualTo("${Libraries.instrumentationCore}:${Libraries.instrumentationVersion}") assertThat(project.dependencyNamed("androidTestRuntimeOnly", "android-test-runner")) - .isEqualTo(Libraries.instrumentationRunner) + .isEqualTo("${Libraries.instrumentationRunner}:${Libraries.instrumentationVersion}") + } + + @Test + fun `allow overriding the version`() { + project.addJUnitJupiterApi() + project.junitPlatform.instrumentationTests.version.set("1.3.3.7") + project.evaluate() + + assertThat(project.dependencyNamed("androidTestImplementation", "android-test-core")).endsWith("1.3.3.7") + assertThat(project.dependencyNamed("androidTestRuntimeOnly", "android-test-runner")).endsWith("1.3.3.7") } @Test @@ -93,7 +103,7 @@ class InstrumentationSupportTests { @Test fun `do not add the dependencies when disabled`() { project.addJUnitJupiterApi() - project.junitPlatform.instrumentationTests.enabled = false + project.junitPlatform.instrumentationTests.enabled.set(false) project.evaluate() assertThat(project.dependencyNamed("androidTestImplementation", "android-test-core")).isNull() diff --git a/plugin/android-junit5/src/test/resources/test-projects/build.gradle.kts.template b/plugin/android-junit5/src/test/resources/test-projects/build.gradle.kts.template index 9dd1cf5d..ae33e50c 100644 --- a/plugin/android-junit5/src/test/resources/test-projects/build.gradle.kts.template +++ b/plugin/android-junit5/src/test/resources/test-projects/build.gradle.kts.template @@ -55,9 +55,7 @@ android { compileSdkVersion("${compileSdk}") defaultConfig { - {% if atLeastAgp("8.0.0") %} - namespace = "de.mannodermaus.app" - {% endif %} + namespace = "de.mannodermaus.app" minSdkVersion(minSdk) targetSdkVersion(targetSdk) @@ -74,20 +72,17 @@ android { {% endif %} } - // Enforce Java 17 for AGP 8 and above - {% if atLeastAgp("8.0.0") %} - val javaVersion = JavaVersion.VERSION_17 + val javaVersion = JavaVersion.VERSION_17 - compileOptions { - sourceCompatibility = javaVersion - targetCompatibility = javaVersion - } + compileOptions { + sourceCompatibility = javaVersion + targetCompatibility = javaVersion + } - {% if USE_KOTLIN %} - tasks.withType { - kotlinOptions.jvmTarget = javaVersion.toString() - } - {% endif %} + {% if USE_KOTLIN %} + tasks.withType { + kotlinOptions.jvmTarget = javaVersion.toString() + } {% endif %} {% if USE_FLAVORS %} @@ -121,15 +116,11 @@ android { } junitPlatform { - {% if INCLUDE_ANDROID_RESOURCES %} - instrumentationTests.integrityCheckEnabled = false - {% endif %} - {% if USE_JACOCO %} jacocoOptions { html { - enabled = true - destination = layout.buildDirectory.file("reports/jacoco/${name}.html").get().asFile + enabled.set(true) + destination.set(layout.buildDirectory.file("reports/jacoco/${name}.html")) } } {% endif %}