Skip to content

Commit 476fe77

Browse files
committed
added tests
1 parent 30842e4 commit 476fe77

File tree

6 files changed

+99
-6
lines changed

6 files changed

+99
-6
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,16 @@ class AffectedModuleConfiguration {
8181

8282
var compareFrom: String = "PreviousCommit"
8383
set(value) {
84-
val commitShaProviders = listOf("PreviousCommit", "ForkCommit", "SpecifiedBranchCommit")
84+
val commitShaProviders = listOf(
85+
"PreviousCommit",
86+
"ForkCommit",
87+
"SpecifiedBranchCommit",
88+
"SpecifiedBranchCommit2"
89+
)
8590
require(commitShaProviders.contains(value)) {
8691
"The property configuration compareFrom must be one of the following: ${commitShaProviders.joinToString(", ")}"
8792
}
88-
if (value == "SpecifiedBranchCommit") {
93+
if (value == "SpecifiedBranchCommit" || value == "SpecifiedBranchCommit2") {
8994
requireNotNull(specifiedBranch) {
9095
"Specify a branch using the configuration specifiedBranch"
9196
}

affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/SpecifiedBranchCommit2.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package com.dropbox.affectedmoduledetector.commitshaproviders
33
import com.dropbox.affectedmoduledetector.GitClient
44
import com.dropbox.affectedmoduledetector.Sha
55

6-
class SpecifiedBranchCommit2(private val branch: String) : CommitShaProvider {
6+
class SpecifiedBranchCommit2(private val specifiedBranch: String) : CommitShaProvider {
77

88
override fun get(commandRunner: GitClient.CommandRunner): Sha {
99
val currentBranch = commandRunner.executeAndParseFirst(CURRENT_BRANCH_CMD)
10-
return commandRunner.executeAndParseFirst("git merge-base $currentBranch $branch")
10+
return commandRunner.executeAndParseFirst("git merge-base $currentBranch $specifiedBranch")
1111
}
1212

1313
companion object {

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import org.junit.rules.TemporaryFolder
99
import org.junit.runner.RunWith
1010
import org.junit.runners.JUnit4
1111
import java.io.File
12-
import java.lang.Exception
1312

1413
@RunWith(JUnit4::class)
1514
class AffectedModuleConfigurationTest {
@@ -173,6 +172,30 @@ class AffectedModuleConfigurationTest {
173172
assertThat(actual).isEqualTo("PreviousCommit")
174173
}
175174

175+
@Test
176+
fun `WHEN compareFrom is set to SpecifiedBranchCommit2 AND specifiedBranch is set THEN return SpecifiedBranchCommit2`() {
177+
val specifiedBranchCommit2 = "SpecifiedBranchCommit2"
178+
val specifiedBranch = "origin/dev"
179+
180+
config.specifiedBranch = specifiedBranch
181+
config.compareFrom = specifiedBranchCommit2
182+
183+
val actual = config.compareFrom
184+
185+
assertThat(actual).isEqualTo(specifiedBranchCommit2)
186+
}
187+
188+
@Test
189+
fun `WHEN compareFrom is set to SpecifiedBranchCommit2 AND specifiedBranch isn't set THEN throw exception`() {
190+
val specifiedBranchCommit2 = "SpecifiedBranchCommit2"
191+
192+
try {
193+
config.compareFrom = specifiedBranchCommit2
194+
} catch (e: IllegalArgumentException) {
195+
assertThat(e.message).isEqualTo("Specify a branch using the configuration specifiedBranch")
196+
}
197+
}
198+
176199
@Test
177200
fun `GIVEN AffectedModuleConfiguration WHEN compareFrom is set to ForkCommit THEN is ForkCommit`() {
178201
val forkCommit = "ForkCommit"
@@ -218,7 +241,7 @@ class AffectedModuleConfigurationTest {
218241
fail()
219242
} catch (e: Exception) {
220243
assertThat(e::class).isEqualTo(IllegalArgumentException::class)
221-
assertThat(e.message).isEqualTo("The property configuration compareFrom must be one of the following: PreviousCommit, ForkCommit, SpecifiedBranchCommit")
244+
assertThat(e.message).isEqualTo("The property configuration compareFrom must be one of the following: PreviousCommit, ForkCommit, SpecifiedBranchCommit, SpecifiedBranchCommit2")
222245
assertThat(config.compareFrom).isEqualTo("PreviousCommit")
223246
}
224247
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class AffectedModuleDetectorIntegrationTest {
5252
""".trimMargin()
5353
)
5454

55+
tmpFolder.newFile("local.properties").writeText(
56+
"""
57+
|sdk.dir=/Users/romanajmaletdinov/Library/Android/sdk
58+
""".trimMargin()
59+
)
60+
5561
tmpFolder.newFile("build.gradle").writeText(
5662
"""buildscript {
5763
| repositories {

affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProviderTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ class CommitShaProviderTest {
4141
}
4242
}
4343

44+
@Test
45+
fun givenSpecifiedBranchCommit2AndSpecifiedBranchNull_whenFromString_thenThrowException() {
46+
try {
47+
CommitShaProvider.fromString("SpecifiedBranchCommit2")
48+
} catch (e: Exception) {
49+
assertThat(e::class).isEqualTo(IllegalArgumentException::class)
50+
assertThat(e.message).isEqualTo("Specified branch must be defined")
51+
}
52+
}
53+
54+
@Test
55+
fun givenSpecifiedBranchCommit2_whenFromString_thenReturnSpecifiedBranchCommit2() {
56+
val actual = CommitShaProvider.fromString("SpecifiedBranchCommit2", "branch")
57+
58+
assertThat(actual::class).isEqualTo(SpecifiedBranchCommit2::class)
59+
}
60+
4461
@Test
4562
fun givenInvalidCommitString_whenFromString_thenExceptionThrown() {
4663
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.dropbox.affectedmoduledetector.commitshaproviders
2+
3+
import com.dropbox.affectedmoduledetector.AttachLogsTestRule
4+
import com.dropbox.affectedmoduledetector.mocks.MockCommandRunner
5+
import com.google.common.truth.Truth
6+
import org.junit.Rule
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.junit.runners.JUnit4
10+
11+
@RunWith(JUnit4::class)
12+
class SpecifiedBranchCommit2Test {
13+
14+
@Rule
15+
@JvmField
16+
val attachLogsRule = AttachLogsTestRule()
17+
private val logger = attachLogsRule.logger
18+
private val commandRunner = MockCommandRunner(logger)
19+
private val previousCommit = SpecifiedBranchCommit2(SPECIFIED_BRANCH)
20+
21+
@Test
22+
fun `WHEN CURRENT_BRANCH_CMD THEN command returned`() {
23+
Truth.assertThat(SpecifiedBranchCommit2.CURRENT_BRANCH_CMD).isEqualTo("git rev-parse --abbrev-ref HEAD")
24+
}
25+
26+
@Test
27+
fun `WHEN get commit sha THEN return sha`() {
28+
29+
commandRunner.addReply(SpecifiedBranchCommit2.CURRENT_BRANCH_CMD, NEW_FEATURE_BRANCH)
30+
commandRunner.addReply("git merge-base $NEW_FEATURE_BRANCH $SPECIFIED_BRANCH", "commit-sha")
31+
32+
val actual = previousCommit.get(commandRunner)
33+
34+
Truth.assertThat(actual).isEqualTo("commit-sha")
35+
}
36+
37+
private companion object {
38+
39+
const val SPECIFIED_BRANCH = "origin/dev"
40+
const val NEW_FEATURE_BRANCH = "newFeatureBranch"
41+
}
42+
}

0 commit comments

Comments
 (0)