From 9bfeeff177d9c1e300d93f28b1c6171c3dfa4491 Mon Sep 17 00:00:00 2001 From: Rd Date: Mon, 24 Jun 2024 18:28:44 +0530 Subject: [PATCH] Saving the MARKDOWN reports --- .gitignore | 1 + .../scripts/coverage/CoverageReporter.kt | 20 ++++++++++++--- .../android/scripts/coverage/RunCoverage.kt | 25 +++++++++++++++---- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 531f05c8a20..224c3b3ef25 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ bazel-* .bazelproject .aswb *.pb +coverage_reports diff --git a/scripts/src/java/org/oppia/android/scripts/coverage/CoverageReporter.kt b/scripts/src/java/org/oppia/android/scripts/coverage/CoverageReporter.kt index 23dbbb2fb26..63f6ae4de73 100644 --- a/scripts/src/java/org/oppia/android/scripts/coverage/CoverageReporter.kt +++ b/scripts/src/java/org/oppia/android/scripts/coverage/CoverageReporter.kt @@ -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) { +class CoverageReporter( + private val coverageReportList: List, + 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() } @@ -20,7 +26,7 @@ class CoverageReporter(private val coverageReportList: List) { val (totalFunctionsFound, totalFunctionsHit) = computeTotalsFor("functions") val (totalBranchesFound, totalBranchesHit) = computeTotalsFor("branches") - return """ + val markdownReport = """ # Coverage Report **Total coverage:** @@ -32,6 +38,12 @@ class CoverageReporter(private val coverageReportList: List) { - **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 { diff --git a/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt b/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt index add9778896a..6ee036d0e24 100644 --- a/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt +++ b/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt @@ -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 @@ -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("=") @@ -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() } } @@ -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 ) { @@ -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.") @@ -153,6 +158,16 @@ private fun findTestFile(repoRoot: String, filePath: String): List { .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 ->