Skip to content

Commit

Permalink
Syncing in with upstream many to one targets
Browse files Browse the repository at this point in the history
Probably require a lot of iterations to check all passes an clear up the centralized protos execution. Things to do 1. Add error messages to be included in the failed coverage report proto to provide more info on the fail case, 2. Have one centralized proto generation to handle md generation (flexible for both local and ci and with distinction in creation based on md and html (need to figure out), 3. Update to have a sequential flow, 4. Update tests as most error cases are removed
  • Loading branch information
Rd4dev committed Jul 27, 2024
2 parents e0d1afe + c2eddfb commit 07e6fad
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class RunCoverage(
}

val testTargets = bazelClient.retrieveBazelTargets(testFilePaths)

val deferredCoverageReports = testTargets.map { testTarget ->
CoverageRunner(rootDirectory, scriptBgDispatcher, commandExecutor)
.runWithCoverageAsync(testTarget.removeSuffix(".kt"))
Expand Down Expand Up @@ -284,9 +285,7 @@ class RunCoverage(
}

val allCoveredLines = coverageReports.flatMap { it.coveredLineList }

val groupedCoveredLines = allCoveredLines.groupBy { it.lineNumber }

val aggregatedCoveredLines = groupedCoveredLines.map { (lineNumber, coveredLines) ->
CoveredLine.newBuilder()
.setLineNumber(lineNumber)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.oppia.android.scripts.coverage

import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Before
Expand All @@ -14,6 +16,7 @@ import org.oppia.android.scripts.proto.CoverageReport
import org.oppia.android.scripts.proto.CoveredLine
import org.oppia.android.scripts.testing.TestBazelWorkspace
import org.oppia.android.testing.assertThrows
import java.io.File
import java.util.concurrent.TimeUnit

/** Tests for [CoverageRunner]. */
Expand All @@ -27,11 +30,50 @@ class CoverageRunnerTest {
private lateinit var testBazelWorkspace: TestBazelWorkspace
private lateinit var bazelTestTarget: String

private lateinit var sourceContent: String
private lateinit var testContent: String

@Before
fun setUp() {
coverageRunner = CoverageRunner(tempFolder.root, scriptBgDispatcher, longCommandExecutor)
bazelTestTarget = "//:testTarget"
testBazelWorkspace = TestBazelWorkspace(tempFolder)

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

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

@After
Expand Down Expand Up @@ -65,45 +107,82 @@ class CoverageRunnerTest {
}

@Test
fun testRunWithCoverageAsync_validSampleTestTarget_returnsCoverageData() {
fun testRunWithCoverageAsync_coverageRetrievalFailed_throwsException() {
val coverageFilePath = "${tempFolder.root}/bazel-out/k8-fastbuild/testlogs" +
"/coverage/test/java/com/example/AddNumsTest/coverage.dat"

testBazelWorkspace.initEmptyWorkspace()
testBazelWorkspace.addSourceAndTestFileWithContent(
filename = "AddNums",
testFilename = "AddNumsTest",
sourceContent = sourceContent,
testContent = testContent,
sourceSubpackage = "coverage/main/java/com/example",
testSubpackage = "coverage/test/java/com/example"
)

val sourceContent =
"""
package com.example
class AddNums {
companion object {
fun sumNumbers(a: Int, b: Int): Any {
return if (a == 0 && b == 0) {
"Both numbers are zero"
} else {
a + b
}
}
}
val exception = assertThrows<IllegalStateException>() {
runBlocking {
launch {
coverageRunner.runWithCoverageAsync("//coverage/test/java/com/example:AddNumsTest")
.await()
}

launch {
do {
File(coverageFilePath).takeIf { it.exists() }?.apply {
writeText("")
return@launch
}
delay(1)
} while (true)
}
}
""".trimIndent()
}

val testContent =
"""
package com.example
import org.junit.Assert.assertEquals
import org.junit.Test
class AddNumsTest {
@Test
fun testSumNumbers() {
assertEquals(AddNums.sumNumbers(0, 1), 1)
assertEquals(AddNums.sumNumbers(3, 4), 7)
assertEquals(AddNums.sumNumbers(0, 0), "Both numbers are zero")
}
assertThat(exception).hasMessageThat().contains("Failed to retrieve coverage result")
}

@Test
fun testRunWithCoverageAsync_coverageDataMissing_throwsException() {
val coverageFilePath = "${tempFolder.root}/bazel-out/k8-fastbuild/testlogs" +
"/coverage/test/java/com/example/AddNumsTest/coverage.dat"

testBazelWorkspace.initEmptyWorkspace()
testBazelWorkspace.addSourceAndTestFileWithContent(
filename = "AddNums",
testFilename = "AddNumsTest",
sourceContent = sourceContent,
testContent = testContent,
sourceSubpackage = "coverage/main/java/com/example",
testSubpackage = "coverage/test/java/com/example"
)

val exception = assertThrows<IllegalArgumentException>() {
runBlocking {
launch {
coverageRunner.runWithCoverageAsync("//coverage/test/java/com/example:AddNumsTest")
.await()
}

launch {
do {
File(coverageFilePath).takeIf { it.exists() }?.apply {
writeText("SF: coverage/test/java/com/example/IncorrectCoverageFile.kt")
return@launch
}
delay(1)
} while (true)
}
}
""".trimIndent()
}

assertThat(exception).hasMessageThat().contains("Coverage data not found")
}

@Test
fun testRunWithCoverageAsync_validSampleTestTarget_returnsCoverageData() {
testBazelWorkspace.initEmptyWorkspace()
testBazelWorkspace.addSourceAndTestFileWithContent(
filename = "AddNums",
testFilename = "AddNumsTest",
Expand Down

0 comments on commit 07e6fad

Please sign in to comment.