From ce3420db25e23cac43b06e84e92690b5d0385205 Mon Sep 17 00:00:00 2001 From: Sinan Kozak Date: Fri, 9 Feb 2024 14:41:14 +0100 Subject: [PATCH 1/4] Set up dependOn task only if project is affected --- .../AffectedModuleDetectorPlugin.kt | 24 ++++++++++--------- .../AffectedModuleDetectorIntegrationTest.kt | 8 +++---- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt index fd6cc2b5..ee88a9c0 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt @@ -159,7 +159,7 @@ class AffectedModuleDetectorPlugin : Plugin { private fun withPlugin( pluginId: String, - task: Task, + taskToConfigure: Task, testType: AffectedModuleTaskType, project: Project ) { @@ -171,16 +171,18 @@ class AffectedModuleDetectorPlugin : Plugin { project.pluginManager.withPlugin(pluginId) { getAffectedPath(testType, project)?.let { path -> - val pathOrNull = project.tasks.findByPath(path) - if (AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path) && pathOrNull != null) { - task.dependsOn(path) - } - - pathOrNull?.onlyIf { task -> - when { - !AffectedModuleDetector.isProjectEnabled(task.project) -> true - else -> AffectedModuleDetector.isProjectAffected(task.project) - } + project + .tasks + .findByPath(path) + ?.onlyIf { task -> + when { + !AffectedModuleDetector.isProjectEnabled(task.project) -> true + else -> AffectedModuleDetector.isProjectAffected(task.project) + }.also { onlyIf -> + if (onlyIf && AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) { + taskToConfigure.dependsOn(path) + } + } } } } diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt index 13006d11..97a47150 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt @@ -111,8 +111,8 @@ class AffectedModuleDetectorIntegrationTest { .build() // THEN - assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED") - assertThat(result.output).contains(":sample-core:assembleAndroidTest SKIPPED") + assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest SKIPPED") + assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest SKIPPED") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } @@ -189,8 +189,8 @@ class AffectedModuleDetectorIntegrationTest { .build() // THEN - assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED") + assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest SKIPPED") assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest SKIPPED") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } -} \ No newline at end of file +} From 22d372e4c9f48e3927637b190d86c5410799cb74 Mon Sep 17 00:00:00 2001 From: Sinan Kozak Date: Fri, 9 Feb 2024 14:52:38 +0100 Subject: [PATCH 2/4] cover other task states in test --- .../AffectedModuleDetectorIntegrationTest.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt index 97a47150..51c38f71 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt @@ -111,8 +111,8 @@ class AffectedModuleDetectorIntegrationTest { .build() // THEN - assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest SKIPPED") - assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest SKIPPED") + assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest") + assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } @@ -189,8 +189,8 @@ class AffectedModuleDetectorIntegrationTest { .build() // THEN - assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest SKIPPED") - assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest SKIPPED") + assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest") + assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } } From 3871eec80858ccf05769513501f4410a7432e3aa Mon Sep 17 00:00:00 2001 From: Sinan Kozak Date: Sat, 10 Feb 2024 14:04:31 +0100 Subject: [PATCH 3/4] Improve the code --- .../AffectedModuleDetectorPlugin.kt | 35 ++++++++++--------- .../AffectedModuleDetectorIntegrationTest.kt | 6 ++-- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt index ee88a9c0..f8771ae1 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt @@ -31,7 +31,7 @@ import org.gradle.util.GradleVersion * logFolder = "${project.rootDir}". * } * - * To enable affected module detection, you need to pass [ENABLE_ARG] + * To enable affected module detection, you need to pass [com.dropbox.affectedmoduledetector.AffectedModuleDetector.Companion.ENABLE_ARG] * into the build as a command line parameter. * * See [AffectedModuleDetector] for additional flags. @@ -125,7 +125,6 @@ class AffectedModuleDetectorPlugin : Plugin { ) } - @Suppress("UnstableApiUsage") @VisibleForTesting internal fun registerInternalTask( rootProject: Project, @@ -151,6 +150,7 @@ class AffectedModuleDetectorPlugin : Plugin { } } + @Suppress("UnstableApiUsage") private fun disableConfigCache(task: Task) { if (GradleVersion.current() >= GradleVersion.version("7.4")) { task.notCompatibleWithConfigurationCache("AMD requires knowledge of what has changed in the file system so we can not cache those values (https://github.com/dropbox/AffectedModuleDetector/issues/150)") @@ -159,7 +159,7 @@ class AffectedModuleDetectorPlugin : Plugin { private fun withPlugin( pluginId: String, - taskToConfigure: Task, + task: Task, testType: AffectedModuleTaskType, project: Project ) { @@ -171,19 +171,17 @@ class AffectedModuleDetectorPlugin : Plugin { project.pluginManager.withPlugin(pluginId) { getAffectedPath(testType, project)?.let { path -> - project - .tasks - .findByPath(path) - ?.onlyIf { task -> - when { - !AffectedModuleDetector.isProjectEnabled(task.project) -> true - else -> AffectedModuleDetector.isProjectAffected(task.project) - }.also { onlyIf -> - if (onlyIf && AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) { - taskToConfigure.dependsOn(path) - } - } + val pathOrNull = project.tasks.findByPath(path) + val onlyIf = when { + pathOrNull == null -> false + !AffectedModuleDetector.isProjectEnabled(pathOrNull.project) -> true + else -> AffectedModuleDetector.isProjectAffected(pathOrNull.project) + } + + if (onlyIf && AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) { + task.dependsOn(path) } + pathOrNull?.onlyIf { onlyIf } } } } @@ -201,12 +199,15 @@ class AffectedModuleDetectorPlugin : Plugin { InternalTaskType.ANDROID_TEST -> { getPathAndTask(project, tasks.runAndroidTestTask) } + InternalTaskType.ASSEMBLE_ANDROID_TEST -> { getPathAndTask(project, tasks.assembleAndroidTestTask) } + InternalTaskType.ANDROID_JVM_TEST -> { getPathAndTask(project, tasks.jvmTestTask) } + InternalTaskType.JVM_TEST -> { if (tasks.jvmTestTask != AffectedTestConfiguration.DEFAULT_JVM_TEST_TASK) { getPathAndTask(project, tasks.jvmTestTask) @@ -214,6 +215,7 @@ class AffectedModuleDetectorPlugin : Plugin { getPathAndTask(project, taskType.originalGradleCommand) } } + else -> { getPathAndTask(project, taskType.originalGradleCommand) } @@ -256,7 +258,7 @@ class AffectedModuleDetectorPlugin : Plugin { private fun requireConfiguration(project: Project): AffectedModuleConfiguration { return requireNotNull( value = project.rootProject.extensions.findByName(AffectedModuleConfiguration.name), - lazyMessage = { "Unable to find ${AffectedModuleConfiguration.name} in ${project.rootProject}" } + lazyMessage = { "Unable to find ${AffectedModuleConfiguration.name} in ${project.rootProject}" } ) as AffectedModuleConfiguration } @@ -264,6 +266,7 @@ class AffectedModuleDetectorPlugin : Plugin { @VisibleForTesting internal const val TEST_TASK_GROUP_NAME = "Affected Module Detector" + @VisibleForTesting internal const val CUSTOM_TASK_GROUP_NAME = "Affected Module Detector custom tasks" diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt index 51c38f71..f0242058 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt @@ -111,8 +111,8 @@ class AffectedModuleDetectorIntegrationTest { .build() // THEN - assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest") - assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest") + assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED") + assertThat(result.output).contains(":sample-core:assembleAndroidTest SKIPPED") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } @@ -189,7 +189,7 @@ class AffectedModuleDetectorIntegrationTest { .build() // THEN - assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest") + assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED") assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } From 61d8dde2941f19fe8ed0c717f3083fbefe60a151 Mon Sep 17 00:00:00 2001 From: Sinan Kozak Date: Sat, 10 Feb 2024 18:57:52 +0100 Subject: [PATCH 4/4] Update tests --- .../AffectedModuleDetectorIntegrationTest.kt | 6 ++++++ .../kotlin/com/dropbox/sample/tasks/AffectedTasksPlugin.kt | 5 ++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt index f0242058..eb83d89f 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt @@ -112,6 +112,9 @@ class AffectedModuleDetectorIntegrationTest { // THEN assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED") + assertThat(result.output).contains(":sample-core:mergeDexDebugAndroidTest SKIPPED") + assertThat(result.output).contains(":sample-core:packageDebugAndroidTest SKIPPED") + assertThat(result.output).contains(":sample-core:assembleDebugAndroidTest SKIPPED") assertThat(result.output).contains(":sample-core:assembleAndroidTest SKIPPED") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } @@ -190,6 +193,9 @@ class AffectedModuleDetectorIntegrationTest { // THEN assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED") + assertThat(result.output).doesNotContain(":sample-core:mergeDexDebugAndroidTest") + assertThat(result.output).doesNotContain(":sample-core:packageDebugAndroidTest") + assertThat(result.output).doesNotContain(":sample-core:assembleDebugAndroidTest") assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } diff --git a/sample/buildSrc/src/main/kotlin/com/dropbox/sample/tasks/AffectedTasksPlugin.kt b/sample/buildSrc/src/main/kotlin/com/dropbox/sample/tasks/AffectedTasksPlugin.kt index a49115c1..5af78ae5 100644 --- a/sample/buildSrc/src/main/kotlin/com/dropbox/sample/tasks/AffectedTasksPlugin.kt +++ b/sample/buildSrc/src/main/kotlin/com/dropbox/sample/tasks/AffectedTasksPlugin.kt @@ -53,8 +53,8 @@ class AffectedTasksPlugin : Plugin { private fun registerAffectedTestTask( taskName: String, testTask: String, testTaskBackup: String?, rootProject: Project - ): Task { - val task = rootProject.tasks.register(taskName) { task -> + ) { + rootProject.tasks.register(taskName) { task -> val paths = getAffectedPaths(testTask, testTaskBackup, rootProject) paths.forEach { path -> task.dependsOn(path) @@ -62,7 +62,6 @@ class AffectedTasksPlugin : Plugin { task.enabled = paths.isNotEmpty() task.onlyIf { paths.isNotEmpty() } } - return task.get() } private fun getAffectedPaths(