blob: 426e6295c8492eb9b1f83ff6d7e0e03c2335fee3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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/.
//! 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();
}
}
|