Skip to content

Commit

Permalink
Added tests case for RunCoverage to check execution of coverage and f…
Browse files Browse the repository at this point in the history
…ixed other test cases with appropriate testFileName implementation
  • Loading branch information
Rd4dev committed Jun 20, 2024
1 parent 5075a1a commit fdaf9cb
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 12 deletions.
30 changes: 25 additions & 5 deletions scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ class RunCoverage(
private val commandExecutor: CommandExecutor,
private val scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher
) {
private val bazelClient by lazy { BazelClient(File(repoRoot), commandExecutor) }

private val rootDirectory = File(repoRoot).absoluteFile
private val testFileExemptionTextProto = "scripts/assets/test_file_exemptions"
var coverageDataList = mutableListOf<String>()
// var covdat: String = ""

/**
* Executes coverage analysis for the specified file.
Expand All @@ -69,28 +73,44 @@ class RunCoverage(
* a Bazel client, finds potential test file paths, retrieves Bazel targets, and initiates
* coverage analysis for each test target found.
*/
fun execute() {
fun execute(): List<String?> {
val testFileExemptionList = loadTestFileExemptionsProto(testFileExemptionTextProto)
.getExemptedFilePathList()

val isExempted = testFileExemptionList.contains(filePath)
if (isExempted) {
println("This file is exempted from having a test file. Hence No coverage!")
return
// return null
return emptyList()
}

val bazelClient = BazelClient(rootDirectory, commandExecutor)
// val bazelClient = BazelClient(rootDirectory, commandExecutor)
val testFilePaths = findTestFile(repoRoot, filePath)
// return findTestFile(repoRoot, filePath)
val testTargets = bazelClient.retrieveBazelTargets(testFilePaths)
// val testTargets = bazelClient.retrieveBazelTargets(testFilePaths)

for (testTarget in testTargets) {
RunCoverageForTestTarget(
val coverageData = RunCoverageForTestTarget(
rootDirectory,
testTarget.substringBeforeLast(".kt"),
commandExecutor,
scriptBgDispatcher
).runCoverage()
).runCoverage()!!
coverageDataList.add(coverageData)
}
return coverageDataList

//this works
/*val covdat = RunCoverageForTestTarget(
rootDirectory,
"//coverage/test/java/com/example:test",
commandExecutor,
scriptBgDispatcher
).runCoverage()
coverageDataList.add(covdat!!)
return coverageDataList*/
// return covdat
}

fun findTestFile(repoRoot: String, filePath: String): List<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class TestBazelWorkspace(private val temporaryRootFolder: TemporaryFolder) {
testBuildFile.appendText(
"""
kt_jvm_test(
name = "test",
name = "$testName",
srcs = ["$testName.kt"],
deps = [
"//$sourceSubpackage:${filename.lowercase()}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class BazelClientTest {
subpackage = "coverage"
)

val result = bazelClient.runCoverageForTestTarget("//coverage/test/java/com/example:test")
val result = bazelClient.runCoverageForTestTarget("//coverage/test/java/com/example:TwoSumTest")

// Check if ByteArray is returned from executing coverage command
assertThat(result).isInstanceOf(ByteArray::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CoverageRunnerTest {
}

@Test
fun testRunCoverage_emptyDirectory_throwsException() {
fun testCoverageRunner_emptyDirectory_throwsException() {
val exception = assertThrows<IllegalStateException>() {
coverageRunner.getCoverage(bazelTestTarget)
}
Expand All @@ -45,7 +45,7 @@ class CoverageRunnerTest {
}

@Test
fun testRunCoverage_invalidTestTarget_throwsException() {
fun testCoverageRunner_invalidTestTarget_throwsException() {
testBazelWorkspace.initEmptyWorkspace()

val exception = assertThrows<IllegalStateException>() {
Expand All @@ -57,7 +57,7 @@ class CoverageRunnerTest {
}

@Test
fun testRunCoverage_validSampleTestTarget_returnsCoverageData() {
fun testCoverageRunner_validSampleTestTarget_returnsCoverageData() {
testBazelWorkspace.initEmptyWorkspace()

val sourceContent =
Expand Down Expand Up @@ -103,7 +103,7 @@ class CoverageRunnerTest {
subpackage = "coverage"
)

val result = coverageRunner.getCoverage("//coverage/test/java/com/example:test")
val result = coverageRunner.getCoverage("//coverage/test/java/com/example:TwoSumTest")
val expectedResult =
"SF:coverage/main/java/com/example/TwoSum.kt\n" +
"FN:7,com/example/TwoSum${'$'}Companion::sumNumbers (II)Ljava/lang/Object;\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class RunCoverageForTestTargetTest {

val result = RunCoverageForTestTarget(
tempFolder.root,
"//coverage/test/java/com/example:test",
"//coverage/test/java/com/example:TwoSumTest",
longCommandExecutor,
scriptBgDispatcher
).runCoverage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,107 @@ class RunCoverageTest {
assertEquals(expectedLocalTestFilePaths, result)
}

@Test
fun testRunCoverage_validSampleTestFile_returnsCoverageData() {
testBazelWorkspace.initEmptyWorkspace()

val sourceContent =
"""
package com.example
class TwoSum {
companion object {
fun sumNumbers(a: Int, b: Int): Any {
return if (a ==0 && b == 0) {
"Both numbers are zero"
} else {
a + b
}
}
}
}
""".trimIndent()

val testContent =
"""
package com.example
import org.junit.Assert.assertEquals
import org.junit.Test
class TwoSumTest {
@Test
fun testSumNumbers() {
assertEquals(TwoSum.sumNumbers(0, 1), 1)
assertEquals(TwoSum.sumNumbers(3, 4), 7)
assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero")
}
}
""".trimIndent()

testBazelWorkspace.addSourceAndTestFileWithContent(
filename = "TwoSum",
sourceContent = sourceContent,
testContent = testContent,
subpackage = "coverage"
)

val result = RunCoverage(
"${tempFolder.root}",
"coverage/main/java/com/example/TwoSum.kt",
longCommandExecutor,
scriptBgDispatcher).execute()

/*val expectedResult =
"["+"SF:coverage/main/java/com/example/TwoSum.kt\n" +
"FN:7,com/example/TwoSum${'$'}Companion::sumNumbers (II)Ljava/lang/Object;\n" +
"FN:3,com/example/TwoSum::<init> ()V\n" +
"FNDA:1,com/example/TwoSum${'$'}Companion::sumNumbers (II)Ljava/lang/Object;\n" +
"FNDA:0,com/example/TwoSum::<init> ()V\n" +
"FNF:2\n" +
"FNH:1\n" +
"BRDA:7,0,0,1\n" +
"BRDA:7,0,1,1\n" +
"BRDA:7,0,2,1\n" +
"BRDA:7,0,3,1\n" +
"BRF:4\n" +
"BRH:4\n" +
"DA:3,0\n" +
"DA:7,1\n" +
"DA:8,1\n" +
"DA:10,1\n" +
"LH:3\n" +
"LF:4\n" +
"end_of_record\n"+"]"*/

val expectedResultList = listOf(
"SF:coverage/main/java/com/example/TwoSum.kt\n"+
"FN:7,com/example/TwoSum${'$'}Companion::sumNumbers (II)Ljava/lang/Object;\n"+
"FN:3,com/example/TwoSum::<init> ()V\n"+
"FNDA:1,com/example/TwoSum${'$'}Companion::sumNumbers (II)Ljava/lang/Object;\n"+
"FNDA:0,com/example/TwoSum::<init> ()V\n"+
"FNF:2\n"+
"FNH:1\n"+
"BRDA:7,0,0,1\n"+
"BRDA:7,0,1,1\n"+
"BRDA:7,0,2,1\n"+
"BRDA:7,0,3,1\n"+
"BRF:4\n"+
"BRH:4\n"+
"DA:3,0\n"+
"DA:7,1\n"+
"DA:8,1\n"+
"DA:10,1\n"+
"LH:3\n"+
"LF:4\n"+
"end_of_record\n"
)

assertThat(result).isEqualTo(expectedResultList)
}

private fun initializeCommandExecutorWithLongProcessWaitTime(): CommandExecutorImpl {
return CommandExecutorImpl(
scriptBgDispatcher, processTimeout = 5, processTimeoutUnit = TimeUnit.MINUTES
Expand Down

0 comments on commit fdaf9cb

Please sign in to comment.