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: Generate Code Coverage Report in HTML and MARKDOWN formats #5443

Merged
merged 128 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 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
7e10505
Introduced Coverage Reporter script to generate HTML and MARKDOWN rep…
Rd4dev Jun 24, 2024
9bfeeff
Saving the MARKDOWN reports
Rd4dev Jun 24, 2024
5441356
Generate HTML reports - hardcoded messy implementation for base layout
Rd4dev Jun 25, 2024
aed9857
Generate HTML for just one target
Rd4dev Jun 25, 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
c4c7ead
Updated with upstream code_coverage_store_proto
Rd4dev Jun 26, 2024
32d9ae4
Fix Coverage Runner Test
Rd4dev Jun 26, 2024
402f33f
Merge branch 'code_coverage_store_as_proto' of https://github.com/opp…
Rd4dev Jun 26, 2024
a17377a
Cleaned the md report generation to just hold line coverage data
Rd4dev Jun 26, 2024
12eecfe
Cleaned the html report generation to just hold line coverage data
Rd4dev Jun 26, 2024
9b28dca
Code cleanup - refactored redundant code
Rd4dev Jun 26, 2024
266d0f4
Resolved conflicts with develop branch
Rd4dev Jun 26, 2024
a0bc41c
Resolved conflicting overloads
Rd4dev Jun 26, 2024
098a057
Resolve merge conflicts with upstream code_coverage_store_proto
Rd4dev Jun 26, 2024
01e0ad9
Added tests for CoverageReporter utility
Rd4dev Jun 27, 2024
6416569
Renamed test case names to be more crisp and clear
Rd4dev Jun 27, 2024
3bf9919
Added tests for RunCoverage to check md and html reports
Rd4dev Jun 27, 2024
e1e2db3
Fix Lint checks and added kdocs
Rd4dev Jun 27, 2024
821725f
Fixed failing tests and fixed kdoc checks
Rd4dev Jun 27, 2024
2b97b51
Fix test cases that failed due to wrong output path
Rd4dev Jun 27, 2024
d6fb599
Fix test case that had mis-matched outputs
Rd4dev Jun 27, 2024
036ab34
Resolved conflicts with develop branch
Rd4dev Jun 27, 2024
032abcd
Fix Lint checks on unused imports
Rd4dev Jun 27, 2024
f080cba
Refactor to ignore case formats and updated the html styling with tests
Rd4dev Jun 28, 2024
a2273ec
Updated RunCoverageTest with update to html stylings
Rd4dev Jun 28, 2024
5d0bb7e
Fixed failing test case that failed due to changes in file and unchan…
Rd4dev Jun 28, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ bazel-*
.bazelproject
.aswb
*.pb
coverage_reports
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 14 additions & 0 deletions scripts/src/java/org/oppia/android/scripts/coverage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ kt_jvm_library(
],
visibility = ["//scripts:oppia_script_binary_visibility"],
deps = [
":coverage_reporter",
":coverage_runner",
"//scripts/src/java/org/oppia/android/scripts/common:bazel_client",
"//scripts/src/java/org/oppia/android/scripts/proto:script_exemptions_java_proto",
Expand All @@ -30,3 +31,16 @@ kt_jvm_library(
"//scripts/src/java/org/oppia/android/scripts/proto:coverage_java_proto",
],
)

