-
Notifications
You must be signed in to change notification settings - Fork 151
Bug 1690780 - Coverage support #1482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -189,6 +189,7 @@ serializer | |
| setuptools | ||
| stateful | ||
| struct | ||
| subcommand | ||
| subprocess | ||
| subprocesses | ||
| swiftlint | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ These functions expose a way to inspect and validate recorded metric values with | |
| (`@VisibleForTesting(otherwise = VisibleForTesting.NONE)` for Kotlin, `internal` methods for Swift). | ||
| (Outside of a testing context, Glean APIs are otherwise write-only so that it can enforce semantics and constraints about data). | ||
|
|
||
| To encourage using the testing API, it is also possible to [generate testing coverage reports](#generating-testing-coverage-reports) to show which metrics in your project are tested. | ||
|
|
||
| ## General test API method semantics | ||
|
|
||
| {{#include ../../shared/tab_header.md}} | ||
|
|
@@ -266,3 +268,53 @@ TODO. To be implemented in [bug 1648448](https://bugzilla.mozilla.org/show_bug.c | |
| </div> | ||
|
|
||
| {{#include ../../shared/tab_footer.md}} | ||
|
|
||
| ## Generating testing coverage reports | ||
|
|
||
| Glean can generate coverage reports to track which metrics are tested in your unit test suite. | ||
|
|
||
| There are three steps to integrate it into your continuous integration workflow: recording coverage, post-processing the results, and uploading the results. | ||
|
|
||
| ### Recording coverage | ||
|
|
||
| Glean testing coverage is enabled by setting the `GLEAN_TEST_COVERAGE` environment variable to the name of a file to store results. | ||
| It is good practice to set it to the absolute path to a file, since some testing harnesses (such as `cargo test`) may change the current working directory. | ||
|
|
||
| ```bash | ||
| GLEAN_TEST_COVERAGE=$(realpath glean_coverage.txt) make test | ||
| ``` | ||
|
|
||
| ### Post-processing the results | ||
|
|
||
| A post-processing step is required to convert the raw output in the file specified by `GLEAN_TEST_COVERAGE` into usable output for coverage reporting tools. Currently, the only coverage reporting tool supported is [codecov.io](https://codecov.io). | ||
|
|
||
| This post-processor is available in the `coverage` subcommand in the [`glean_parser`](https://github.com/mozilla/glean_parser) tool. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this call out that (right now) we only support the codecov format? |
||
|
|
||
| For some build systems, `glean_parser` is already installed for you by the build system integration at the following locations: | ||
|
|
||
| - On Android/Gradle, `$GRADLE_HOME/glean/bootstrap-4.5.11/Miniconda3/bin/glean_parser` | ||
| - On iOS/Carthage, `$PROJECT_ROOT/.venv/bin/glean_parser` | ||
| - For other systems, install `glean_parser` using `pip install glean_parser` | ||
|
|
||
| The `glean_parser coverage` command requires the following parameters: | ||
|
|
||
| - `-f`: The output format to produce, for example `codecovio` to produce [codecov.io](https://codecov.io)'s custom format. | ||
| - `-o`: The path to the output file, for example `codecov.json`. | ||
| - `-c`: The input raw coverage file. `glean_coverage.txt` in the example above. | ||
| - A list of the `metrics.yaml` files in your repository. | ||
|
|
||
| For example, to produce output for [codecov.io](https://codecov.io): | ||
|
|
||
| ```bash | ||
| glean_parser coverage -f codecovio -o glean_coverage.json -c glean_coverage.txt app/metrics.yaml | ||
| ``` | ||
|
|
||
| In this example, the `glean_coverage.json` file is now ready for uploading to codecov.io. | ||
|
|
||
| ### Uploading coverage | ||
|
|
||
| If using `codecov.io`, the uploader doesn't send coverage results for YAML files by default. Pass the `-X yaml` option to the uploader to make sure they are included: | ||
|
|
||
| ```bash | ||
| bash <(curl -s https://codecov.io/bash) -X yaml | ||
| ``` | ||
This file contains hidden or 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,47 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
badboy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| //! Utilities for recording when testing APIs have been called on specific | ||
| //! metrics. | ||
| //! | ||
| //! Testing coverage is enabled by setting the GLEAN_TEST_COVERAGE environment | ||
| //! variable to the name of an output file. This output file must run through a | ||
| //! post-processor (in glean_parser's `coverage` command) to convert to a format | ||
| //! understood by third-party coverage reporting tools. | ||
| //! | ||
| //! While running a unit test suite, Glean records which database keys were | ||
| //! accessed by the testing APIs, with one entry per line. Database keys are | ||
| //! usually, but not always, the same as metric identifiers, but it is the | ||
| //! responsibility of the post-processor to resolve that difference. | ||
| //! | ||
| //! This functionality has no runtime overhead unless the testing API is used. | ||
|
|
||
| use std::env; | ||
| use std::fs::{File, OpenOptions}; | ||
| use std::io::Write; | ||
| use std::sync::Mutex; | ||
|
|
||
| use once_cell::sync::Lazy; | ||
|
|
||
| static COVERAGE_FILE: Lazy<Option<Mutex<File>>> = Lazy::new(|| { | ||
| if let Some(filename) = env::var_os("GLEAN_TEST_COVERAGE") { | ||
| match OpenOptions::new().append(true).create(true).open(filename) { | ||
| Ok(file) => { | ||
| return Some(Mutex::new(file)); | ||
| } | ||
| Err(err) => { | ||
| log::error!("Couldn't open file for coverage results: {:?}", err); | ||
| } | ||
| } | ||
| } | ||
| None | ||
| }); | ||
|
|
||
| pub(crate) fn record_coverage(metric_id: &str) { | ||
| if let Some(file_mutex) = &*COVERAGE_FILE { | ||
| let mut file = file_mutex.lock().unwrap(); | ||
| writeln!(&mut file, "{}", metric_id).ok(); | ||
| file.flush().ok(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.