forked from oppia/oppia-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix part of oppia#5343: Introduce Utilities to execute code coverage …
…for a specific bazel test target (oppia#5423) <!-- READ ME FIRST: Please fill in the explanation section below and check off every point from the Essential Checklist! --> ## Explanation <!-- - Explain what your PR does. If this PR fixes an existing bug, please include - "Fixes #bugnum:" in the explanation so that GitHub can auto-close the issue - when this PR is merged. --> Fixes part of oppia#5343 ### Project [PR 1.1 of Project 4.1] ### This PR introduces 2 utilities: 1. **RunCoverageForTestTarget** - A new script for running code coverage locally 2. **CoverageRunner** - A new scripting utility for running code coverage for a selected target ### Source: - The RunCoverageForTestTarget script takes in a test target as an argument. - The CoverageRunner executes the bazel coverage command asynchronously utilizing bazelClient. - Implementation is added to BazelClient to execute code coverage commands. - The getCoverage() from CoverageRunner returns a list of string containing the output result. - The result is parsed via parseCoverageDataFile() to extract the coverage.dat file path. The extracted coverage.dat file will then be used to acquire the actual coverage data. ### Tests: - Tests are introduced for RunCoverageForTestTarget and CoverageRunner utilities. - To test the execution, sample test files are generated and utilized for testing. - Sample test files include - - [sourceFile.kt, BUILD.bazel], - [testFile.kt,BUILD.bazel], - WORKSPACE in their appropriate subpackages - To add functionality to introduce source and test files with content TestBazelWorkspace utility is utilized. - The addSourceAndTestFileWithContent() in TestBazelWorkspace utility achieves this. - Tests are added for - BazelClientTest - TestBazelWorkspaceTest ## Essential Checklist <!-- Please tick the relevant boxes by putting an "x" in them. --> - [x] The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".) - [x] Any changes to [scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets) files have their rationale included in the PR explanation. - [x] The PR follows the [style guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide). - [x] The PR does not contain any unnecessary code changes from Android Studio ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)). - [x] The PR is made from a branch that's **not** called "develop" and is up-to-date with "develop". - [x] The PR is **assigned** to the appropriate reviewers ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)). ## For UI-specific PRs only <!-- Delete these section if this PR does not include UI-related changes. --> If your PR includes UI-related changes, then: - Add screenshots for portrait/landscape for both a tablet & phone of the before & after UI changes - For the screenshots above, include both English and pseudo-localized (RTL) screenshots (see [RTL guide](https://github.com/oppia/oppia-android/wiki/RTL-Guidelines)) - Add a video showing the full UX flow with a screen reader enabled (see [accessibility guide](https://github.com/oppia/oppia-android/wiki/Accessibility-A11y-Guide)) - For PRs introducing new UI elements or color changes, both light and dark mode screenshots must be included - Add a screenshot demonstrating that you ran affected Espresso tests locally & that they're passing --------- Co-authored-by: Ben Henning <[email protected]>
- Loading branch information
1 parent
dd80399
commit 2059f9d
Showing
8 changed files
with
650 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
scripts/src/java/org/oppia/android/scripts/coverage/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Libraries corresponding to developer scripts that obtain coverage data for test targets. | ||
""" | ||
|
||
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library") | ||
|
||
kt_jvm_library( | ||
name = "coverage_runner", | ||
testonly = True, | ||
srcs = [ | ||
"CoverageRunner.kt", | ||
], | ||
visibility = ["//scripts:oppia_script_binary_visibility"], | ||
deps = [ | ||
"//scripts/src/java/org/oppia/android/scripts/common:bazel_client", | ||
], | ||
) |
44 changes: 44 additions & 0 deletions
44
scripts/src/java/org/oppia/android/scripts/coverage/CoverageRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.oppia.android.scripts.coverage | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.async | ||
import org.oppia.android.scripts.common.BazelClient | ||
import org.oppia.android.scripts.common.CommandExecutor | ||
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher | ||
import java.io.File | ||
|
||
/** | ||
* Class responsible for running coverage analysis asynchronously. | ||
* | ||
* @param repoRoot the root directory of the repository | ||
* @param scriptBgDispatcher the [ScriptBackgroundCoroutineDispatcher] to be used for running the coverage command | ||
* @param commandExecutor executes the specified command in the specified working directory | ||
*/ | ||
class CoverageRunner( | ||
private val repoRoot: File, | ||
private val scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher, | ||
private val commandExecutor: CommandExecutor | ||
) { | ||
private val bazelClient by lazy { BazelClient(repoRoot, commandExecutor) } | ||
|
||
/** | ||
* Runs coverage analysis asynchronously for the Bazel test target. | ||
* | ||
* @param bazelTestTarget Bazel test target to analyze coverage | ||
* @return a deferred value that contains the coverage data | ||
*/ | ||
fun runWithCoverageAsync( | ||
bazelTestTarget: String | ||
): Deferred<List<String>?> { | ||
return CoroutineScope(scriptBgDispatcher).async { | ||
retrieveCoverageResult(bazelTestTarget) | ||
} | ||
} | ||
|
||
private fun retrieveCoverageResult( | ||
bazelTestTarget: String | ||
): List<String>? { | ||
return bazelClient.runCoverageForTestTarget(bazelTestTarget) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
scripts/src/javatests/org/oppia/android/scripts/coverage/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Tests corresponding to developer scripts that help with obtaining coverage data for test targets. | ||
""" | ||
|
||
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_test") | ||
|
||
kt_jvm_test( | ||
name = "CoverageRunnerTest", | ||
srcs = ["CoverageRunnerTest.kt"], | ||
deps = [ | ||
"//scripts/src/java/org/oppia/android/scripts/coverage:coverage_runner", | ||
"//scripts/src/java/org/oppia/android/scripts/testing:test_bazel_workspace", | ||
"//testing:assertion_helpers", | ||
"//third_party:com_google_truth_truth", | ||
"//third_party:org_jetbrains_kotlin_kotlin-test-junit", | ||
], | ||
) |
Oops, something went wrong.