Skip to content

Commit

Permalink
Saving the MARKDOWN reports
Browse files Browse the repository at this point in the history
  • Loading branch information
Rd4dev committed Jun 24, 2024
1 parent 7e10505 commit 9bfeeff
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.oppia.android.scripts.coverage

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

class CoverageReporter(private val coverageReportList: List<CoverageReport>) {
class CoverageReporter(
private val coverageReportList: List<CoverageReport>,
private val reportFormat: ReportFormat,
private val reportOutputPath: String
) {

fun generateRichTextReport(format: ReportFormat, computedCoverageRatio: Float): String {
return when (format) {
fun generateRichTextReport(computedCoverageRatio: Float): String {
println("output: $reportOutputPath")
return when (reportFormat) {
ReportFormat.MARKDOWN -> generateMarkdownReport(computedCoverageRatio)
ReportFormat.HTML -> generateHtmlReport()
}
Expand All @@ -20,7 +26,7 @@ class CoverageReporter(private val coverageReportList: List<CoverageReport>) {
val (totalFunctionsFound, totalFunctionsHit) = computeTotalsFor("functions")
val (totalBranchesFound, totalBranchesHit) = computeTotalsFor("branches")

return """
val markdownReport = """
# Coverage Report
**Total coverage:**
Expand All @@ -32,6 +38,12 @@ class CoverageReporter(private val coverageReportList: List<CoverageReport>) {
- **Branch coverage:** $totalBranchesFound covered / $totalBranchesHit found
""".trimIndent()

val outputFile = File(reportOutputPath)
outputFile.parentFile?.mkdirs()
outputFile.writeText(markdownReport)

return reportOutputPath
}

private fun computeTotalsFor(type: String): Pair<Int, Int> {
Expand Down
25 changes: 20 additions & 5 deletions scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt
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 @@ -33,10 +35,12 @@ fun main(vararg args: String) {
val filePath = args[1]
val reportFormat = when (args.getOrNull(2)) {
"HTML" -> ReportFormat.HTML
"MARKDOWN", null -> ReportFormat.MARKDOWN // Default to MARKDOWN if not specified
"MARKDOWN", null -> ReportFormat.MARKDOWN
else -> throw IllegalArgumentException("Unsupported report format: ${args[2]}")
}

val reportOutputPath = getReportOutputPath(repoRoot, filePath, reportFormat)

ScriptBackgroundCoroutineDispatcher().use { scriptBgDispatcher ->
val processTimeout: Long = args.find { it.startsWith("processTimeout=") }
?.substringAfter("=")
Expand All @@ -46,7 +50,7 @@ fun main(vararg args: String) {
scriptBgDispatcher, processTimeout = processTimeout, processTimeoutUnit = TimeUnit.MINUTES
)

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

Expand All @@ -62,6 +66,7 @@ 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 Down Expand Up @@ -108,9 +113,9 @@ class RunCoverage(
}

if (coverageReports.isNotEmpty()) {
val reporter = CoverageReporter(coverageReports)
val reporter = CoverageReporter(coverageReports, reportFormat, reportOutputPath)
val coverageRatio = reporter.computeCoverageRatio()
val generatedReport = reporter.generateRichTextReport(reportFormat, coverageRatio)
val generatedReport = reporter.generateRichTextReport(coverageRatio)
println("Generated report: $generatedReport")
} else {
println("No coverage reports generated.")
Expand Down Expand Up @@ -153,6 +158,16 @@ private fun findTestFile(repoRoot: String, filePath: String): List<String> {
.map { it.relativeTo(repoRootFile).path }
}

private fun getReportOutputPath(repoRoot: String, filePath: String, reportFormat: ReportFormat): String {
println("Repo root: $repoRoot")
val fileWithoutExtension = filePath.substringBeforeLast(".")
val defaultFilename = when (reportFormat) {
ReportFormat.HTML -> "coverage.html"
ReportFormat.MARKDOWN -> "coverage.md"
}
return "$repoRoot/coverage_reports/$fileWithoutExtension/$defaultFilename"
}

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

0 comments on commit 9bfeeff

Please sign in to comment.