From 15061ba821641f78fa9595e3513cef5c73379d55 Mon Sep 17 00:00:00 2001 From: Rd Date: Mon, 22 Jul 2024 15:18:57 +0530 Subject: [PATCH] Adding workflow_run to trigger coverage workflow only when unit tests are completed --- .github/workflows/code_coverage.yml | 14 ++++-- .../android/scripts/common/BazelClient.kt | 46 +++++-------------- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index c8e56dcd5e8..9de81fad0fd 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -5,12 +5,16 @@ name: Code Coverage # Controls when the action will run. Triggers the workflow on pull request # events or push events in the develop branch. on: + workflow_run: + workflows: ["Unit Tests (Robolectric - Bazel)"] + types: + - completed workflow_dispatch: - pull_request: - push: - branches: - # Push events on develop branch - - develop +# pull_request: +# push: +# branches: +# # Push events on develop branch +# - develop jobs: compute_changed_files: diff --git a/scripts/src/java/org/oppia/android/scripts/common/BazelClient.kt b/scripts/src/java/org/oppia/android/scripts/common/BazelClient.kt index e4f97d472b3..8aadac080ef 100644 --- a/scripts/src/java/org/oppia/android/scripts/common/BazelClient.kt +++ b/scripts/src/java/org/oppia/android/scripts/common/BazelClient.kt @@ -162,10 +162,10 @@ class BazelClient(private val rootDirectory: File, private val commandExecutor: "attr(name, $targetName, $targetPath)", "--output=build", ) - println("BUILD: $buildRule") - println("BUILD has shard count: ${buildRule.any { "shard_count" in it }}") +// println("BUILD: $buildRule") +// println("BUILD has shard count: ${buildRule.any { "shard_count" in it }}") - println("Printing for the bazel test target: $bazelTestTarget") +// println("Printing for the bazel test target: $bazelTestTarget") // println(File(rootDirectory,"/bazel-out/_coverage/_coverage_report.dat").exists()) // println(File(rootDirectory,"/bazel-out/_coverage/_coverage_report.dat").readText()) /*return parseCoverageDataFilePath(coverageCommandOutputLines)?.let { path -> @@ -174,47 +174,25 @@ class BazelClient(private val rootDirectory: File, private val commandExecutor: val hasShardCount = buildRule.any { "shard_count" in it } if (hasShardCount) { - println("In has Shard count") - val coverageCommandOutputLines = executeBazelCommand( -// executeBazelCommand( +// println("In has Shard count") +// val coverageCommandOutputLines = executeBazelCommand( + executeBazelCommand( "test", "--collect_code_coverage", "--combined_report=lcov", bazelTestTarget, "--instrumentation_filter=$computeInstrumentation" ) - println( + /*println( "Coverage command output lines " + "with bazel tests --collect coverage: $coverageCommandOutputLines" - ) - - val regex = """(.*/shard_\d+_of_\d+/coverage\.dat)""".toRegex() - val shardCoveragePaths = coverageCommandOutputLines - .flatMap { regex.findAll(it).map { matchResult -> - matchResult.value.trim() - } - } - - println("Shard coverage path: $shardCoveragePaths") - - val lcovCommand = buildString { - append("lcov ") - shardCoveragePaths.forEach { path -> - append("--add-tracefile $path ") - } - append("--output-file ${rootDirectory}/coverage_reports/lcov_combined.dat") - } - - val process = ProcessBuilder(*lcovCommand.split(" ").toTypedArray()) - .redirectErrorStream(true) - .start() - - val output = process.inputStream.bufferedReader().readText() - println("Output: $output") + )*/ - return File(rootDirectory, "/bazel-out/_coverage/_coverage_report.dat").readLines() + return File(rootDirectory, "/bazel-out/_coverage/_coverage_report.dat") + .takeIf { it.exists() && it.isFile } + ?.readLines() } else { - println("In does not have Shard count") +// println("In does not have Shard count") val coverageCommandOutputLines = executeBazelCommand( // executeBazelCommand( "coverage",