Skip to content

Commit

Permalink
Added sha hash and hit count for branches
Browse files Browse the repository at this point in the history
  • Loading branch information
Rd4dev committed Jun 21, 2024
1 parent 0735c64 commit 307214a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import org.oppia.android.scripts.common.BazelClient
import org.oppia.android.scripts.common.CommandExecutor
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
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 Down Expand Up @@ -48,12 +51,6 @@ class CoverageRunner(
}
}

/**
* Runs coverage command for the Bazel test target
*
* @param bazelTestTarget Bazel test target to analyze coverage
* @return the generated coverage data as a string
*/
private fun retrieveCoverageResult(
bazelTestTarget: String
): List<String>? {
Expand Down Expand Up @@ -105,6 +102,7 @@ class CoverageRunner(
.setLineNumber(lineNumber)
.setBlockNumber(blockNumber)
.setBranchNumber(branchNumber)
.setHitCount(hitCount)
.setCoverage(coverage)
.build()
)
Expand Down Expand Up @@ -164,8 +162,12 @@ class CoverageRunner(
}
}

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

val coveredFile = CoveredFile.newBuilder()
.setFilePath(filePath)
.setFileSha1Hash(fileSha1Hash)
.addAllCoveredLine(coveredLines)
.addAllBranchCoverage(branchCoverage)
.addAllFunctionCoverage(functionCoverage)
Expand All @@ -182,5 +184,11 @@ class CoverageRunner(
.addCoveredFile(coveredFile)
.build()
}
}

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 @@ -39,7 +39,8 @@ message BranchCoverage {
int32 line_number = 1;
optional int32 block_number = 2;
optional int32 branch_number = 3;
BranchCoverage.Coverage coverage = 4;
optional int32 hit_count = 4;
BranchCoverage.Coverage coverage = 5;

enum Coverage {
UNSPECIFIED = 0;
Expand Down

0 comments on commit 307214a

Please sign in to comment.