Skip to content

Commit 0f16ade

Browse files
committed
Merge branch 'main' into feature/new-specified-branch-commit
2 parents dd7eff4 + ae455b5 commit 0f16ade

File tree

5 files changed

+124
-38
lines changed

5 files changed

+124
-38
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,15 @@ class AffectedModuleDetectorImpl constructor(
362362
private var unknownFiles: MutableSet<String> = mutableSetOf()
363363

364364
override fun shouldInclude(project: Project): Boolean {
365-
return (
366-
(project.isRoot || (
367-
affectedProjects.contains(project) &&
368-
isProjectProvided2(project)
369-
)) && !config.excludedModules.contains(project.name)
370-
).also {
371-
logger?.info(
372-
"checking whether I should include ${project.path} and my answer is $it"
373-
)
374-
}
365+
val isRootProject = project.isRoot
366+
val isProjectAffected = affectedProjects.contains(project)
367+
val isProjectProvided = isProjectProvided2(project)
368+
val isNotModuleExcluded = !config.excludedModules.contains(project.name)
369+
370+
val shouldInclude = (isRootProject || (isProjectAffected && isProjectProvided)) && isNotModuleExcluded
371+
logger?.info("checking whether I should include ${project.path} and my answer is $shouldInclude")
372+
373+
return shouldInclude
375374
}
376375

377376
override fun hasAffectedProjects() = affectedProjects.isNotEmpty()

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

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
7777
}
7878

