Skip to content

Commit 257ca9a

Browse files
committed
add example to simple and fix tests
1 parent a8dceff commit 257ca9a

6 files changed

Lines changed: 68 additions & 71 deletions

File tree

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,49 +75,58 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
7575
}
7676
}
7777

78-
@VisibleForTesting
79-
internal fun registerCustomTasks(rootProject: Project) {
78+
private fun registerCustomTasks(rootProject: Project) {
8079
val mainConfiguration = requireNotNull(
8180
value = rootProject.extensions.findByName(AffectedModuleConfiguration.name),
8281
lazyMessage = { "Unable to find ${AffectedTestConfiguration.name} in $rootProject" }
8382
) as AffectedModuleConfiguration
8483

8584
rootProject.afterEvaluate {
86-
mainConfiguration
87-
.customTasks
88-
.forEach { taskType ->
89-
registerImpactAnalysisTask(
90-
rootProject = rootProject,
91-
taskType = taskType,
92-
groupName = CUSTOM_TASK_GROUP_NAME
93-
)
85+
registerCustomTasks(rootProject, mainConfiguration.customTasks)
86+
}
87+
}
88+
89+
@VisibleForTesting
90+
internal fun registerCustomTasks(
91+
rootProject: Project,
92+
customTasks: Set<AffectedModuleTaskType>
93+
) {
94+
customTasks.forEach { taskType ->
95+
val task = rootProject.tasks.register(taskType.commandByImpact).get()
96+
task.group = CUSTOM_TASK_GROUP_NAME
97+
task.description = taskType.taskDescription
98+
99+
rootProject.subprojects { project ->
100+
pluginIds.forEach { pluginId ->
101+
withPlugin(pluginId, task, taskType, project)
94102
}
103+
}
95104
}
96105
}
97106

98107
@VisibleForTesting
99108
internal fun registerTestTasks(rootProject: Project) {
100-
registerImpactAnalysisTask(
109+
registerInternalTask(
101110
rootProject = rootProject,
102111
taskType = InternalTaskType.ANDROID_JVM_TEST,
103112
groupName = TEST_TASK_GROUP_NAME
104113
)
105114

106-
registerImpactAnalysisTask(
115+
registerInternalTask(
107116
rootProject = rootProject,
108117
taskType = InternalTaskType.ANDROID_TEST,
109118
groupName = TEST_TASK_GROUP_NAME
110119
)
111120

112-
registerImpactAnalysisTask(
121+
registerInternalTask(
113122
rootProject = rootProject,
114123
taskType = InternalTaskType.ASSEMBLE_ANDROID_TEST,
115124
groupName = TEST_TASK_GROUP_NAME
116125
)
117126
}
118127

119128
@VisibleForTesting
120-
internal fun registerImpactAnalysisTask(
129+
internal fun registerInternalTask(
121130
rootProject: Project,
122131
taskType: AffectedModuleTaskType,
123132
groupName: String

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,11 @@ class AffectedModuleConfigurationTest {
1919

2020
private lateinit var config : AffectedModuleConfiguration
2121

22-
private enum class CustomImpactAnalysisTaskType(
23-
override val commandByImpact: String,
24-
override val originalGradleCommand: String,
25-
override val taskDescription: String
26-
): AffectedModuleTaskType {
27-
28-
FAKE_TASK(
29-
commandByImpact = "runFakeTask",
30-
originalGradleCommand = "fakeOriginalGradleCommand",
31-
taskDescription = "Description of fake task"
32-
)
33-
}
22+
private val FAKE_TASK = AffectedModuleConfiguration.CustomTask(
23+
commandByImpact = "runFakeTask",
24+
originalGradleCommand = "fakeOriginalGradleCommand",
25+
taskDescription = "Description of fake task"
26+
)
3427

3528
@Before
3629
fun setup() {
@@ -282,31 +275,31 @@ class AffectedModuleConfigurationTest {
282275

283276
@Test
284277
fun `GIVEN AffectedModuleConfiguration WHEN customTasks contains task THEN is not empty`() {
285-
config.customTasks = setOf(CustomImpactAnalysisTaskType.FAKE_TASK)
278+
config.customTasks = setOf(FAKE_TASK)
286279
val actual = config.customTasks
287280

288-
assertThat(actual).contains(CustomImpactAnalysisTaskType.FAKE_TASK)
281+
assertThat(actual).contains(FAKE_TASK)
289282
}
290283

291284
@Test
292285
fun `GIVEN AffectedModuleConfiguration WHEN customTasks contains task THEN task contains commandByImpact field`() {
293-
config.customTasks = setOf(CustomImpactAnalysisTaskType.FAKE_TASK)
286+
config.customTasks = setOf(FAKE_TASK)
294287
val actual = config.customTasks
295288

296289
assert(actual.first().commandByImpact == "runFakeTask")
297290
}
298291

299292
@Test
300293
fun `GIVEN AffectedModuleConfiguration WHEN customTasks contains task THEN task contains originalGradleCommand field`() {
301-
config.customTasks = setOf(CustomImpactAnalysisTaskType.FAKE_TASK)
294+
config.customTasks = setOf(FAKE_TASK)
302295
val actual = config.customTasks
303296

304297
assert(actual.first().originalGradleCommand == "fakeOriginalGradleCommand")
305298
}
306299

307300
@Test
308301
fun `GIVEN AffectedModuleConfiguration WHEN customTasks contains task THEN task contains taskDescription field`() {
309-
config.customTasks = setOf(CustomImpactAnalysisTaskType.FAKE_TASK)
302+
config.customTasks = setOf(FAKE_TASK)
310303
val actual = config.customTasks
311304

312305
assert(actual.first().taskDescription == "Description of fake task")

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

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,11 @@ class AffectedModuleDetectorPluginTest {
2222
const val FAKE_TASK_DESCRIPTION = "fake_description"
2323
}
2424

25-
private enum class FakeTaskType(
26-
override val commandByImpact: String,
27-
override val originalGradleCommand: String,
28-
override val taskDescription: String
29-
): AffectedModuleTaskType {
30-
31-
FAKE_TASK(
32-
commandByImpact = FAKE_COMMAND_BY_IMPACT,
33-
originalGradleCommand = FAKE_ORIGINAL_COMMAND,
34-
taskDescription = FAKE_TASK_DESCRIPTION
35-
)
36-
}
25+
private val fakeTask = AffectedModuleConfiguration.CustomTask(
26+
commandByImpact = FAKE_COMMAND_BY_IMPACT,
27+
originalGradleCommand = FAKE_ORIGINAL_COMMAND,
28+
taskDescription = FAKE_TASK_DESCRIPTION
29+
)
3730

3831
@Rule
3932
@JvmField
@@ -87,11 +80,11 @@ class AffectedModuleDetectorPluginTest {
8780
@Test
8881
fun `GIVEN affected module detector plugin WHEN register task is called THEN task is added`() {
8982
// GIVEN
90-
val task = FakeTaskType.FAKE_TASK
83+
val task = fakeTask
9184
val plugin = AffectedModuleDetectorPlugin()
9285

9386
// WHEN
94-
plugin.registerImpactAnalysisTask(
87+
plugin.registerInternalTask(
9588
rootProject = rootProject,
9689
taskType = task,
9790
groupName = "fakeGroup"
@@ -100,29 +93,29 @@ class AffectedModuleDetectorPluginTest {
10093

10194
// THEN
10295
assertThat(result).isNotNull()
103-
assertThat(result?.name).isEqualTo(FakeTaskType.FAKE_TASK.commandByImpact)
96+
assertThat(result?.name).isEqualTo(fakeTask.commandByImpact)
10497
assertThat(result?.group).isEqualTo("fakeGroup")
105-
assertThat(result?.description).isEqualTo(FakeTaskType.FAKE_TASK.taskDescription)
98+
assertThat(result?.description).isEqualTo(fakeTask.taskDescription)
10699
}
107100

108101
@Test
109-
fun `GIVEN affected module detector plugin WHEN register custom task is called AND AffectedModuleConfiguration customTask is not empty THEN task is added`() {
102+
fun `GIVEN affected module detector plugin WHEN register_custom_task is called AND AffectedModuleConfiguration customTask is not empty THEN task is added`() {
110103
// GIVEN
111104
val configuration = AffectedModuleConfiguration()
112-
configuration.customTasks = setOf(FakeTaskType.FAKE_TASK)
105+
configuration.customTasks = setOf(fakeTask)
113106
rootProject.extensions.add(AffectedModuleConfiguration.name, configuration)
114107

115108
val plugin = AffectedModuleDetectorPlugin()
116109

117110
// WHEN
118-
plugin.registerCustomTasks(rootProject)
119-
val result = rootProject.tasks.findByPath(FakeTaskType.FAKE_TASK.commandByImpact)
111+
plugin.registerCustomTasks(rootProject, setOf(fakeTask))
112+
val result = rootProject.tasks.findByPath(fakeTask.commandByImpact)
120113

121114
// THEN
122115
assertThat(result).isNotNull()
123-
assertThat(result?.name).isEqualTo(FakeTaskType.FAKE_TASK.commandByImpact)
116+
assertThat(result?.name).isEqualTo(fakeTask.commandByImpact)
124117
assertThat(result?.group).isEqualTo(AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME)
125-
assertThat(result?.description).isEqualTo(FakeTaskType.FAKE_TASK.taskDescription)
118+
assertThat(result?.description).isEqualTo(fakeTask.taskDescription)
126119
}
127120

128121
@Test
@@ -133,7 +126,7 @@ class AffectedModuleDetectorPluginTest {
133126
val plugin = AffectedModuleDetectorPlugin()
134127

135128
// WHEN
136-
plugin.registerCustomTasks(rootProject)
129+
plugin.registerCustomTasks(rootProject, emptySet())
137130
val result = rootProject
138131
.tasks
139132
.filter { it.group == AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME }
@@ -192,19 +185,20 @@ class AffectedModuleDetectorPluginTest {
192185
@Test
193186
fun `GIVEN affected module detector plugin WHEN registerCustomTasks called THEN added all tasks from FakeTaskType`() {
194187
// GIVEN
188+
val givenCustomTasks = setOf(fakeTask, fakeTask.copy(commandByImpact = "otherCommand"))
195189
val configuration = AffectedModuleConfiguration()
196-
configuration.customTasks = setOf(FakeTaskType.FAKE_TASK)
190+
configuration.customTasks = givenCustomTasks
197191
rootProject.extensions.add(AffectedModuleConfiguration.name, configuration)
198192
val plugin = AffectedModuleDetectorPlugin()
199193

200194
// WHEN
201-
plugin.registerCustomTasks(rootProject)
195+
plugin.registerCustomTasks(rootProject, givenCustomTasks)
202196

203197
val customTasks = rootProject
204198
.tasks
205199
.filter { it.group == AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME }
206200

207201
// THEN
208-
assert(customTasks.size == FakeTaskType.values().size)
202+
assert(customTasks.size == 2)
209203
}
210204
}

sample/build.gradle

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ buildscript {
1515
classpath Dependencies.Libs.ANDROID_BUILD_TOOLS
1616
classpath Dependencies.Libs.KOTLIN_GRADLE_PLUGIN
1717
classpath Dependencies.Libs.KTLINT
18+
classpath Dependencies.Libs.DETEKT
1819
}
1920
}
2021

22+
plugins {
23+
id("io.gitlab.arturbosch.detekt") version "1.20.0"
24+
}
25+
2126
apply plugin: "org.jlleitschuh.gradle.ktlint"
2227
apply plugin: "com.dropbox.affectedmoduledetector"
2328

@@ -30,10 +35,9 @@ affectedModuleDetector {
3035
compareFrom = "SpecifiedBranchCommit"
3136
customTasks = [
3237
new AffectedModuleConfiguration.CustomTask(
33-
"runSomeCustomTaskByImpact",
34-
"someTaskForExample",
35-
"Command someTaskForExample is just an example task for demonstration of" +
36-
"how to add custom commands to impact analysis"
38+
"runDetektByImpact",
39+
"detekt",
40+
"Run static analysis tool by Impact analysis"
3741
)
3842
]
3943
logFolder = "${project.rootDir}"
@@ -42,14 +46,9 @@ affectedModuleDetector {
4246
]
4347
}
4448

45-
// just for example; Do not add to you project.
4649
allprojects {
47-
tasks.register("someTaskForExample") {task ->
48-
println("someTaskForExample on path: ${task.path} was called")
49-
}
50-
}
50+
apply plugin: Dependencies.Libs.DETEKT_PLUGIN
5151

52-
allprojects {
5352
repositories {
5453
google()
5554
mavenCentral()
@@ -59,4 +58,3 @@ allprojects {
5958
task clean(type: Delete) {
6059
delete rootProject.buildDir
6160
}
62-

sample/buildSrc/src/main/kotlin/com/dropbox/sample/Dependencies.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.dropbox.sample
22

33
object Dependencies {
4+
45
private object Versions {
56
const val KOTLIN_VERSION = "1.6.10"
7+
const val DETEKT_VERSION = "1.20.0"
68
}
79

810
object Libs {
@@ -17,5 +19,7 @@ object Dependencies {
1719
const val ANDROIDX_ESPRESSO = "androidx.test.espresso:espresso-core:3.3.0"
1820
const val ANDROID_BUILD_TOOLS = "com.android.tools.build:gradle:4.1.0"
1921
const val KTLINT = "org.jlleitschuh.gradle:ktlint-gradle:9.1.1"
22+
const val DETEKT = "com.android.tools.build:gradle:${Versions.DETEKT_VERSION}"
23+
const val DETEKT_PLUGIN = "io.gitlab.arturbosch.detekt"
2024
}
21-
}
25+
}

sample/sample-app/src/main/java/com/dropbox/detector/sample/MainActivity.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
55

66
class MainActivity : AppCompatActivity() {
7+
78
override fun onCreate(savedInstanceState: Bundle?) {
89
super.onCreate(savedInstanceState)
910
setContentView(R.layout.activity_main)
1011
}
11-
12-
1312
}

0 commit comments

Comments
 (0)