Skip to content

Commit

Permalink
Added test case with compute all file settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Rd4dev committed Aug 8, 2024
1 parent 87b45a9 commit 30f3e97
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,3 @@ public static void addItemDecorator(
recyclerView.addItemDecoration(new DividerItemDecorator(drawable));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,14 @@ class ComputeChangedFiles(
rootDirectory: File,
pathToRoot: String
): List<String> {
val testFileExemptiontextProto = "scripts/assets/test_file_exemptions"
val testFileExemptionList = loadTestFileExemptionsProto(testFileExemptiontextProto)
.testFileExemptionList
.filter { it.testFileNotRequired }
.map { it.exemptedFilePath }

val searchFiles = RepositoryFile.collectSearchFiles(
repoPath = pathToRoot,
expectedExtension = ".kt",
exemptionsList = testFileExemptionList
)

return searchFiles
.filter { it.name.endsWith(".kt") && !it.name.endsWith("Test.kt") }
.map { rootDirectory.toPath().relativize(it.toPath()).toString() }
.filter { it.extension == "kt" && !it.nameWithoutExtension.endsWith("Test") }
.map { it.toRelativeString(rootDirectory) }
}

private fun computeChangedFilesForNonDevelopBranch(
Expand All @@ -161,7 +154,7 @@ class ComputeChangedFiles(
return gitClient.changedFiles
.map { File(rootDirectory, it) }
.filter { it.exists() }
.map { rootDirectory.toPath().relativize(it.toPath()).toString() }
.map { it.toRelativeString(rootDirectory) }
}

private fun filterFiles(files: List<String>) : List<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private fun findTestFile(rootDirectory: File, filePath: String): List<String> {
return possibleTestFilePaths
.map { File(rootDirectory, it) }
.filter(File::exists)
.map { it.relativeTo(rootDirectory).path }
.map { it.toRelativeString(rootDirectory) }
}

private fun loadTestFileExemptionsProto(testFileExemptiontextProto: String): TestFileExemptions {
Expand Down
20 changes: 14 additions & 6 deletions scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fun main(vararg args: String) {
.flatMap { filePath ->
when {
filePath.endsWith("Test.kt") -> {
findSourceFile(repoRoot, filePath)
findSourceFile(File(repoRoot).absoluteFile, repoRoot, filePath)
}
filePath.endsWith(".kt") -> listOf(filePath)
else -> emptyList()
Expand Down Expand Up @@ -182,7 +182,7 @@ class RunCoverage(
.build()
).build()
} else {
val testFilePaths = findTestFiles(repoRoot, filePath)
val testFilePaths = findTestFiles(rootDirectory, repoRoot, filePath)
if (testFilePaths.isEmpty()) {
return CoverageReport.newBuilder()
.setFailure(
Expand Down Expand Up @@ -264,7 +264,11 @@ class RunCoverage(
}
}

private fun findTestFiles(repoRoot: String, filePath: String): List<String> {
private fun findTestFiles(
rootDirectory: File,
repoRoot: String,
filePath: String
): List<String> {
val possibleTestFilePaths = when {
filePath.startsWith("scripts/") -> {
listOf(filePath.replace("/java/", "/javatests/").replace(".kt", "Test.kt"))
Expand All @@ -286,10 +290,14 @@ private fun findTestFiles(repoRoot: String, filePath: String): List<String> {
return possibleTestFilePaths
.map { File(repoRootFile, it) }
.filter(File::exists)
.map { it.relativeTo(repoRootFile).path }
.map { it.toRelativeString(rootDirectory) }
}

private fun findSourceFile(repoRoot: String, filePath: String): List<String> {
private fun findSourceFile(
rootDirectory: File,
repoRoot: String,
filePath: String
): List<String> {
val possibleSourceFilePaths = when {
filePath.startsWith("scripts/") -> {
listOf(filePath.replace("/javatests/", "/java/").replace("Test.kt", ".kt"))
Expand Down Expand Up @@ -320,7 +328,7 @@ private fun findSourceFile(repoRoot: String, filePath: String): List<String> {
return possibleSourceFilePaths
.map { File(repoRootFile, it) }
.filter(File::exists)
.map { it.relativeTo(repoRootFile).path }
.map { it.toRelativeString(rootDirectory) }
}

private fun getReportOutputPath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,8 @@ class ComputeChangedFilesTest {
switchToFeatureBranch()
createEmptyWorkspace()

val changedFiles = listOf(file)*/

createAndCommitFile("First", "Second", "Third", subPackage = "app")


val reportedFiles = runScript()

// Since the develop branch is checked out, all files should be returned.
Expand All @@ -160,8 +157,20 @@ class ComputeChangedFilesTest {
assertThat(reportedFiles).isEmpty()
}

// Update later
// Add test here for computeAllFiles flag
@Test
fun testUtility_featureBranch_noChanges_computeAllFiles_returnsAllFiles() {
initializeEmptyGitRepository()
createFiles("First", "Second", "Third", subPackage = "app")
switchToFeatureBranch()

val reportedFiles = runScript(computeAllFiles = true)

// Even though there are no changes, all files should be returned since that was requested via
// a command argument.
assertThat(reportedFiles).hasSize(1)
assertThat(reportedFiles.first().changedFilesList)
.containsExactly("app/First.kt", "app/Second.kt", "app/Third.kt")
}


@Test
Expand Down

0 comments on commit 30f3e97

Please sign in to comment.