Skip to content

Commit c5977c7

Browse files
Merge pull request #240 from kozaxinan/depend-fix
Set up dependOn task only if project is affected
2 parents d3b8a1f + 61d8dde commit c5977c7

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.gradle.util.GradleVersion
3131
* logFolder = "${project.rootDir}".
3232
* }
3333
*
34-
* To enable affected module detection, you need to pass [ENABLE_ARG]
34+
* To enable affected module detection, you need to pass [com.dropbox.affectedmoduledetector.AffectedModuleDetector.Companion.ENABLE_ARG]
3535
* into the build as a command line parameter.
3636
*
3737
* See [AffectedModuleDetector] for additional flags.
@@ -125,7 +125,6 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
125125
)
126126
}
127127

128-
@Suppress("UnstableApiUsage")
129128
@VisibleForTesting
130129
internal fun registerInternalTask(
131130
rootProject: Project,
@@ -151,6 +150,7 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
151150
}
152151
}
153152

153+
@Suppress("UnstableApiUsage")
154154
private fun disableConfigCache(task: Task) {
155155
if (GradleVersion.current() >= GradleVersion.version("7.4")) {
156156
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)")
@@ -172,16 +172,16 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
172172
project.pluginManager.withPlugin(pluginId) {
173173
getAffectedPath(testType, project)?.let { path ->
174174
val pathOrNull = project.tasks.findByPath(path)
175-
if (AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path) && pathOrNull != null) {
176-
task.dependsOn(path)
175+
val onlyIf = when {
176+
pathOrNull == null -> false
177+
!AffectedModuleDetector.isProjectEnabled(pathOrNull.project) -> true
178+
else -> AffectedModuleDetector.isProjectAffected(pathOrNull.project)
177179
}
178180

179-
pathOrNull?.onlyIf { task ->
180-
when {
181-
!AffectedModuleDetector.isProjectEnabled(task.project) -> true
182-
else -> AffectedModuleDetector.isProjectAffected(task.project)
183-
}
181+
if (onlyIf && AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) {
182+
task.dependsOn(path)
184183
}
184+
pathOrNull?.onlyIf { onlyIf }
185185
}
186186
}
187187
}
@@ -199,19 +199,23 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
199199
InternalTaskType.ANDROID_TEST -> {
200200
getPathAndTask(project, tasks.runAndroidTestTask)
201201
}
202+
202203
InternalTaskType.ASSEMBLE_ANDROID_TEST -> {
203204
getPathAndTask(project, tasks.assembleAndroidTestTask)
204205
}
206+
205207
InternalTaskType.ANDROID_JVM_TEST -> {
206208
getPathAndTask(project, tasks.jvmTestTask)
207209
}
210+
208211
InternalTaskType.JVM_TEST -> {
209212
if (tasks.jvmTestTask != AffectedTestConfiguration.DEFAULT_JVM_TEST_TASK) {
210213
getPathAndTask(project, tasks.jvmTestTask)
211214
} else {
212215
getPathAndTask(project, taskType.originalGradleCommand)
213216
}
214217
}
218+
215219
else -> {
216220
getPathAndTask(project, taskType.originalGradleCommand)
217221
}
@@ -254,14 +258,15 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
254258
private fun requireConfiguration(project: Project): AffectedModuleConfiguration {
255259
return requireNotNull(
256260
value = project.rootProject.extensions.findByName(AffectedModuleConfiguration.name),
257-
lazyMessage = { "Unable to find ${AffectedModuleConfiguration.name} in ${project.rootProject}" }
261+
lazyMessage = { "Unable to find ${AffectedModuleConfiguration.name} in ${project.rootProject}" }
258262
) as AffectedModuleConfiguration
259263
}
260264

261265
companion object {
262266

263267
@VisibleForTesting
264268
internal const val TEST_TASK_GROUP_NAME = "Affected Module Detector"
269+
265270
@VisibleForTesting
266271
internal const val CUSTOM_TASK_GROUP_NAME = "Affected Module Detector custom tasks"
267272

affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class AffectedModuleDetectorIntegrationTest {
112112

113113
// THEN
114114
assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED")
115+
assertThat(result.output).contains(":sample-core:mergeDexDebugAndroidTest SKIPPED")
116+
assertThat(result.output).contains(":sample-core:packageDebugAndroidTest SKIPPED")
117+
assertThat(result.output).contains(":sample-core:assembleDebugAndroidTest SKIPPED")
115118
assertThat(result.output).contains(":sample-core:assembleAndroidTest SKIPPED")
116119
assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED")
117120
}
@@ -190,7 +193,10 @@ class AffectedModuleDetectorIntegrationTest {
190193

191194
// THEN
192195
assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED")
193-
assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest SKIPPED")
196+
assertThat(result.output).doesNotContain(":sample-core:mergeDexDebugAndroidTest")
197+
assertThat(result.output).doesNotContain(":sample-core:packageDebugAndroidTest")
198+
assertThat(result.output).doesNotContain(":sample-core:assembleDebugAndroidTest")
199+
assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest")
194200
assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED")
195201
}
196-
}
202+
}

sample/buildSrc/src/main/kotlin/com/dropbox/sample/tasks/AffectedTasksPlugin.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ class AffectedTasksPlugin : Plugin<Project> {
5353
private fun registerAffectedTestTask(
5454
taskName: String, testTask: String, testTaskBackup: String?,
5555
rootProject: Project
56-
): Task {
57-
val task = rootProject.tasks.register(taskName) { task ->
56+
) {
57+
rootProject.tasks.register(taskName) { task ->
5858
val paths = getAffectedPaths(testTask, testTaskBackup, rootProject)
5959
paths.forEach { path ->
6060
task.dependsOn(path)
6161
}
6262
task.enabled = paths.isNotEmpty()
6363
task.onlyIf { paths.isNotEmpty() }
6464
}
65-
return task.get()
6665
}
6766

6867
private fun getAffectedPaths(

0 commit comments

Comments
 (0)