From ba16aaf08cb423c7b1c6a5da7516c451a91c777d Mon Sep 17 00:00:00 2001 From: Rd Date: Thu, 11 Jul 2024 18:18:30 +0530 Subject: [PATCH] Refactor to take in a list of file paths as args for both cli and ci --- scripts/assets/test_file_exemptions.textproto | 4 - .../android/scripts/coverage/RunCoverage.kt | 86 ++++++++++--------- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/scripts/assets/test_file_exemptions.textproto b/scripts/assets/test_file_exemptions.textproto index 1b873b622a0..d1ff255fc64 100644 --- a/scripts/assets/test_file_exemptions.textproto +++ b/scripts/assets/test_file_exemptions.textproto @@ -1,7 +1,3 @@ -test_file_exemption { - exempted_file_path: "utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt" - override_min_coverage_percent_required: 388 -} test_file_exemption { exempted_file_path: "app/src/main/java/org/oppia/android/app/activity/ActivityComponent.kt" test_file_not_required: true 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 2f7dcfcada8..95bf80a4eb6 100644 --- a/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt +++ b/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt @@ -18,38 +18,47 @@ import java.util.concurrent.TimeUnit * Entry point function for running coverage analysis for a source file. * * Usage: - * bazel run //scripts:run_coverage_for_test_target -- + * bazel run //scripts:run_coverage_for_test_target -- * * 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. + * - list_of_relative_path_to_files: the list of relative path to the files to analyse coverage + * - reportFormat: the format of the coverage report. Defaults to HTML if not specified. + * Available options: MARKDOWN, HTML. + * - processTimeout: The amount of time that should be waited before considering a process as 'hung', + * in minutes. * * Example: - * bazel run //scripts:run_coverage -- $(pwd) - * utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt format=HTML + * bazel run //scripts:run_coverage -- $(pwd) + * utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt --format=HTML + * + * Example with list of files: + * 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/math/MathTokenizer.kt --format=MARKDOWN + * * Example with custom process timeout: - * bazel run //scripts:run_coverage -- $(pwd) - * utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt processTimeout=15 + * bazel run //scripts:run_coverage -- $(pwd) + * utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt --processTimeout=15 * */ fun main(vararg args: String) { val repoRoot = args[0] - val filePath = args[1] - println("File Path: $filePath") - - // TODO: once the file list is received (git client), it need to be filtered to just have - // .kt files and also not Test.kt files - val filePaths = listOf( - "utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt", - "app/src/main/java/org/oppia/android/app/activity/ActivityComponent.kt", - "utility/src/main/java/org/oppia/android/util/math/NumericExpressionEvaluator.kt", - "utility/src/main/java/org/oppia/android/util/math/MathTokenizer.kt", - "utility/src/main/java/org/oppia/android/util/math/RealExtensions.kt", - ) - - val format = args.find { it.startsWith("format=", ignoreCase = true) } + + val filePathList = args.drop(1) + .takeWhile { !it.startsWith("--") } + .map { it.trim(',', '[', ']') } + .filter { it.endsWith(".kt") && !it.endsWith("Test.kt") } + + for (file in filePathList) { + if (!File(repoRoot, file).exists()) { + error("File doesn't exist: $file") + } + } + + println("Running coverage analysis for the files: $filePathList") + + val format = args.find { it.startsWith("--format=", ignoreCase = true) } ?.substringAfter("=") ?.uppercase() ?: "HTML" @@ -59,14 +68,8 @@ fun main(vararg args: String) { else -> throw IllegalArgumentException("Unsupported report format: $format") } - val reportOutputPath = getReportOutputPath(repoRoot, filePath, reportFormat) - - if (!File(repoRoot, filePath).exists()) { - error("File doesn't exist: $filePath.") - } - ScriptBackgroundCoroutineDispatcher().use { scriptBgDispatcher -> - val processTimeout: Long = args.find { it.startsWith("processTimeout=") } + val processTimeout: Long = args.find { it.startsWith("--processTimeout=") } ?.substringAfter("=") ?.toLongOrNull() ?: 10 @@ -76,9 +79,8 @@ fun main(vararg args: String) { RunCoverage( repoRoot, - filePaths, + filePathList, reportFormat, - reportOutputPath, commandExecutor, scriptBgDispatcher ).execute() @@ -95,9 +97,8 @@ fun main(vararg args: String) { */ class RunCoverage( private val repoRoot: String, - private val filePaths: List, + private val filePathList: List, private val reportFormat: ReportFormat, - private val reportOutputPath: String, private val commandExecutor: CommandExecutor, private val scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher ) { @@ -123,7 +124,7 @@ class RunCoverage( * coverage analysis for each test target found. */ fun execute() = runBlocking { - val coverageResults = filePaths.map { filePath -> + val coverageResults = filePathList.map { filePath -> async { runCoverageForFile(filePath) } @@ -174,13 +175,15 @@ class RunCoverage( val coverageCheckThreshold = exemption?.overrideMinCoveragePercentRequired ?: MIN_THRESHOLD - if (computedCoverageRatio * 100 < coverageCheckThreshold) { - coverageCheckState = CoverageCheck.FAIL - reportText += "|:x:|" - } else { - reportText += "|:white_check_mark:|" - } + coverageCheckState = computedCoverageRatio.takeIf { it * 100