7979
private fun registerCustomTasks(rootProject: Project) {
80-
val mainConfiguration = requireNotNull(
81-
value = rootProject.extensions.findByName(AffectedModuleConfiguration.name),
82-
lazyMessage = { "Unable to find ${AffectedTestConfiguration.name} in $rootProject" }
83-
) as AffectedModuleConfiguration
80+
val mainConfiguration = requireConfiguration(rootProject)
8481

8582
rootProject.afterEvaluate {
8683
registerCustomTasks(rootProject, mainConfiguration.customTasks)
@@ -93,14 +90,15 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
9390
customTasks: Set<AffectedModuleTaskType>
9491
) {
9592
customTasks.forEach { taskType ->
96-
val task = rootProject.tasks.register(taskType.commandByImpact).get()
97-
task.group = CUSTOM_TASK_GROUP_NAME
98-
task.description = taskType.taskDescription
99-
disableConfigCache(task)
100-
101-
rootProject.subprojects { project ->
102-
pluginIds.forEach { pluginId ->
103-
withPlugin(pluginId, task, taskType, project)
93+
rootProject.tasks.register(taskType.commandByImpact) { task ->
94+
task.group = CUSTOM_TASK_GROUP_NAME
95+
task.description = taskType.taskDescription
96+
disableConfigCache(task)
97+
98+
rootProject.subprojects { project ->
99+
pluginIds.forEach { pluginId ->
100+
withPlugin(pluginId, task, taskType, project)
101+
}
104102
}
105103
}
106104
}
@@ -134,20 +132,19 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
134132
taskType: AffectedModuleTaskType,
135133
groupName: String
136134
) {
137-
val task = rootProject.tasks.register(taskType.commandByImpact).get()
138-
task.group = groupName
139-
task.description = taskType.taskDescription
140-
disableConfigCache(task)
135+
rootProject.tasks.register(taskType.commandByImpact) { task ->
136+
task.group = groupName
137+
task.description = taskType.taskDescription
138+
disableConfigCache(task)
141139

142-
rootProject.subprojects { project ->
143-
project.afterEvaluate { evaluatedProject ->
140+
rootProject.subprojects { project ->
144141
pluginIds.forEach { pluginId ->
145142
if (pluginId == PLUGIN_JAVA_LIBRARY || pluginId == PLUGIN_KOTLIN) {
146143
if (taskType == InternalTaskType.ANDROID_JVM_TEST) {
147-
withPlugin(pluginId, task, InternalTaskType.JVM_TEST, evaluatedProject)
144+
withPlugin(pluginId, task, InternalTaskType.JVM_TEST, project)
148145
}
149146
} else {
150-
withPlugin(pluginId, task, taskType, evaluatedProject)
147+
withPlugin(pluginId, task, taskType, project)
151148
}
152149
}
153150
}
@@ -166,18 +163,22 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
166163
testType: AffectedModuleTaskType,
167164
project: Project
168165
) {
166+
val config = requireConfiguration(project)
167+
168+
fun isExcludedModule(configuration: AffectedModuleConfiguration, path: String): Boolean {
169+
return configuration.excludedModules.find { path.startsWith(":$it") } != null
170+
}
171+
169172
project.pluginManager.withPlugin(pluginId) {
170173
getAffectedPath(testType, project)?.let { path ->
171-
if (AffectedModuleDetector.isProjectProvided(project)) {
174+
if (AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) {
172175
task.dependsOn(path)
173176
}
174177

175-
project.afterEvaluate {
176-
project.tasks.findByPath(path)?.onlyIf { task ->
177-
when {
178-
!AffectedModuleDetector.isProjectEnabled(task.project) -> true
179-
else -> AffectedModuleDetector.isProjectAffected(task.project)
180-
}
178+
project.tasks.findByPath(path)?.onlyIf { task ->
179+
when {
180+
!AffectedModuleDetector.isProjectEnabled(task.project) -> true
181+
else -> AffectedModuleDetector.isProjectAffected(task.project)
181182
}
182183
}
183184
}
@@ -249,6 +250,13 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
249250
}
250251
}
251252

253+
private fun requireConfiguration(project: Project): AffectedModuleConfiguration {
254+
return requireNotNull(
255+
value = project.rootProject.extensions.findByName(AffectedModuleConfiguration.name),
256+
lazyMessage = { "Unable to find ${AffectedModuleConfiguration.name} in ${project.rootProject}" }
257+
) as AffectedModuleConfiguration
258+
}
259+
252260
companion object {
253261

254262
@VisibleForTesting

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,82 @@ class AffectedModuleDetectorIntegrationTest {
114114
assertThat(result.output).contains(":sample-core:assembleAndroidTest SKIPPED")
115115
assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED")
116116
}
117+
118+
@Test
119+
fun `GIVEN multiple project with one excluded WHEN plugin is applied THEN tasks has dependencies minus the exclusions`() {
120+
// GIVEN
121+
tmpFolder.newFolder("sample-app")
122+
tmpFolder.newFolder("sample-core")
123+
tmpFolder.newFile("settings.gradle").writeText(
124+
"""
125+
|include ':sample-app'
126+
|include ':sample-core'
127+
""".trimMargin()
128+
)
129+
130+
tmpFolder.newFile("build.gradle").writeText(
131+
"""buildscript {
132+
| repositories {
133+
| google()
134+
| jcenter()
135+
| }
136+
| dependencies {
137+
| classpath "com.android.tools.build:gradle:4.1.0"
138+
| classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
139+
| }
140+
|}
141+
|plugins {
142+
| id "com.dropbox.affectedmoduledetector"
143+
|}
144+
|affectedModuleDetector {
145+
| excludedModules = [ "sample-core" ]
146+
|}
147+
|allprojects {
148+
| repositories {
149+
| google()
150+
| jcenter()
151+
| }
152+
|}""".trimMargin()
153+
)
154+
155+
tmpFolder.newFile("sample-app/build.gradle").writeText(
156+
"""plugins {
157+
| id 'com.android.application'
158+
| id 'kotlin-android'
159+
| }
160+
| android {
161+
| compileSdkVersion 30
162+
| buildToolsVersion "30.0.2"
163+
| }
164+
| dependencies {
165+
| implementation project(":sample-core")
166+
| }""".trimMargin()
167+
)
168+
169+
tmpFolder.newFile("sample-core/build.gradle").writeText(
170+
"""plugins {
171+
| id 'com.android.library'
172+
| id 'kotlin-android'
173+
| }
174+
| affectedTestConfiguration {
175+
| assembleAndroidTestTask = "assembleAndroidTest"
176+
| }
177+
| android {
178+
| compileSdkVersion 30
179+
| buildToolsVersion "30.0.2"
180+
| }""".trimMargin()
181+
)
182+
183+
// WHEN
184+
val result = GradleRunner.create()
185+
.withProjectDir(tmpFolder.root)
186+
.withPluginClasspath()
187+
.withArguments("assembleAffectedAndroidTests", "--dry-run")
188+
.build()
189+
190+
// THEN
191+
assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED")
192+
assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest SKIPPED")
193+
assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED")
194+
}
117195
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class AffectedModuleDetectorPluginTest {
8282
// GIVEN
8383
val task = fakeTask
8484
val plugin = AffectedModuleDetectorPlugin()
85+
rootProject.pluginManager.apply(AffectedModuleDetectorPlugin::class.java)
8586

8687
// WHEN
8788
plugin.registerInternalTask(

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
}
1010
}
1111
dependencies {
12-
classpath "com.android.tools.build:gradle:7.3.0"
12+
classpath "com.android.tools.build:gradle:7.3.1"
1313
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1414
classpath("org.jlleitschuh.gradle:ktlint-gradle:10.2.1")
1515
classpath("org.jacoco:org.jacoco.core:0.8.8")

0 commit comments

Comments
 (0)