Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix part of #5343: Building proto with coverage results for data processing #5439

Merged
merged 107 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
6ec35a7
Fix lint new line and kdoc checks
Rd4dev Jun 4, 2024
1ec224a
Execute Bazel Command to run coverage on the test target
Rd4dev Jun 5, 2024
ee92168
Parse the coverage execution result to acquire the generated coverage…
Rd4dev Jun 5, 2024
075c77c
Implemented functionality to TestBazelWorkspace utility to add source…
Rd4dev Jun 7, 2024
d0519d6
Sample Test data and Listing all contents
Rd4dev Jun 9, 2024
b15b7bb
Added Test for BazelClient - for sample target and non target executions
Rd4dev Jun 9, 2024
f441d11
Added Coverage Runner functionality tests
Rd4dev Jun 10, 2024
cc86309
Added tests for the RunCoverageForTestTarget utility
Rd4dev Jun 11, 2024
4a6073b
Replaced Java Test Sources with Kotlin Sources
Rd4dev Jun 13, 2024
13534a1
Fix Static Checks / Lint warnings
Rd4dev Jun 13, 2024
fc52d82
Fix Buildifier Lint checks
Rd4dev Jun 13, 2024
0226baa
Added tests for TestBazelWorkspaceTest to check addition of source an…
Rd4dev Jun 13, 2024
2fe7485
Fix Lint warnings with TestBazelWorkspaceTest
Rd4dev Jun 13, 2024
bd683b5
Fix Regex Pattern checks
Rd4dev Jun 13, 2024
d410d17
Prototyped parsing and reading the extracted file data as byte array
Rd4dev Jun 15, 2024
ece147a
Updated the runCoverageForTestTarget to parse the data file and read …
Rd4dev Jun 15, 2024
b101ec8
Updated Bazel Client Tests with updated execution to return byte array
Rd4dev Jun 15, 2024
5db0650
Updated BazelClientTest for ParseCoverageDataFile and readDatFileAsBi…
Rd4dev Jun 17, 2024
87be114
Updated CoverageRunnerTest to check with updated getCoverage result r…
Rd4dev Jun 17, 2024
456e81b
Updated RunCoverageForTestTargetTest to check the returned coverage data
Rd4dev Jun 17, 2024
2c65524
Update kdoc to match the function paramaters
Rd4dev Jun 17, 2024
62f9e1d
Removing suspend while keeping the async operations
Rd4dev Jun 17, 2024
5e6d2ef
Updated RunCoverageForTestTarget to take in custom process timeout as…
Rd4dev Jun 17, 2024
31b6401
Initialized bazelClient using lazy field
Rd4dev Jun 17, 2024
81d6855
Adjusted visibility and removed unnecessary documentation for private…
Rd4dev Jun 17, 2024
6cb1da7
Revoking visibility of bazelClient functions as that is called outsid…
Rd4dev Jun 17, 2024
b41fb01
Fix Lint Check issues
Rd4dev Jun 17, 2024
e66ac12
Merge branch 'develop' of https://github.com/Rd4dev/oppia-android int…
Rd4dev Jun 17, 2024
c646840
Merge branch 'develop' of https://github.com/Rd4dev/oppia-android int…
Rd4dev Jun 18, 2024
1ffc83d
Introduce new script RunCoverage.kt to later take in source filename …
Rd4dev Jun 18, 2024
248a20a
Merge branch 'develop' of https://github.com/Rd4dev/oppia-android int…
Rd4dev Jun 19, 2024
d817c85
Map file names to appropriate test and localTest names
Rd4dev Jun 19, 2024
7f1be37
Running Multiple test targets using hardcoded test target names
Rd4dev Jun 19, 2024
9e78d1a
Added Kdoc and processTimeout custom argument with basic clean up
Rd4dev Jun 19, 2024
cc5e981
Seperated logic as class and functions, introduced test file with set…
Rd4dev Jun 20, 2024
5075a1a
Added tests for findTestFile()
Rd4dev Jun 20, 2024
fdaf9cb
Added tests case for RunCoverage to check execution of coverage and f…
Rd4dev Jun 20, 2024
8e28f1d
Cleanup of RunCoverageTest
Rd4dev Jun 20, 2024
92bf266
Fix kdoc lint checks and TestBazelWorkspace Tests
Rd4dev Jun 20, 2024
eb7cdc2
Fix Lint Checks on indentation
Rd4dev Jun 20, 2024
ee078e2
Refactored BazelClient coverage execution to return data as list of s…
Rd4dev Jun 21, 2024
f15e087
Fix Lint checks
Rd4dev Jun 21, 2024
506916d
Removed ending periods in KDoc Strings
Rd4dev Jun 21, 2024
c68487d
Merge branch 'develop' of https://github.com/Rd4dev/oppia-android int…
Rd4dev Jun 21, 2024
0f5cffd
Resolved with conflicts to the code_coverage_bazel_command_execution …
Rd4dev Jun 21, 2024
87d34ad
Update the script to align with changes in BazelClient PR 1.1 to retu…
Rd4dev Jun 21, 2024
0735c64
Introduced coverage.proto and parsed the coverage data and store them…
Rd4dev Jun 21, 2024
307214a
Added sha hash and hit count for branches
Rd4dev Jun 21, 2024
bb2cbb2
Isolate the coverage data to only contain the target coverage data
Rd4dev Jun 21, 2024
653ec68
Fix Lint Checks
Rd4dev Jun 21, 2024
46cc3ff
Include coverage data to proto for any related tests - LocalTest and …
Rd4dev Jun 21, 2024
25a064e
Updated the tests to now check with the CoverageReport proto result
Rd4dev Jun 22, 2024
ba1c66a
Fix KDoc String punctuations, more clearer variable names and simplif…
Rd4dev Jun 22, 2024
438476c
Removed unused Paths import
Rd4dev Jun 22, 2024
ac30ca7
Merge branch 'develop' of https://github.com/Rd4dev/oppia-android int…
Rd4dev Jun 22, 2024
c7c2f2f
Fixed indentation and line spaces for better readability
Rd4dev Jun 22, 2024
1eac413
Merge branch 'code_coverage_bazel_command_execution' of https://githu…
Rd4dev Jun 22, 2024
6a2a029
For Reference: Multiple test cases with LocalTest and Test works but …
Rd4dev Jun 22, 2024
e4ede82
For Reference: Added package group for sharedTest and Test, yet multi…
Rd4dev Jun 22, 2024
9e0189d
Added implementation for script module test content files (to be refa…
Rd4dev Jun 22, 2024
d477d20
Refactored the addSourceAndTestFileWithContent to be suitable for mul…
Rd4dev Jun 22, 2024
6720de7
For Reference: Added App cases, but just having sharedTest in package…
Rd4dev Jun 22, 2024
04243f1
KDoC and reverting sharedTest to test, this passes test
Rd4dev Jun 22, 2024
985e861
For Reference: Even doing app/test fails with NPE
Rd4dev Jun 22, 2024
33f9a0b
Made findTestFile private and removed related tests
Rd4dev Jun 22, 2024
5229ecd
Used kotlin patterns to simplify the and make it more readable
Rd4dev Jun 22, 2024
d9ad634
Returning the coverageDataList as immutable list
Rd4dev Jun 22, 2024
ac4c82b
Fix Lint Checks
Rd4dev Jun 23, 2024
6eb90c7
Fixed Multi Level Source and Test File coverage execution by adding t…
Rd4dev Jun 23, 2024
68c714b
Fix Lint Check
Rd4dev Jun 23, 2024
e0be83a
Removed RunCoverageForTestTarget as it is redundant
Rd4dev Jun 23, 2024
c834aa7
Removed the RunCoverageForTestTarget as the functionality will be re-…
Rd4dev Jun 23, 2024
47a6197
Resolved merge conflicts with upstream code_coverage_bazel_command_ex…
Rd4dev Jun 23, 2024
c654eed
Removed the RunCoverageForTestTarget Build inclusions
Rd4dev Jun 23, 2024
3ab4e1e
Resolved merge conflict while syncing with upstream
Rd4dev Jun 23, 2024
0306258
Refactor execute to use separate helper functions
Rd4dev Jun 23, 2024
6179699
Used mapNotNull to avoid mutability
Rd4dev Jun 23, 2024
0c79e4d
Updated with upstream and removed all the optional declarations to ju…
Rd4dev Jun 24, 2024
73f553d
Merge branch 'develop' into code_coverage_bazel_command_execution
BenHenning Jun 24, 2024
0f63469
Merge branch 'develop' into code_coverage_bazel_command_execution
BenHenning Jun 24, 2024
83eb035
Updated with the revised proto structure and added docs
Rd4dev Jun 24, 2024
bc9a5c0
Throw runtime exception when no coverage result is retrieved
Rd4dev Jun 24, 2024
1b9426d
Fix instrumentation and throw error on invalid path
Rd4dev Jun 26, 2024
85b4bcd
Throw error on missing test files
Rd4dev Jun 26, 2024
356e4fb
Removed accidentally added coverage_reports folder
Rd4dev Jun 26, 2024
b4abdde
Restructuring coverage proto to just compute line coverage
Rd4dev Jun 26, 2024
8e07f50
Updated with upstream
Rd4dev Jun 26, 2024
9734a84
Lint fix and Fix failing Test
Rd4dev Jun 26, 2024
f36eb83
Merge branch 'develop' of https://github.com/Rd4dev/oppia-android int…
Rd4dev Jun 26, 2024
6488d53
Merge branch 'code_coverage_bazel_command_execution' of https://githu…
Rd4dev Jun 26, 2024
6f4b76f
Merge branch 'code_coverage_bazel_command_execution' of https://githu…
Rd4dev Jun 26, 2024
7a020a1
Update the test file exemption list retrieval
Rd4dev Jun 26, 2024
4d8b250
Updated from upstream code_coverage_using_filename
Rd4dev Jun 26, 2024
c123365
Updated tests with the proto structure, multi test cases are still fa…
Rd4dev Jun 26, 2024
d6c1f33
Changed mutable lists to lists
Rd4dev Jun 26, 2024
3b6b049
Removed print statement and refactored same package deps
Rd4dev Jun 26, 2024
d88edf6
Fixed improper dep assignment
Rd4dev Jun 26, 2024
4e89da1
Updated with upstream code_coverage_using_filename
Rd4dev Jun 26, 2024
7a837ea
Refactor runCoverageForTarget to return non-nullable CoverageReport
Rd4dev Jun 26, 2024
cba0815
Fix tests in CoverageRunner and Lint checks
Rd4dev Jun 26, 2024
f6b4620
Fix Lint checks
Rd4dev Jun 26, 2024
cf405c6
Added tests for local and sharedTests separately
Rd4dev Jun 26, 2024
88f286b
Fix Lint check, missed separator
Rd4dev Jun 26, 2024
32d9ae4
Fix Coverage Runner Test
Rd4dev Jun 26, 2024
266d0f4
Resolved conflicts with develop branch
Rd4dev Jun 26, 2024
a0bc41c
Resolved conflicting overloads
Rd4dev Jun 26, 2024
a781919
Merge branch 'develop' into code_coverage_store_as_proto
adhiamboperes Jun 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ kt_jvm_library(
visibility = ["//scripts:oppia_script_binary_visibility"],
deps = [
"//scripts/src/java/org/oppia/android/scripts/common:bazel_client",
"//scripts/src/java/org/oppia/android/scripts/proto:coverage_java_proto",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import kotlinx.coroutines.async
import org.oppia.android.scripts.common.BazelClient
import org.oppia.android.scripts.common.CommandExecutor
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
import org.oppia.android.scripts.proto.Coverage
import org.oppia.android.scripts.proto.CoverageReport
import org.oppia.android.scripts.proto.CoveredLine
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.security.MessageDigest

/**
* Class responsible for running coverage analysis asynchronously.
Expand All @@ -30,9 +36,12 @@ class CoverageRunner(
*/
fun runWithCoverageAsync(
bazelTestTarget: String
): Deferred<List<String>?> {
): Deferred<CoverageReport> {
return CoroutineScope(scriptBgDispatcher).async {
retrieveCoverageResult(bazelTestTarget)
val coverageResult = retrieveCoverageResult(bazelTestTarget)
?: error("Failed to retrieve coverage result for $bazelTestTarget")

coverageDataFileLines(coverageResult, bazelTestTarget)
}
}

Expand All @@ -41,4 +50,67 @@ class CoverageRunner(
): List<String>? {
return bazelClient.runCoverageForTestTarget(bazelTestTarget)
}

private fun coverageDataFileLines(
coverageData: List<String>,
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
bazelTestTarget: String
): CoverageReport {
val extractedFileName = "${extractTargetName(bazelTestTarget)}.kt"

val sfStartIdx = coverageData.indexOfFirst {
it.startsWith("SF:") && it.substringAfter("SF:").substringAfterLast("/") == extractedFileName
}
if (sfStartIdx == -1) throw IllegalArgumentException("File not found")
val eofIdx = coverageData.subList(sfStartIdx, coverageData.size).indexOfFirst {
it.startsWith("end_of_record")
}
if (eofIdx == -1) throw IllegalArgumentException("End of record not found")

val fileSpecificCovDatLines = coverageData.subList(sfStartIdx, sfStartIdx + eofIdx + 1)

val coverageDataProps = fileSpecificCovDatLines.groupBy { line ->
line.substringBefore(":")
}.mapValues { (_, lines) ->
lines.map { line ->
line.substringAfter(":").split(",")
}
}

val filePath = coverageDataProps["SF"]?.firstOrNull()?.get(0)
?: throw IllegalArgumentException("File path not found")

val linesFound = coverageDataProps["LF"]?.singleOrNull()?.single()?.toInt() ?: 0
val linesHit = coverageDataProps["LH"]?.singleOrNull()?.single()?.toInt() ?: 0

val coveredLines = coverageDataProps["DA"]?.map { (lineNumStr, hitCountStr) ->
CoveredLine.newBuilder().apply {
this.lineNumber = lineNumStr.toInt()
this.coverage = if (hitCountStr.toInt() > 0) Coverage.FULL else Coverage.NONE
}.build()
}.orEmpty()

val file = File(repoRoot, filePath)
val fileSha1Hash = calculateSha1(file.absolutePath)

return CoverageReport.newBuilder()
.setBazelTestTarget(bazelTestTarget)
.setFilePath(filePath)
.setFileSha1Hash(fileSha1Hash)
.addAllCoveredLine(coveredLines)
.setLinesFound(linesFound)
.setLinesHit(linesHit)
.build()
}
}

private fun extractTargetName(bazelTestTarget: String): String {
val targetName = bazelTestTarget.substringAfterLast(":").trim()
return targetName.removeSuffix("LocalTest").removeSuffix("Test")
}

private fun calculateSha1(filePath: String): String {
val fileBytes = Files.readAllBytes(Paths.get(filePath))
val digest = MessageDigest.getInstance("SHA-1")
val hashBytes = digest.digest(fileBytes)
return hashBytes.joinToString("") { "%02x".format(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 org.oppia.android.scripts.proto.CoverageReport
import org.oppia.android.scripts.proto.TestFileExemptions
import java.io.File
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -78,7 +79,7 @@ class RunCoverage(
* @return a list of lists containing coverage data for each requested test target, if
* the file is exempted from having a test file, an empty list is returned
*/
fun execute(): List<List<String>> {
fun execute(): List<CoverageReport> {
val testFileExemptionList = loadTestFileExemptionsProto(testFileExemptionTextProto)
.testFileExemptionList
.filter { it.testFileNotRequired }
Expand All @@ -97,15 +98,11 @@ class RunCoverage(
val testTargets = bazelClient.retrieveBazelTargets(testFilePaths)

return testTargets.mapNotNull { testTarget ->
val coverageData = runCoverageForTarget(testTarget)
if (coverageData == null) {
println("Coverage data for $testTarget is null")
}
coverageData
runCoverageForTarget(testTarget)
}
}

private fun runCoverageForTarget(testTarget: String): List<String>? {
private fun runCoverageForTarget(testTarget: String): CoverageReport {
return runBlocking {
CoverageRunner(rootDirectory, scriptBgDispatcher, commandExecutor)
.runWithCoverageAsync(testTarget.removeSuffix(".kt"))
Expand Down
11 changes: 11 additions & 0 deletions scripts/src/java/org/oppia/android/scripts/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ java_proto_library(
deps = [":affected_tests_proto"],
)

oppia_proto_library(
name = "coverage_proto",
srcs = ["coverage.proto"],
)

java_proto_library(
name = "coverage_java_proto",
visibility = ["//scripts:oppia_script_library_visibility"],
deps = [":coverage_proto"],
)

oppia_proto_library(
name = "filename_pattern_validation_checks_proto",
srcs = ["filename_pattern_validation_checks.proto"],
Expand Down
40 changes: 40 additions & 0 deletions scripts/src/java/org/oppia/android/scripts/proto/coverage.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";

package proto;

option java_package = "org.oppia.android.scripts.proto";
option java_multiple_files = true;

// Coverage Report that contains the bazel coverage data retrieved from the
// Bazel coverage execution.
message CoverageReport {
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
// The test target for which the coverage report is generated.
string bazel_test_target = 1;
// The relative path of the covered file.
string file_path = 2;
// SHA-1 hash of the file content at the time of report (to guard against changes).
string file_sha1_hash = 3;
// The lines of code covered in the report.
repeated CoveredLine covered_line = 4;
// The total number of lines found in the covered file.
int32 lines_found = 5;
// The total number of lines hit in the covered file.
int32 lines_hit = 6;
}

// Information about a single line that was covered during the tests.
message CoveredLine {
// The line number of the covered line.
int32 line_number = 1;
// The coverage status of the covered line.
Coverage coverage = 2;
}

enum Coverage {
// Coverage status is unspecified.
UNSPECIFIED = 0;
// The line, branch, or function is fully covered, ie. executed atleast once.
FULL = 1;
// The line, branch, or function is not covered at all.
NONE = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class TestBazelWorkspace(private val temporaryRootFolder: TemporaryFolder) {
*/
fun addSourceAndTestFileWithContent(
filename: String,
testFilename: String,
sourceContent: String,
testContent: String,
sourceSubpackage: String,
Expand All @@ -73,10 +74,9 @@ class TestBazelWorkspace(private val temporaryRootFolder: TemporaryFolder) {
sourceSubpackage
)

val testFileName = "${filename}Test"
addTestContentAndBuildFile(
filename,
testFileName,
testFilename,
testContent,
sourceSubpackage,
testSubpackage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ class BazelClientTest {

testBazelWorkspace.addSourceAndTestFileWithContent(
filename = "TwoSum",
testFilename = "TwoSumTest",
sourceContent = sourceContent,
testContent = testContent,
sourceSubpackage = "coverage/main/java/com/example",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.oppia.android.scripts.common.CommandExecutorImpl
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
import org.oppia.android.scripts.proto.Coverage
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.util.concurrent.TimeUnit
Expand Down Expand Up @@ -103,6 +106,7 @@ class CoverageRunnerTest {

testBazelWorkspace.addSourceAndTestFileWithContent(
filename = "TwoSum",
testFilename = "TwoSumTest",
sourceContent = sourceContent,
testContent = testContent,
sourceSubpackage = "coverage/main/java/com/example",
Expand All @@ -114,28 +118,38 @@ class CoverageRunnerTest {
"//coverage/test/java/com/example:TwoSumTest"
).await()
}
val expectedResult = listOf(
"SF:coverage/main/java/com/example/TwoSum.kt",
"FN:7,com/example/TwoSum${'$'}Companion::sumNumbers (II)Ljava/lang/Object;",
"FN:3,com/example/TwoSum::<init> ()V",
"FNDA:1,com/example/TwoSum${'$'}Companion::sumNumbers (II)Ljava/lang/Object;",
"FNDA:0,com/example/TwoSum::<init> ()V",
"FNF:2",
"FNH:1",
"BRDA:7,0,0,1",
"BRDA:7,0,1,1",
"BRDA:7,0,2,1",
"BRDA:7,0,3,1",
"BRF:4",
"BRH:4",
"DA:3,0",
"DA:7,1",
"DA:8,1",
"DA:10,1",
"LH:3",
"LF:4",
"end_of_record"
)

val expectedResult = CoverageReport.newBuilder()
.setBazelTestTarget("//coverage/test/java/com/example:TwoSumTest")
.setFilePath("coverage/main/java/com/example/TwoSum.kt")
.setFileSha1Hash("f6fb075e115775f6729615a79f0e7e34fe9735b5")
.addCoveredLine(
CoveredLine.newBuilder()
.setLineNumber(3)
.setCoverage(Coverage.NONE)
.build()
)
.addCoveredLine(
CoveredLine.newBuilder()
.setLineNumber(7)
.setCoverage(Coverage.FULL)
.build()
)
.addCoveredLine(
CoveredLine.newBuilder()
.setLineNumber(8)
.setCoverage(Coverage.FULL)
.build()
)
.addCoveredLine(
CoveredLine.newBuilder()
.setLineNumber(10)
.setCoverage(Coverage.FULL)
.build()
)
.setLinesFound(4)
.setLinesHit(3)
.build()

assertThat(result).isEqualTo(expectedResult)
}
Expand Down
Loading
Loading