Skip to content

Commit 60ad8ec

Browse files
committed
code review: added tests
1 parent 15d6d17 commit 60ad8ec

File tree

4 files changed

+193
-37
lines changed

4 files changed

+193
-37
lines changed

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
4545
}
4646
)
4747

48-
val mainConfiguration = registerMainConfiguration(project)
49-
registerCustomTasks(project, mainConfiguration)
5048
registerSubprojectConfiguration(project)
49+
registerMainConfiguration(project)
50+
registerCustomTasks(project)
5151
registerTestTasks(project)
5252

5353
project.gradle.projectsEvaluated {
@@ -59,13 +59,11 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
5959
}
6060
}
6161

62-
private fun registerMainConfiguration(project: Project): AffectedModuleConfiguration {
63-
val configuration = AffectedModuleConfiguration()
62+
private fun registerMainConfiguration(project: Project) {
6463
project.extensions.add(
6564
AffectedModuleConfiguration.name,
66-
configuration
65+
AffectedModuleConfiguration()
6766
)
68-
return configuration
6967
}
7068

7169
private fun registerSubprojectConfiguration(project: Project) {
@@ -77,45 +75,47 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
7775
}
7876
}
7977

80-
private fun registerCustomTasks(
81-
rootProject: Project,
82-
mainConfiguration: AffectedModuleConfiguration
83-
) {
84-
rootProject.afterEvaluate {
85-
mainConfiguration
86-
.customTasks
87-
.forEach { taskType ->
88-
registerAffectedTestTask(
89-
rootProject = rootProject,
90-
taskType = taskType,
91-
groupName = CUSTOM_TASK_GROUP_NAME
92-
)
93-
}
94-
}
78+
@VisibleForTesting
79+
internal 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
84+
85+
mainConfiguration
86+
.customTasks
87+
.forEach { taskType ->
88+
registerImpactAnalysisTask(
89+
rootProject = rootProject,
90+
taskType = taskType,
91+
groupName = CUSTOM_TASK_GROUP_NAME
92+
)
93+
}
9594
}
9695

