Skip to content

Commit 897934e

Browse files
Add support for toggling if uncommitted code should be included (#113)
Co-authored-by: Josh Feinberg <joshfeinberg@dropbox.com>
1 parent 1d8d66b commit 897934e

File tree

6 files changed

+77
-10
lines changed

6 files changed

+77
-10
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ affectedModuleDetector {
7373
excludedModules = [
7474
"sample-util"
7575
]
76+
includeUncommitted = true
77+
top = "HEAD"
7678
}
7779
```
7880

@@ -85,14 +87,16 @@ affectedModuleDetector {
8587
- PreviousCommit: compare against the previous commit
8688
- ForkCommit: compare against the commit the branch was forked from
8789
- SpecifiedBranchCommit: specify the branch to compare changes against using the `specifiedBranch` configuration before the `compareFrom` configuration
88-
- `excludedModules` : A list of modules that will be excluded from the build process
89-
90-
91-
90+
- `excludedModules`: A list of modules that will be excluded from the build process
91+
- `includeUncommitted`: If uncommitted files should be considered affected
92+
- `top`: The top of the git log to use. Must be used in combination with configuration `includeUncommitted = false`
93+
94+
95+
9296
By default, the Detector will look for `assembleAndroidDebugTest`, `connectedAndroidDebugTest`, and `testDebug`. Modules can specify a configuration block to specify which variant tests to run:
9397
```groovy
9498
affectedTestConfiguration {
95-
assembleAndroidTestTask = "assmebleAndroidReleaseTest"
99+
assembleAndroidTestTask = "assembleAndroidReleaseTest"
96100
runAndroidTestTask = "connectedAndroidReleaseTest"
97101
jvmTestTask = "testRelease"
98102
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ class AffectedModuleConfiguration {
5858
*/
5959
var excludedModules = emptySet<String>()
6060

61+
/**
62+
* If uncommitted files should be considered affected
63+
*/
64+
var includeUncommitted: Boolean = true
65+
66+
/**
67+
* The top of the git log to use, only used when [includeUncommitted] is false
68+
*/
69+
var top: String = "HEAD"
70+
set(value) {
71+
require(!includeUncommitted) {
72+
"Set includeUncommitted to false to set a custom top"
73+
}
74+
field = value
75+
}
76+
6177
companion object {
6278
const val name = "affectedModuleDetector"
6379
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class AffectedModuleDetectorImpl constructor(
351351
}
352352

353353
private val changedProjects by lazy {
354-
findChangedProjects()
354+
findChangedProjects(config.top, config.includeUncommitted)
355355
}
356356

357357
private val dependentProjects by lazy {
@@ -401,9 +401,13 @@ class AffectedModuleDetectorImpl constructor(
401401
*
402402
* Also populates the unknownFiles var which is used in findAffectedProjects
403403
*/
404-
private fun findChangedProjects(): Set<Project> {
404+
private fun findChangedProjects(
405+
top: Sha,
406+
includeUncommitted: Boolean = true
407+
): Set<Project> {
405408
git.findChangedFiles(
406-
includeUncommitted = true
409+
top = top,
410+
includeUncommitted = includeUncommitted
407411
).forEach { fileName ->
408412
if (affectsAllModules(fileName)) {
409413
return allProjects

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal class GitClientImpl(
8383
return commandRunner.executeAndParse(if (includeUncommitted) {
8484
"$CHANGED_FILES_CMD_PREFIX $sha"
8585
} else {
86-
"$CHANGED_FILES_CMD_PREFIX $top $sha"
86+
"$CHANGED_FILES_CMD_PREFIX $top..$sha"
8787
})
8888
}
8989

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,47 @@ class AffectedModuleConfigurationTest {
216216
assertThat(config.compareFrom).isEqualTo("PreviousCommit")
217217
}
218218
}
219+
220+
@Test
221+
fun `GIVEN AffectedModuleConfiguration WHEN top THEN is HEAD`() {
222+
val actual = config.top
223+
224+
assertThat(actual).isEqualTo("HEAD")
225+
}
226+
227+
@Test
228+
fun `GIVEN AffectedModuleConfiguration WHEN includeUncommitted is true top is set to sha THEN exception thrown and value not set`() {
229+
val includeUncommitted = true
230+
val sha = "12345"
231+
232+
try {
233+
config.includeUncommitted = includeUncommitted
234+
config.top = sha
235+
} catch (e: IllegalArgumentException) {
236+
// THEN
237+
assertThat(e.message).isEqualTo("Set includeUncommitted to false to set a custom top")
238+
return
239+
}
240+
241+
fail("Expected to catch an exception")
242+
}
243+
244+
@Test
245+
fun `GIVEN AffectedModuleConfiguration WHEN includeUncommitted is false and top is set to sha THEN top is sha`() {
246+
val includeUncommitted = false
247+
val sha = "12345"
248+
249+
config.includeUncommitted = includeUncommitted
250+
config.top = sha
251+
252+
val actual = config.top
253+
assertThat(actual).isEqualTo(sha)
254+
}
255+
256+
@Test
257+
fun `GIVEN AffectedModuleConfiguration WHEN includeUncommitted THEN is true`() {
258+
val actual = config.includeUncommitted
259+
260+
assertThat(actual).isTrue()
261+
}
219262
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class GitClientImplTest {
6161
convertToFilePath("a", "b", "c.java"),
6262
convertToFilePath("d", "e", "f.java"))
6363
commandRunner.addReply(
64-
"$CHANGED_FILES_CMD_PREFIX otherSha mySha",
64+
"$CHANGED_FILES_CMD_PREFIX otherSha..mySha",
6565
changes.joinToString(System.lineSeparator())
6666
)
6767
commitShaProvider.addReply("mySha")

0 commit comments

Comments
 (0)