Skip to content

Commit

Permalink
Fix Static Checks / Lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Rd4dev committed Jun 13, 2024
1 parent 2d51176 commit fbd5084
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.oppia.android.scripts.coverage

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import org.oppia.android.scripts.common.BazelClient
import org.oppia.android.scripts.common.CommandExecutor
import org.oppia.android.scripts.common.CommandExecutorImpl
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.withContext
import java.io.File

/**
Expand All @@ -17,9 +16,9 @@ import java.io.File
* @param targetFile Path to the target file to analyze coverage.
*/
class CoverageRunner(
private val repoRoot: File,
private val scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher
) {
private val repoRoot: File,
private val scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher
) {

/**
* Runs coverage analysis asynchronously for the Bazel test target.
Expand Down Expand Up @@ -50,10 +49,10 @@ class CoverageRunner(
fun getCoverage(
bazelTestTarget: String
): List<String> {
val commandExecutor: CommandExecutor = CommandExecutorImpl(scriptBgDispatcher)
val bazelClient = BazelClient(repoRoot, commandExecutor)
val coverageData = bazelClient.runCoverageForTestTarget(bazelTestTarget)
return coverageData
val commandExecutor: CommandExecutor = CommandExecutorImpl(scriptBgDispatcher)
val bazelClient = BazelClient(repoRoot, commandExecutor)
val coverageData = bazelClient.runCoverageForTestTarget(bazelTestTarget)
return coverageData
}

/**
Expand All @@ -62,7 +61,7 @@ class CoverageRunner(
* @param data the result from the execution of the coverage command
* @return the extracted path of the coverage data file.
*/
fun parseCoverageDataFile(data: List<String>) : String? {
fun parseCoverageDataFile(data: List<String>): String? {
val regex = ".*coverage\\.dat$".toRegex()
for (line in data) {
val match = regex.find(line)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.oppia.android.scripts.coverage

import org.oppia.android.scripts.coverage.CoverageRunner
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
import kotlinx.coroutines.runBlocking
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
import java.io.File

/**
Expand Down Expand Up @@ -31,7 +30,7 @@ fun main(vararg args: String) {
*/
class RunCoverageForTestTarget(
private val repoRoot: File,
private val targetPath : String
private val targetPath: String
) {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class TestBazelWorkspace(private val temporaryRootFolder: TemporaryFolder) {
assertThat(bazelRcFile.exists()).isTrue()
}

/**
* Adds a source file and test file with the specified name and content,
* and updates the corresponding build configuration.
*
* @param filename the name of the source file (without the .kt extension)
* @param sourceContent the content of the source file
* @param testContent the content of the test file
* @param subpackage the subpackage under which the source and test files should be added
*/
fun addSourceAndTestFileWithContent(
filename: String,
sourceContent: String,
Expand Down Expand Up @@ -90,7 +99,9 @@ class TestBazelWorkspace(private val temporaryRootFolder: TemporaryFolder) {
}

// Create the source file
val sourceFile = temporaryRootFolder.newFile("${sourceSubpackage.replace(".", "/")}/$filename.kt")
val sourceFile = temporaryRootFolder.newFile(
"${sourceSubpackage.replace(".", "/")}/$filename.kt"
)
sourceFile.writeText(sourceContent)

// Create or update the BUILD file for the source file
Expand All @@ -106,7 +117,7 @@ class TestBazelWorkspace(private val temporaryRootFolder: TemporaryFolder) {
kt_jvm_library(
name = "${filename.lowercase()}",
srcs = ["${filename}.kt"],
srcs = ["$filename.kt"],
visibility = ["//visibility:public"],
deps = [],
)
Expand Down Expand Up @@ -154,13 +165,13 @@ class TestBazelWorkspace(private val temporaryRootFolder: TemporaryFolder) {
kt_jvm_test(
name = "test",
srcs = ["${testName}.kt"],
srcs = ["$testName.kt"],
deps = [
"//coverage/main/java/com/example:${filename.lowercase()}",
"@maven//:junit_junit",
],
visibility = ["//visibility:public"],
test_class = "com.example.${testName}",
test_class = "com.example.$testName",
)
""".trimIndent() + "\n"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ import org.oppia.android.testing.mockito.anyOrNull
import java.io.File
import java.util.concurrent.TimeUnit







/**
* Tests for [BazelClient].
*
Expand Down Expand Up @@ -390,39 +384,41 @@ class BazelClientTest {
val bazelClient = BazelClient(tempFolder.root, commandExecutor)
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()
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
import org.oppia.android.testing.assertThrows
import org.oppia.android.scripts.testing.TestBazelWorkspace
import kotlin.test.assertEquals
import java.io.File
import org.oppia.android.testing.assertThrows

/** Tests for [CoverageRunner] */
class CoverageRunnerTest {
Expand Down Expand Up @@ -52,7 +50,8 @@ class CoverageRunnerTest {
"/path/.cache/bazel/4654367352564/sandbox/__main__/__tmp/coverage/package/test/coverage.dat",
"Executed 1 out of 1 test: 1 test passes."
)
val expectedResultParsedData = "/path/.cache/bazel/4654367352564/sandbox/__main__/__tmp/coverage/package/test/coverage.dat"
val expectedResultParsedData =
"/path/.cache/bazel/4654367352564/sandbox/__main__/__tmp/coverage/package/test/coverage.dat"

val parsedData = coverageRunner.parseCoverageDataFile(validResultData)
assertThat(parsedData).isEqualTo(expectedResultParsedData)
Expand Down Expand Up @@ -83,39 +82,41 @@ class CoverageRunnerTest {
fun testRunCoverage_validSampleTestTarget_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()
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,41 @@ class RunCoverageForTestTargetTest {
fun testRunCoverageForTestTarget_validSampleTestTarget_returnsCoverageDataPath() {
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()
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",
Expand All @@ -111,4 +113,4 @@ class RunCoverageForTestTargetTest {
val parsedCoverageDataPath = result?.endsWith("coverage.dat")
assert(parsedCoverageDataPath!!) { "The coverage.dat is not generated" }
}
}
}

0 comments on commit fbd5084

Please sign in to comment.