mirror of
https://github.com/denoland/deno.git
synced 2025-08-02 10:02:23 +00:00
fix(coverage): merge duplicate reports (#8942)
Merging multiple runs isn't quite right because we rely on a 0 count to signal that a block hasn't been called. Other tools like c8 expect this to be true as-well so we need to do our best to merge coverage files rather than duplicating them.
This commit is contained in:
parent
3e5a3daf59
commit
4ca77ad84c
5 changed files with 85 additions and 27 deletions
|
@ -93,7 +93,7 @@ impl CoverageCollector {
|
|||
|
||||
// TODO(caspervonb) all of these structs can and should be made private, possibly moved to
|
||||
// inspector::protocol.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CoverageRange {
|
||||
pub start_offset: usize,
|
||||
|
@ -101,7 +101,7 @@ pub struct CoverageRange {
|
|||
pub count: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FunctionCoverage {
|
||||
pub function_name: String,
|
||||
|
@ -109,7 +109,7 @@ pub struct FunctionCoverage {
|
|||
pub is_block_coverage: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ScriptCoverage {
|
||||
pub script_id: String,
|
||||
|
@ -117,7 +117,7 @@ pub struct ScriptCoverage {
|
|||
pub functions: Vec<FunctionCoverage>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Coverage {
|
||||
pub script_coverage: ScriptCoverage,
|
||||
|
@ -237,29 +237,48 @@ fn collect_coverages(dir: &PathBuf) -> Result<Vec<Coverage>, AnyError> {
|
|||
let entries = fs::read_dir(dir)?;
|
||||
for entry in entries {
|
||||
let json = fs::read_to_string(entry.unwrap().path())?;
|
||||
let coverage: Coverage = serde_json::from_str(&json)?;
|
||||
let new_coverage: Coverage = serde_json::from_str(&json)?;
|
||||
|
||||
coverages.push(coverage);
|
||||
}
|
||||
let existing_coverage = coverages
|
||||
.iter_mut()
|
||||
.find(|x| x.script_coverage.url == new_coverage.script_coverage.url);
|
||||
|
||||
// TODO(caspervonb) drain_filter would make this cleaner, its nightly at the moment.
|
||||
if coverages.len() > 1 {
|
||||
coverages.sort_by_key(|k| k.script_coverage.url.clone());
|
||||
if let Some(existing_coverage) = existing_coverage {
|
||||
for new_function in new_coverage.script_coverage.functions {
|
||||
let existing_function = existing_coverage
|
||||
.script_coverage
|
||||
.functions
|
||||
.iter_mut()
|
||||
.find(|x| x.function_name == new_function.function_name);
|
||||
|
||||
for i in (1..coverages.len() - 1).rev() {
|
||||
if coverages[i].script_coverage.url
|
||||
== coverages[i - 1].script_coverage.url
|
||||
{
|
||||
let current = coverages.remove(i);
|
||||
let previous = &mut coverages[i - 1];
|
||||
if let Some(existing_function) = existing_function {
|
||||
for new_range in new_function.ranges {
|
||||
let existing_range =
|
||||
existing_function.ranges.iter_mut().find(|x| {
|
||||
x.start_offset == new_range.start_offset
|
||||
&& x.end_offset == new_range.end_offset
|
||||
});
|
||||
|
||||
for function in current.script_coverage.functions {
|
||||
previous.script_coverage.functions.push(function);
|
||||
if let Some(existing_range) = existing_range {
|
||||
existing_range.count += new_range.count;
|
||||
} else {
|
||||
existing_function.ranges.push(new_range);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
existing_coverage
|
||||
.script_coverage
|
||||
.functions
|
||||
.push(new_function);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
coverages.push(new_coverage);
|
||||
}
|
||||
}
|
||||
|
||||
coverages.sort_by_key(|k| k.script_coverage.url.clone());
|
||||
|
||||
Ok(coverages)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue