Skip to content

Commit

Permalink
Fixed/Updated all the test cases with changes to the computing only t…
Browse files Browse the repository at this point in the history
…he changed files
  • Loading branch information
Rd4dev committed Aug 9, 2024
1 parent 9b0c209 commit 48bc7f7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class ComputeChangedFiles(
rootDirectory: File,
pathToRoot: String
): List<String> {
val changedKtFiles = gitClient.changedFiles
val changedKtFiles = gitClient.committedFiles
.map { File(rootDirectory, it) }
.filter { it.exists() }
.map { it.toRelativeString(rootDirectory) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class GitClient(
*/
val changedFiles: Set<String> by lazy { retrieveChangedFilesWithPotentialDuplicates().toSet() }

/** The list of files that have been committed in the local branch. */
val committedFiles: List<String> by lazy { retrieveChangedCommittedFiles() }

private fun retrieveCurrentCommit(): String {
return executeGitCommandWithOneLineOutput("rev-parse HEAD")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,45 +198,46 @@ class ComputeChangedFilesTest {
}

@Test
fun testUtility_featureBranch_fileChange_staged_returnsChangedFile() {
fun testUtility_featureBranch_fileChangeStaged_shouldNotBeComputed() {
initializeEmptyGitRepository()
createAndCommitFiles("First", "Second", "Third", subPackage = "app")
switchToFeatureBranch()
changeAndStageFile("First", subPackage = "app")

val reportedFiles = runScript()

// Only the first file should be reported since the file itself was changed & staged.
assertThat(reportedFiles).hasSize(1)
assertThat(reportedFiles.first().changedFilesList).containsExactly("app/First.kt")
// No files should be reported since the file was only staged and
// not fully committed in the feature branch.
assertThat(reportedFiles).hasSize(0)
}

@Test
fun testUtility_featureBranch_fileChange_unstaged_returnsChangedFile() {
fun testUtility_featureBranch_fileChangeUnstaged_shouldNotBeComputed() {
initializeEmptyGitRepository()
createAndCommitFiles("First", "Second", "Third", subPackage = "app")
switchToFeatureBranch()
changeAndCommitFile("First", subPackage = "app")

val reportedFiles = runScript()

// Only the first file should be reported since the file itself was changed & staged.
// Only the first file should be reported because it is the only one
// that was changed and committed in the feature branch.
assertThat(reportedFiles).hasSize(1)
assertThat(reportedFiles.first().changedFilesList).containsExactly("app/First.kt")
}

@Test
fun testUtility_featureBranch_newFile_untracked_returnsChangedFile() {
fun testUtility_featureBranch_newFile_untracked_shouldNotBeComputed() {
initializeEmptyGitRepository()
createAndCommitFiles("First", "Second", "Third", subPackage = "app")
switchToFeatureBranch()
createFiles("NewUntrackedFile", subPackage = "data")

val reportedFiles = runScript()

// Only the first file should be reported since the file itself was changed & staged.
assertThat(reportedFiles).hasSize(1)
assertThat(reportedFiles.first().changedFilesList).containsExactly("data/NewUntrackedFile.kt")
// No files should be reported since the file was untracked and
// not fully committed in the feature branch.
assertThat(reportedFiles).hasSize(0)
}

@Test
Expand All @@ -253,7 +254,7 @@ class ComputeChangedFilesTest {
}

@Test
fun testUtility_featureBranch_movedFile_staged_returnsNewFile() {
fun testUtility_featureBranch_movedFile_stagedAndCommitted_returnsNewFile() {
initializeEmptyGitRepository()
createAndCommitFiles("First", subPackage = "app")
switchToFeatureBranch()
Expand Down Expand Up @@ -748,6 +749,37 @@ class ComputeChangedFilesTest {
assertThat(reportedFiles.first().changedFilesList).doesNotContain("app/SecondTest.kt")
}

@Test
fun testUtility_featureBranch_withChangesToTestFile_mapsTestFileToSourceFile() {
initializeEmptyGitRepository()
createAndCommitFiles("First", subPackage = "app/main/java/com/src")
createAndCommitFiles("FirstTest", subPackage = "app/test/java/com/src")
switchToFeatureBranch()
changeAndCommitFile("FirstTest", subPackage = "app/test/java/com/src")

val reportedFiles = runScript()

assertThat(reportedFiles).hasSize(1)
assertThat(reportedFiles.first().changedFilesList)
.containsExactly("app/main/java/com/src/First.kt")
}

@Test
fun testUtility_featureBranch_withChangesToSourceAndTestFile_computesSingleSourceFile() {
initializeEmptyGitRepository()
createAndCommitFiles("First", subPackage = "app/main/java/com/src")
createAndCommitFiles("FirstTest", subPackage = "app/test/java/com/src")
switchToFeatureBranch()
changeAndCommitFile("First", subPackage = "app/main/java/com/src")
changeAndCommitFile("FirstTest", subPackage = "app/test/java/com/src")

val reportedFiles = runScript()

assertThat(reportedFiles).hasSize(1)
assertThat(reportedFiles.first().changedFilesList)
.containsExactly("app/main/java/com/src/First.kt")
}

private fun runScriptWithTextOutput(
currentHeadHash: String = computeMergeBase("develop"),
computeAllFiles: Boolean = false
Expand Down Expand Up @@ -878,7 +910,6 @@ class ComputeChangedFilesTest {
oldFilePath.delete()

testGitRepository.stageFileForCommit(newFilePath)

testGitRepository.commit(message = "Move file from $oldFilePath to $newFilePath")
}

Expand Down

0 comments on commit 48bc7f7

Please sign in to comment.