Skip to content

Commit 3871eec

Browse files
committed
Improve the code
1 parent 22d372e commit 3871eec

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

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

Lines changed: 19 additions & 16 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)")
@@ -159,7 +159,7 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
159159

160160
private fun withPlugin(
161161
pluginId: String,
162-
taskToConfigure: Task,
162+
task: Task,
163163
testType: AffectedModuleTaskType,
164164
project: Project
165165
) {
@@ -171,19 +171,17 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
171171

172172
project.pluginManager.withPlugin(pluginId) {
173173
getAffectedPath(testType, project)?.let { path ->
174-
project
175-
.tasks
176-
.findByPath(path)
177-
?.onlyIf { task ->
178-
when {
179-
!AffectedModuleDetector.isProjectEnabled(task.project) -> true
180-
else -> AffectedModuleDetector.isProjectAffected(task.project)
181-
}.also { onlyIf ->
182-
if (onlyIf && AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) {
183-
taskToConfigure.dependsOn(path)
184-
}
185-
}
174+
val pathOrNull = project.tasks.findByPath(path)
175+
val onlyIf = when {
176+
pathOrNull == null -> false
177+
!AffectedModuleDetector.isProjectEnabled(pathOrNull.project) -> true
178+
else -> AffectedModuleDetector.isProjectAffected(pathOrNull.project)
179+
}
180+
181+
if (onlyIf && AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) {
182+
task.dependsOn(path)
186183
}
184+
pathOrNull?.onlyIf { onlyIf }
187185
}
188186
}
189187
}
@@ -201,19 +199,23 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
201199
InternalTaskType.ANDROID_TEST -> {
202200
getPathAndTask(project, tasks.runAndroidTestTask)
203201
}
202+
204203
InternalTaskType.ASSEMBLE_ANDROID_TEST -> {
205204
getPathAndTask(project, tasks.assembleAndroidTestTask)
206205
}
206+
207207
InternalTaskType.ANDROID_JVM_TEST -> {
208208
getPathAndTask(project, tasks.jvmTestTask)
209209
}
210+
210211
InternalTaskType.JVM_TEST -> {
211212
if (tasks.jvmTestTask != AffectedTestConfiguration.DEFAULT_JVM_TEST_TASK) {
212213
getPathAndTask(project, tasks.jvmTestTask)
213214
} else {
214215
getPathAndTask(project, taskType.originalGradleCommand)
215216
}
216217
}
218+
217219
else -> {
218220
getPathAndTask(project, taskType.originalGradleCommand)
219221
}
@@ -256,14 +258,15 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
256258
private fun requireConfiguration(project: Project): AffectedModuleConfiguration {
257259
return requireNotNull(
258260
value = project.rootProject.extensions.findByName(AffectedModuleConfiguration.name),
259-
lazyMessage = { "Unable to find ${AffectedModuleConfiguration.name} in ${project.rootProject}" }
261+
lazyMessage = { "Unable to find ${AffectedModuleConfiguration.name} in ${project.rootProject}" }
260262
) as AffectedModuleConfiguration
261263
}
262264

263265
companion object {
264266

265267
@VisibleForTesting
266268
internal const val TEST_TASK_GROUP_NAME = "Affected Module Detector"
269+
267270
@VisibleForTesting
268271
internal const val CUSTOM_TASK_GROUP_NAME = "Affected Module Detector custom tasks"
269272

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class AffectedModuleDetectorIntegrationTest {
111111
.build()
112112

113113
// THEN
114-
assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest")
115-
assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest")
114+
assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED")
115+
assertThat(result.output).contains(":sample-core:assembleAndroidTest SKIPPED")
116116
assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED")
117117
}
118118

@@ -189,7 +189,7 @@ class AffectedModuleDetectorIntegrationTest {
189189
.build()
190190

191191
// THEN
192-
assertThat(result.output).doesNotContain(":sample-app:assembleDebugAndroidTest")
192+
assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED")
193193
assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest")
194194
assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED")
195195
}

0 commit comments

Comments
 (0)