From 528e3c78213d0f6d0880945a314e183e005fd1de Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 21 Sep 2022 16:18:48 +0530 Subject: [PATCH] feat(build-logic): port `slimTests` from APS --- .github/workflows/ci.yml | 2 +- .../aps/gradle/AndroidCommonPlugin.kt | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8bfaf036..7b92b241 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,7 @@ jobs: - name: Run unit tests uses: gradle/gradle-build-action@v2.3.0 with: - arguments: check --stacktrace --no-configuration-cache + arguments: check -PslimTests --stacktrace --no-configuration-cache gradle-home-cache-cleanup: true - name: (Fail-only) Upload test report diff --git a/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/AndroidCommonPlugin.kt b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/AndroidCommonPlugin.kt index e5006b9c..2ff5f85b 100644 --- a/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/AndroidCommonPlugin.kt +++ b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/AndroidCommonPlugin.kt @@ -1,6 +1,8 @@ package dev.msfjarvis.aps.gradle import com.android.build.api.dsl.TestExtension +import com.android.build.api.variant.ApplicationAndroidComponentsExtension +import com.android.build.api.variant.LibraryAndroidComponentsExtension import com.android.build.gradle.TestedExtension import org.gradle.android.AndroidCacheFixPlugin import org.gradle.api.JavaVersion @@ -9,9 +11,12 @@ import org.gradle.api.Project import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.findByType +private const val SLIM_TESTS_PROPERTY = "slimTests" + @Suppress("UnstableApiUsage") class AndroidCommonPlugin : Plugin { override fun apply(project: Project) { + project.configureSlimTests() project.pluginManager.apply(AndroidCacheFixPlugin::class) project.extensions.findByType()?.run { setCompileSdkVersion(33) @@ -63,3 +68,31 @@ class AndroidCommonPlugin : Plugin { } } } + +/** + * When the "slimTests" project property is provided, disable the unit test tasks on `release` build + * type and `nonFree` product flavor to avoid running the same tests repeatedly in different build + * variants. + * + * Examples: `./gradlew test -PslimTests` will run unit tests for `nonFreeDebug` and `debug` build + * variants in Android App and Library projects, and all tests in JVM projects. + */ +internal fun Project.configureSlimTests() { + if (providers.gradleProperty(SLIM_TESTS_PROPERTY).isPresent) { + // Disable unit test tasks on the release build type for Android Library projects + extensions.findByType()?.run { + beforeVariants(selector().withBuildType("release")) { + it.enableUnitTest = false + it.enableAndroidTest = false + } + } + + // Disable unit test tasks on the release build type for Android Application projects. + extensions.findByType()?.run { + beforeVariants(selector().withBuildType("release")) { + it.enableUnitTest = false + it.enableAndroidTest = false + } + } + } +}