kt_jvm_library(
name = "coverage_reporter",
testonly = True,
srcs = [
"CoverageReporter.kt",
],
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
@@ -0,0 +1,236 @@
package org.oppia.android.scripts.coverage

import org.oppia.android.scripts.proto.Coverage
import org.oppia.android.scripts.proto.CoverageReport
import java.io.File

/**
* Class responsible for generating rich text coverage report.
*
* @param repoRoot the root directory of the repository
* @param coverageReportList the list of coverage data proto
* @param reportFormat the format in which the report will be generated
*/
class CoverageReporter(
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
private val repoRoot: String,
private val coverageReportList: List<CoverageReport>,
private val reportFormat: ReportFormat,
) {
private val computedCoverageRatio = computeCoverageRatio()
private val formattedCoveragePercentage = "%.2f".format(computedCoverageRatio * 100)

private val filePath = coverageReportList.firstOrNull()?.filePath ?: "Unknown"

private val totalLinesFound = coverageReportList.getOrNull(0)?.linesFound ?: 0
private val totalLinesHit = coverageReportList.getOrNull(0)?.linesHit ?: 0

/**
* Generates a rich text report for the analysed coverage data based on the specified format.
* It supports Markdown and HTML formats.
*
* @return a pair where the first value is the computed coverage ratio represented in [0, 1]
* and the second value is the generated report text
*/
fun generateRichTextReport(): Pair<Float, String> {
println("report format: $reportFormat")
return when (reportFormat) {
ReportFormat.MARKDOWN -> generateMarkdownReport()
ReportFormat.HTML -> generateHtmlReport()
}
}

private fun generateMarkdownReport(): Pair<Float, String> {
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
val markdownContent =
"""
## Coverage Report

- **Covered File:** $filePath
- **Coverage percentage:** $formattedCoveragePercentage% covered
- **Line coverage:** $totalLinesHit / $totalLinesFound lines covered
""".trimIndent()

println("\n$markdownContent")

return Pair(computedCoverageRatio, markdownContent)
}

private fun generateHtmlReport(): Pair<Float, String> {
var htmlContent =
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Coverage Report</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
padding: 8px;
margin-left: 20px;
text-align: left;
border-bottom: 1px solid #fdfdfd;
}
.line-number-col {
width: 2%;
}
.line-number-row {
border-right: 1px dashed #000000
}
.source-code-col {
width: 98%;
}
.covered-line, .not-covered-line, .uncovered-line {
white-space: pre-wrap;
word-wrap: break-word;
box-sizing: border-box;
border-radius: 4px;
padding: 2px 8px 2px 4px;
display: inline-block;
}
.covered-line {
background-color: #c8e6c9; /* Light green */
}
.not-covered-line {
background-color: #ffcdd2; /* Light red */
}
.uncovered-line {
background-color: #f1f1f1; /* light gray */
}
.coverage-summary {
margin-bottom: 20px;
}
h2 {
text-align: center;
}
ul {
list-style-type: none;
padding: 0;
text-align: center;
}
.summary-box {
background-color: #f0f0f0;
border: 1px solid #ccc;
border-radius: 8px;
padding: 10px;
margin-bottom: 20px;
display: flex;
justify-content: space-between;
align-items: flex-start;
}
.summary-left {
text-align: left;
}
.summary-right {
text-align: right;
}
.legend {
display: flex;
align-items: center;
}
.legend-item {
width: 20px;
height: 10px;
margin-right: 5px;
border-radius: 2px;
display: inline-block;
}
.legend .covered {
background-color: #c8e6c9; /* Light green */
}
.legend .not-covered {
margin-left: 4px;
background-color: #ffcdd2; /* Light red */
}
@media screen and (max-width: 768px) {
body {
padding: 10px;
}
table {
width: auto;
}
}
</style>
</head>
<body>
<h2>Coverage Report</h2>
<div class="summary-box">
<div class="summary-left">
<strong>Covered File:</strong> $filePath <br>
<div class="legend">
<div class="legend-item covered"></div>
<span>Covered</span>
<div class="legend-item not-covered"></div>
<span>Uncovered</span>
</div>
</div>
<div class="summary-right">
<div><strong>Coverage percentage:</strong> $formattedCoveragePercentage%</div>
<div><strong>Line coverage:</strong> $totalLinesHit / $totalLinesFound covered</div>
</div>
</div>
<table>
<thead>
<tr>
<th class="line-number-col">Line No</th>
<th class="source-code-col">Source Code</th>
</tr>
</thead>
<tbody>
""".trimIndent()

val fileContent = File(repoRoot, filePath).readLines()
val coverageMap = coverageReportList
.firstOrNull()?.coveredLineList?.associateBy { it.lineNumber }

fileContent.forEachIndexed { index, line ->
val lineNumber = index + 1
val lineClass = when (coverageMap?.get(lineNumber)?.coverage) {
Coverage.FULL -> "covered-line"
Coverage.NONE -> "not-covered-line"
else -> "uncovered-line"
}
htmlContent += """
<tr>
<td class="line-number-row">${lineNumber.toString().padStart(4, ' ')}</td>
<td class="$lineClass">$line</td>
</tr>
""".trimIndent()
}

htmlContent += """
</tbody>
</table>
</body>
</html>
""".trimIndent()

return Pair(computedCoverageRatio, htmlContent)
}

private fun computeCoverageRatio(): Float {
val report = coverageReportList.getOrNull(0)
return if (report != null && report.linesFound != 0) {
report.linesHit.toFloat() / report.linesFound.toFloat()
} else {
0f
}
}
}

/** Represents the different types of formats available to generate code coverage reports. */
enum class ReportFormat {
/** Indicates that the report should be formatted in .md format. */
MARKDOWN,
/** Indicates that the report should be formatted in .html format. */
HTML
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ class CoverageRunner(
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import java.util.concurrent.TimeUnit
* Arguments:
* - path_to_root: directory path to the root of the Oppia Android repository.
* - relative_path_to_file: the relative path to the file to analyse coverage
* - reportFormat: the format of the coverage report. Defaults to MARKDOWN if not specified.
* Available options: MARKDOWN, HTML.
*
* Example:
* bazel run //scripts:run_coverage -- $(pwd)
* utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt
* utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt HTML
* Example with custom process timeout:
* bazel run //scripts:run_coverage -- $(pwd)
* utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt processTimeout=10
Expand All @@ -31,6 +33,14 @@ import java.util.concurrent.TimeUnit
fun main(vararg args: String) {
val repoRoot = args[0]
val filePath = args[1]
val format = args.getOrNull(2)
val reportFormat = when {
format.equals("HTML", ignoreCase = true) -> ReportFormat.HTML
format.equals("MARKDOWN", ignoreCase = true) || format == null -> ReportFormat.MARKDOWN
else -> throw IllegalArgumentException("Unsupported report format: $format")
}

val reportOutputPath = getReportOutputPath(repoRoot, filePath, reportFormat)

if (!File(repoRoot, filePath).exists()) {
error("File doesn't exist: $filePath.")
Expand All @@ -45,7 +55,14 @@ fun main(vararg args: String) {
scriptBgDispatcher, processTimeout = processTimeout, processTimeoutUnit = TimeUnit.MINUTES
)

RunCoverage(repoRoot, filePath, commandExecutor, scriptBgDispatcher).execute()
RunCoverage(
repoRoot,
filePath,
reportFormat,
reportOutputPath,
commandExecutor,
scriptBgDispatcher
).execute()
}
}

Expand All @@ -60,6 +77,8 @@ fun main(vararg args: String) {
class RunCoverage(
private val repoRoot: String,
private val filePath: String,
private val reportFormat: ReportFormat,
private val reportOutputPath: String,
private val commandExecutor: CommandExecutor,
private val scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher
) {
Expand All @@ -79,15 +98,14 @@ 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<CoverageReport> {
fun execute(): String {
val testFileExemptionList = loadTestFileExemptionsProto(testFileExemptionTextProto)
.testFileExemptionList
.filter { it.testFileNotRequired }
.map { it.exemptedFilePath }

if (filePath in testFileExemptionList) {
println("This file is exempted from having a test file; skipping coverage check.")
return emptyList()
return "This file is exempted from having a test file; skipping coverage check."
}

val testFilePaths = findTestFile(repoRoot, filePath)
Expand All @@ -97,9 +115,26 @@ class RunCoverage(

val testTargets = bazelClient.retrieveBazelTargets(testFilePaths)

return testTargets.mapNotNull { testTarget ->
val coverageReports = testTargets.mapNotNull { testTarget ->
runCoverageForTarget(testTarget)
}

coverageReports.takeIf { it.isNotEmpty() }?.run {
val reporter = CoverageReporter(repoRoot, this, reportFormat)
val (computedCoverageRatio, reportText) = reporter.generateRichTextReport()

File(reportOutputPath).apply {
parentFile?.mkdirs()
writeText(reportText)
}

if (File(reportOutputPath).exists()) {
println("\nComputed Coverage Ratio is: $computedCoverageRatio")
println("\nGenerated report at: $reportOutputPath\n")
}
} ?: println("No coverage reports generated.")

return reportOutputPath
}

private fun runCoverageForTarget(testTarget: String): CoverageReport {
Expand Down Expand Up @@ -136,6 +171,19 @@ private fun findTestFile(repoRoot: String, filePath: String): List<String> {
.map { it.relativeTo(repoRootFile).path }
}

private fun getReportOutputPath(
repoRoot: String,
filePath: String,
reportFormat: ReportFormat
): String {
val fileWithoutExtension = filePath.substringBeforeLast(".")
val defaultFilename = when (reportFormat) {
ReportFormat.HTML -> "coverage.html"
ReportFormat.MARKDOWN -> "coverage.md"
}
return "$repoRoot/coverage_reports/$fileWithoutExtension/$defaultFilename"
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
}

private fun loadTestFileExemptionsProto(testFileExemptiontextProto: String): TestFileExemptions {
return File("$testFileExemptiontextProto.pb").inputStream().use { stream ->
TestFileExemptions.newBuilder().also { builder ->
Expand Down
Loading
Loading