97-
private fun registerTestTasks(rootProject: Project) {
98-
registerAffectedTestTask(
96+
@VisibleForTesting
97+
internal fun registerTestTasks(rootProject: Project) {
98+
registerImpactAnalysisTask(
9999
rootProject = rootProject,
100100
taskType = InternalTaskType.JVM_TEST,
101101
groupName = TEST_TASK_GROUP_NAME
102102
)
103103

104-
registerAffectedTestTask(
104+
registerImpactAnalysisTask(
105105
rootProject = rootProject,
106106
taskType = InternalTaskType.ANDROID_TEST,
107107
groupName = TEST_TASK_GROUP_NAME
108108
)
109109

110-
registerAffectedTestTask(
110+
registerImpactAnalysisTask(
111111
rootProject = rootProject,
112112
taskType = InternalTaskType.ASSEMBLE_ANDROID_TEST,
113113
groupName = TEST_TASK_GROUP_NAME
114114
)
115115
}
116116

117117
@VisibleForTesting
118-
internal fun registerAffectedTestTask(
118+
internal fun registerImpactAnalysisTask(
119119
rootProject: Project,
120120
taskType: AffectedModuleTaskType,
121121
groupName: String
@@ -218,17 +218,19 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
218218
}
219219
}
220220

221-
private companion object {
221+
companion object {
222222

223-
const val TEST_TASK_GROUP_NAME = "Affected Module Detector"
224-
const val CUSTOM_TASK_GROUP_NAME = "Affected Module Detector custom tasks"
223+
@VisibleForTesting
224+
internal const val TEST_TASK_GROUP_NAME = "Affected Module Detector"
225+
@VisibleForTesting
226+
internal const val CUSTOM_TASK_GROUP_NAME = "Affected Module Detector custom tasks"
225227

226-
const val PLUGIN_ANDROID_APPLICATION = "com.android.application"
227-
const val PLUGIN_ANDROID_LIBRARY = "java-library"
228-
const val PLUGIN_JAVA_LIBRARY = "com.android.library"
229-
const val PLUGIN_KOTLIN = "kotlin"
228+
private const val PLUGIN_ANDROID_APPLICATION = "com.android.application"
229+
private const val PLUGIN_ANDROID_LIBRARY = "java-library"
230+
private const val PLUGIN_JAVA_LIBRARY = "com.android.library"
231+
private const val PLUGIN_KOTLIN = "kotlin"
230232

231-
val pluginIds = listOf(
233+
private val pluginIds = listOf(
232234
PLUGIN_ANDROID_APPLICATION,
233235
PLUGIN_ANDROID_LIBRARY,
234236
PLUGIN_JAVA_LIBRARY,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ package com.dropbox.affectedmoduledetector
99
interface AffectedModuleTaskType {
1010

1111
/**
12-
* Console command `./gradlew commandByImpact` which will run the original
12+
* Console command `./gradlew [commandByImpact]` which will run the original
1313
* command `./gradlew originalCommand` on modules affected by diff changes.
1414
*/
1515
val commandByImpact: String
1616

1717
/**
18-
* The original console command `./gradlew originalCommand` that does something.
18+
* The original console command `./gradlew [originalGradleCommand]` that does something.
1919
* Example:
2020
* - :connectedDebugAndroidTest
2121
* - :assembleDebugAndroidTest

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ 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+
}
34+
2235
@Before
2336
fun setup() {
2437
config = AffectedModuleConfiguration()
@@ -259,4 +272,43 @@ class AffectedModuleConfigurationTest {
259272

260273
assertThat(actual).isTrue()
261274
}
275+
276+
@Test
277+
fun `GIVEN AffectedModuleConfiguration WHEN customTasks THEN is empty`() {
278+
val actual = config.customTasks
279+
280+
assertThat(actual).isEmpty()
281+
}
282+
283+
@Test
284+
fun `GIVEN AffectedModuleConfiguration WHEN customTasks contains task THEN is not empty`() {
285+
config.customTasks = setOf(CustomImpactAnalysisTaskType.FAKE_TASK)
286+
val actual = config.customTasks
287+
288+
assertThat(actual).contains(CustomImpactAnalysisTaskType.FAKE_TASK)
289+
}
290+
291+
@Test
292+
fun `GIVEN AffectedModuleConfiguration WHEN customTasks contains task THEN task contains commandByImpact field`() {
293+
config.customTasks = setOf(CustomImpactAnalysisTaskType.FAKE_TASK)
294+
val actual = config.customTasks
295+
296+
assert(actual.first().commandByImpact == "runFakeTask")
297+
}
298+
299+
@Test
300+
fun `GIVEN AffectedModuleConfiguration WHEN customTasks contains task THEN task contains originalGradleCommand field`() {
301+
config.customTasks = setOf(CustomImpactAnalysisTaskType.FAKE_TASK)
302+
val actual = config.customTasks
303+
304+
assert(actual.first().originalGradleCommand == "fakeOriginalGradleCommand")
305+
}
306+
307+
@Test
308+
fun `GIVEN AffectedModuleConfiguration WHEN customTasks contains task THEN task contains taskDescription field`() {
309+
config.customTasks = setOf(CustomImpactAnalysisTaskType.FAKE_TASK)
310+
val actual = config.customTasks
311+
312+
assert(actual.first().taskDescription == "Description of fake task")
313+
}
262314
}

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

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class AffectedModuleDetectorPluginTest {
8888
val plugin = AffectedModuleDetectorPlugin()
8989

9090
// WHEN
91-
plugin.registerAffectedTestTask(
91+
plugin.registerImpactAnalysisTask(
9292
rootProject = rootProject,
9393
taskType = task,
9494
groupName = "fakeGroup"
@@ -101,4 +101,106 @@ class AffectedModuleDetectorPluginTest {
101101
assertThat(result?.group).isEqualTo("fakeGroup")
102102
assertThat(result?.description).isEqualTo(FakeTaskType.FAKE_TASK.taskDescription)
103103
}
104+
105+
@Test
106+
fun `GIVEN affected module detector plugin WHEN register custom task is called AND AffectedModuleConfiguration customTask is not empty THEN task is added`() {
107+
// GIVEN
108+
val configuration = AffectedModuleConfiguration()
109+
configuration.customTasks = setOf(FakeTaskType.FAKE_TASK)
110+
rootProject.extensions.add(AffectedModuleConfiguration.name, configuration)
111+
112+
val plugin = AffectedModuleDetectorPlugin()
113+
114+
// WHEN
115+
plugin.registerCustomTasks(rootProject)
116+
val result = rootProject.tasks.findByPath(FakeTaskType.FAKE_TASK.commandByImpact)
117+
118+
// THEN
119+
assertThat(result).isNotNull()
120+
assertThat(result?.name).isEqualTo(FakeTaskType.FAKE_TASK.commandByImpact)
121+
assertThat(result?.group).isEqualTo(AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME)
122+
assertThat(result?.description).isEqualTo(FakeTaskType.FAKE_TASK.taskDescription)
123+
}
124+
125+
@Test
126+
fun `GIVEN affected module detector plugin WHEN registerCustomTasks is called AND AffectedModuleConfiguration customTask is empty THEN task isn't added`() {
127+
// GIVEN
128+
val configuration = AffectedModuleConfiguration()
129+
rootProject.extensions.add(AffectedModuleConfiguration.name, configuration)
130+
val plugin = AffectedModuleDetectorPlugin()
131+
132+
// WHEN
133+
plugin.registerCustomTasks(rootProject)
134+
val result = rootProject
135+
.tasks
136+
.filter { it.group == AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME }
137+
138+
// THEN
139+
assertThat(result).isEmpty()
140+
}
141+
142+
@Test
143+
fun `GIVEN AffectedModuleDetectorPlugin WHEN CUSTOM_TASK_GROUP_NAME compared with TEST_TASK_GROUP_NAME THEN they isn't equal`() {
144+
assert(AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME != AffectedModuleDetectorPlugin.TEST_TASK_GROUP_NAME)
145+
}
146+
147+
@Test
148+
fun `GIVEN affected module detector plugin WHEN registerTestTasks THEN task all task added`() {
149+
// GIVEN
150+
val configuration = AffectedModuleConfiguration()
151+
rootProject.extensions.add(AffectedModuleConfiguration.name, configuration)
152+
val plugin = AffectedModuleDetectorPlugin()
153+
154+
// WHEN
155+
plugin.registerTestTasks(rootProject)
156+
val androidTestTask = rootProject.tasks.findByPath(InternalTaskType.ANDROID_TEST.commandByImpact)
157+
val assembleAndroidTestTask = rootProject.tasks.findByPath(InternalTaskType.ASSEMBLE_ANDROID_TEST.commandByImpact)
158+
val jvmTestTask = rootProject.tasks.findByPath(InternalTaskType.JVM_TEST.commandByImpact)
159+
160+
// THEN
161+
assertThat(androidTestTask).isNotNull()
162+
assertThat(androidTestTask?.group).isEqualTo(AffectedModuleDetectorPlugin.TEST_TASK_GROUP_NAME)
163+
164+
assertThat(assembleAndroidTestTask).isNotNull()
165+
assertThat(assembleAndroidTestTask?.group).isEqualTo(AffectedModuleDetectorPlugin.TEST_TASK_GROUP_NAME)
166+
167+
assertThat(jvmTestTask).isNotNull()
168+
assertThat(jvmTestTask?.group).isEqualTo(AffectedModuleDetectorPlugin.TEST_TASK_GROUP_NAME)
169+
}
170+
171+
@Test
172+
fun `GIVEN affected module detector plugin WHEN registerTestTasks called THEN added all tasks from InternalTaskType`() {
173+
// GIVEN
174+
val configuration = AffectedModuleConfiguration()
175+
rootProject.extensions.add(AffectedModuleConfiguration.name, configuration)
176+
val plugin = AffectedModuleDetectorPlugin()
177+
178+
// WHEN
179+
plugin.registerTestTasks(rootProject)
180+
val testTasks = rootProject
181+
.tasks
182+
.filter { it.group == AffectedModuleDetectorPlugin.TEST_TASK_GROUP_NAME }
183+
184+
// THEN
185+
assert(testTasks.size == InternalTaskType.values().size)
186+
}
187+
188+
@Test
189+
fun `GIVEN affected module detector plugin WHEN registerCustomTasks called THEN added all tasks from FakeTaskType`() {
190+
// GIVEN
191+
val configuration = AffectedModuleConfiguration()
192+
configuration.customTasks = setOf(FakeTaskType.FAKE_TASK)
193+
rootProject.extensions.add(AffectedModuleConfiguration.name, configuration)
194+
val plugin = AffectedModuleDetectorPlugin()
195+
196+
// WHEN
197+
plugin.registerCustomTasks(rootProject)
198+
199+
val customTasks = rootProject
200+
.tasks
201+
.filter { it.group == AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME }
202+
203+
// THEN
204+
assert(customTasks.size == FakeTaskType.values().size)
205+
}
104206
}

0 commit comments

Comments
 (0)