2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::CoverageFlags;
|
2023-01-13 16:56:29 -05:00
|
|
|
use crate::args::FileFlags;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::Flags;
|
2020-09-13 09:01:30 -04:00
|
|
|
use crate::colors;
|
2022-11-29 12:43:54 -05:00
|
|
|
use crate::emit::get_source_hash;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2021-12-07 19:21:04 -05:00
|
|
|
use crate::tools::fmt::format_json;
|
2022-12-07 13:10:10 -05:00
|
|
|
use crate::util::fs::FileCollector;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::text_encoding::source_map_from_code;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2021-10-06 13:28:28 -04:00
|
|
|
use deno_ast::MediaType;
|
2021-12-23 20:02:54 -05:00
|
|
|
use deno_ast::ModuleSpecifier;
|
2022-01-10 11:36:58 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
2022-12-09 09:40:48 -05:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2022-04-15 10:08:09 -04:00
|
|
|
use deno_core::sourcemap::SourceMap;
|
2021-10-06 13:28:28 -04:00
|
|
|
use deno_core::url::Url;
|
2021-05-26 15:07:12 -04:00
|
|
|
use deno_core::LocalInspectorSession;
|
2021-02-24 09:27:51 -05:00
|
|
|
use regex::Regex;
|
2020-12-21 08:04:25 -05:00
|
|
|
use std::fs;
|
2021-06-28 21:39:19 -04:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufWriter;
|
2023-01-14 23:18:58 -05:00
|
|
|
use std::io::Error;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::io::{self};
|
2020-12-21 08:04:25 -05:00
|
|
|
use std::path::PathBuf;
|
2021-12-23 20:02:54 -05:00
|
|
|
use text_lines::TextLines;
|
2020-12-21 08:04:25 -05:00
|
|
|
use uuid::Uuid;
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2022-01-11 15:17:25 -05:00
|
|
|
mod json_types;
|
|
|
|
mod merge;
|
|
|
|
mod range_tree;
|
2021-07-08 11:53:56 -04:00
|
|
|
|
2022-01-11 15:17:25 -05:00
|
|
|
use json_types::*;
|
2021-07-08 11:53:56 -04:00
|
|
|
|
2020-09-13 09:01:30 -04:00
|
|
|
pub struct CoverageCollector {
|
2020-12-21 08:04:25 -05:00
|
|
|
pub dir: PathBuf,
|
2021-05-26 11:47:33 -04:00
|
|
|
session: LocalInspectorSession,
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CoverageCollector {
|
2021-05-26 11:47:33 -04:00
|
|
|
pub fn new(dir: PathBuf, session: LocalInspectorSession) -> Self {
|
2020-12-21 08:04:25 -05:00
|
|
|
Self { dir, session }
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
2021-07-08 11:53:56 -04:00
|
|
|
async fn enable_debugger(&mut self) -> Result<(), AnyError> {
|
2022-02-07 11:05:49 -05:00
|
|
|
self
|
|
|
|
.session
|
|
|
|
.post_message::<()>("Debugger.enable", None)
|
|
|
|
.await?;
|
2021-07-08 11:53:56 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn enable_profiler(&mut self) -> Result<(), AnyError> {
|
2022-02-07 11:05:49 -05:00
|
|
|
self
|
|
|
|
.session
|
|
|
|
.post_message::<()>("Profiler.enable", None)
|
|
|
|
.await?;
|
2021-07-08 11:53:56 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn disable_debugger(&mut self) -> Result<(), AnyError> {
|
2022-02-07 11:05:49 -05:00
|
|
|
self
|
|
|
|
.session
|
|
|
|
.post_message::<()>("Debugger.disable", None)
|
|
|
|
.await?;
|
2021-07-08 11:53:56 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn disable_profiler(&mut self) -> Result<(), AnyError> {
|
2022-02-07 11:05:49 -05:00
|
|
|
self
|
|
|
|
.session
|
|
|
|
.post_message::<()>("Profiler.disable", None)
|
|
|
|
.await?;
|
2021-07-08 11:53:56 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn start_precise_coverage(
|
|
|
|
&mut self,
|
|
|
|
parameters: StartPreciseCoverageParameters,
|
|
|
|
) -> Result<StartPreciseCoverageReturnObject, AnyError> {
|
|
|
|
let return_value = self
|
2020-09-29 17:05:06 -04:00
|
|
|
.session
|
2022-02-07 11:05:49 -05:00
|
|
|
.post_message("Profiler.startPreciseCoverage", Some(parameters))
|
2020-09-19 08:01:56 -04:00
|
|
|
.await?;
|
2021-07-08 11:53:56 -04:00
|
|
|
|
|
|
|
let return_object = serde_json::from_value(return_value)?;
|
|
|
|
|
|
|
|
Ok(return_object)
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
2021-07-08 11:53:56 -04:00
|
|
|
async fn take_precise_coverage(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<TakePreciseCoverageReturnObject, AnyError> {
|
|
|
|
let return_value = self
|
2020-09-29 17:05:06 -04:00
|
|
|
.session
|
2022-02-07 11:05:49 -05:00
|
|
|
.post_message::<()>("Profiler.takePreciseCoverage", None)
|
2020-09-13 09:01:30 -04:00
|
|
|
.await?;
|
2020-09-21 23:59:02 -04:00
|
|
|
|
2021-07-08 11:53:56 -04:00
|
|
|
let return_object = serde_json::from_value(return_value)?;
|
|
|
|
|
|
|
|
Ok(return_object)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn start_collecting(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.enable_debugger().await?;
|
|
|
|
self.enable_profiler().await?;
|
|
|
|
self
|
|
|
|
.start_precise_coverage(StartPreciseCoverageParameters {
|
|
|
|
call_count: true,
|
|
|
|
detailed: true,
|
|
|
|
allow_triggered_updates: false,
|
|
|
|
})
|
|
|
|
.await?;
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-07-08 11:53:56 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn stop_collecting(&mut self) -> Result<(), AnyError> {
|
2020-12-21 08:04:25 -05:00
|
|
|
fs::create_dir_all(&self.dir)?;
|
|
|
|
|
2021-07-08 11:53:56 -04:00
|
|
|
let script_coverages = self.take_precise_coverage().await?.result;
|
2020-12-21 08:04:25 -05:00
|
|
|
for script_coverage in script_coverages {
|
|
|
|
let filename = format!("{}.json", Uuid::new_v4());
|
2021-06-28 21:39:19 -04:00
|
|
|
let filepath = self.dir.join(filename);
|
|
|
|
|
|
|
|
let mut out = BufWriter::new(File::create(filepath)?);
|
2021-12-07 19:21:04 -05:00
|
|
|
let coverage = serde_json::to_string(&script_coverage)?;
|
2022-03-29 13:33:00 -04:00
|
|
|
let formated_coverage = format_json(&coverage, &Default::default())
|
|
|
|
.ok()
|
|
|
|
.flatten()
|
|
|
|
.unwrap_or(coverage);
|
2021-12-07 19:21:04 -05:00
|
|
|
|
|
|
|
out.write_all(formated_coverage.as_bytes())?;
|
2021-06-28 21:39:19 -04:00
|
|
|
out.flush()?;
|
2020-09-21 23:59:02 -04:00
|
|
|
}
|
|
|
|
|
2021-07-08 11:53:56 -04:00
|
|
|
self.disable_debugger().await?;
|
|
|
|
self.disable_profiler().await?;
|
2020-09-13 09:01:30 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
struct BranchCoverageItem {
|
|
|
|
line_index: usize,
|
|
|
|
block_number: usize,
|
|
|
|
branch_number: usize,
|
2022-01-11 15:17:25 -05:00
|
|
|
taken: Option<i64>,
|
2021-12-23 20:02:54 -05:00
|
|
|
is_hit: bool,
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
struct FunctionCoverageItem {
|
|
|
|
name: String,
|
|
|
|
line_index: usize,
|
2022-01-11 15:17:25 -05:00
|
|
|
execution_count: i64,
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
struct CoverageReport {
|
|
|
|
url: ModuleSpecifier,
|
|
|
|
named_functions: Vec<FunctionCoverageItem>,
|
|
|
|
branches: Vec<BranchCoverageItem>,
|
2022-01-11 15:17:25 -05:00
|
|
|
found_lines: Vec<(usize, i64)>,
|
2022-02-15 10:33:21 -05:00
|
|
|
output: Option<PathBuf>,
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
fn generate_coverage_report(
|
|
|
|
script_coverage: &ScriptCoverage,
|
|
|
|
script_source: &str,
|
|
|
|
maybe_source_map: &Option<Vec<u8>>,
|
2022-02-15 10:33:21 -05:00
|
|
|
output: &Option<PathBuf>,
|
2021-12-23 20:02:54 -05:00
|
|
|
) -> CoverageReport {
|
|
|
|
let maybe_source_map = maybe_source_map
|
|
|
|
.as_ref()
|
|
|
|
.map(|source_map| SourceMap::from_slice(source_map).unwrap());
|
|
|
|
let text_lines = TextLines::new(script_source);
|
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
let comment_ranges = deno_ast::lex(script_source, MediaType::JavaScript)
|
2021-12-23 20:02:54 -05:00
|
|
|
.into_iter()
|
|
|
|
.filter(|item| {
|
|
|
|
matches!(item.inner, deno_ast::TokenOrComment::Comment { .. })
|
|
|
|
})
|
2022-05-20 16:40:55 -04:00
|
|
|
.map(|item| item.range)
|
2021-12-23 20:02:54 -05:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let url = Url::parse(&script_coverage.url).unwrap();
|
|
|
|
let mut coverage_report = CoverageReport {
|
|
|
|
url,
|
|
|
|
named_functions: Vec::with_capacity(
|
|
|
|
script_coverage
|
|
|
|
.functions
|
|
|
|
.iter()
|
|
|
|
.filter(|f| !f.function_name.is_empty())
|
|
|
|
.count(),
|
|
|
|
),
|
|
|
|
branches: Vec::new(),
|
|
|
|
found_lines: Vec::new(),
|
2022-02-15 10:33:21 -05:00
|
|
|
output: output.clone(),
|
2021-12-23 20:02:54 -05:00
|
|
|
};
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
for function in &script_coverage.functions {
|
|
|
|
if function.function_name.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2022-07-11 19:02:11 -04:00
|
|
|
let dest_line_index = text_lines.line_index(
|
|
|
|
text_lines
|
|
|
|
.byte_index_from_char_index(function.ranges[0].start_char_offset),
|
|
|
|
);
|
2021-12-23 20:02:54 -05:00
|
|
|
let line_index = if let Some(source_map) = maybe_source_map.as_ref() {
|
|
|
|
source_map
|
|
|
|
.tokens()
|
2022-07-11 19:02:11 -04:00
|
|
|
.find(|token| token.get_dst_line() as usize == dest_line_index)
|
2021-12-23 20:02:54 -05:00
|
|
|
.map(|token| token.get_src_line() as usize)
|
|
|
|
.unwrap_or(0)
|
|
|
|
} else {
|
2022-07-11 19:02:11 -04:00
|
|
|
dest_line_index
|
2021-12-23 20:02:54 -05:00
|
|
|
};
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
coverage_report.named_functions.push(FunctionCoverageItem {
|
|
|
|
name: function.function_name.clone(),
|
|
|
|
line_index,
|
|
|
|
execution_count: function.ranges[0].count,
|
|
|
|
});
|
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
for (block_number, function) in script_coverage.functions.iter().enumerate() {
|
|
|
|
let block_hits = function.ranges[0].count;
|
|
|
|
for (branch_number, range) in function.ranges[1..].iter().enumerate() {
|
2022-07-11 19:02:11 -04:00
|
|
|
let source_line_index = text_lines.line_index(
|
|
|
|
text_lines.byte_index_from_char_index(range.start_char_offset),
|
|
|
|
);
|
2021-10-06 13:28:28 -04:00
|
|
|
let line_index = if let Some(source_map) = maybe_source_map.as_ref() {
|
|
|
|
source_map
|
|
|
|
.tokens()
|
2021-12-23 20:02:54 -05:00
|
|
|
.find(|token| token.get_dst_line() as usize == source_line_index)
|
2021-10-06 13:28:28 -04:00
|
|
|
.map(|token| token.get_src_line() as usize)
|
|
|
|
.unwrap_or(0)
|
|
|
|
} else {
|
2021-12-23 20:02:54 -05:00
|
|
|
source_line_index
|
2021-10-06 13:28:28 -04:00
|
|
|
};
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
// From https://manpages.debian.org/unstable/lcov/geninfo.1.en.html:
|
|
|
|
//
|
|
|
|
// Block number and branch number are gcc internal IDs for the branch. Taken is either '-'
|
|
|
|
// if the basic block containing the branch was never executed or a number indicating how
|
|
|
|
// often that branch was taken.
|
|
|
|
//
|
|
|
|
// However with the data we get from v8 coverage profiles it seems we can't actually hit
|
|
|
|
// this as appears it won't consider any nested branches it hasn't seen but its here for
|
|
|
|
// the sake of accuracy.
|
|
|
|
let taken = if block_hits > 0 {
|
|
|
|
Some(range.count)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
coverage_report.branches.push(BranchCoverageItem {
|
|
|
|
line_index,
|
|
|
|
block_number,
|
|
|
|
branch_number,
|
|
|
|
taken,
|
|
|
|
is_hit: range.count > 0,
|
|
|
|
})
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
2021-12-23 20:02:54 -05:00
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
// TODO(caspervonb): collect uncovered ranges on the lines so that we can highlight specific
|
|
|
|
// parts of a line in color (word diff style) instead of the entire line.
|
|
|
|
let mut line_counts = Vec::with_capacity(text_lines.lines_count());
|
|
|
|
for line_index in 0..text_lines.lines_count() {
|
2022-07-11 19:02:11 -04:00
|
|
|
let line_start_byte_offset = text_lines.line_start(line_index);
|
|
|
|
let line_start_char_offset = text_lines.char_index(line_start_byte_offset);
|
|
|
|
let line_end_byte_offset = text_lines.line_end(line_index);
|
|
|
|
let line_end_char_offset = text_lines.char_index(line_end_byte_offset);
|
2022-05-20 16:40:55 -04:00
|
|
|
let ignore = comment_ranges.iter().any(|range| {
|
2022-07-11 19:02:11 -04:00
|
|
|
range.start <= line_start_byte_offset && range.end >= line_end_byte_offset
|
|
|
|
}) || script_source
|
|
|
|
[line_start_byte_offset..line_end_byte_offset]
|
2021-12-23 20:02:54 -05:00
|
|
|
.trim()
|
|
|
|
.is_empty();
|
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
if ignore {
|
|
|
|
count = 1;
|
|
|
|
} else {
|
|
|
|
// Count the hits of ranges that include the entire line which will always be at-least one
|
|
|
|
// as long as the code has been evaluated.
|
|
|
|
for function in &script_coverage.functions {
|
|
|
|
for range in &function.ranges {
|
2022-07-11 19:02:11 -04:00
|
|
|
if range.start_char_offset <= line_start_char_offset
|
|
|
|
&& range.end_char_offset >= line_end_char_offset
|
2021-12-23 20:02:54 -05:00
|
|
|
{
|
|
|
|
count += range.count;
|
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
// We reset the count if any block with a zero count overlaps with the line range.
|
|
|
|
for function in &script_coverage.functions {
|
|
|
|
for range in &function.ranges {
|
|
|
|
if range.count > 0 {
|
|
|
|
continue;
|
2021-10-06 13:28:28 -04:00
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2022-07-11 19:02:11 -04:00
|
|
|
let overlaps = range.start_char_offset < line_end_char_offset
|
|
|
|
&& range.end_char_offset > line_start_char_offset;
|
2021-12-23 20:02:54 -05:00
|
|
|
if overlaps {
|
|
|
|
count = 0;
|
2021-10-06 13:28:28 -04:00
|
|
|
}
|
|
|
|
}
|
2021-12-23 20:02:54 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
line_counts.push(count);
|
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
coverage_report.found_lines =
|
|
|
|
if let Some(source_map) = maybe_source_map.as_ref() {
|
2021-10-06 13:28:28 -04:00
|
|
|
let mut found_lines = line_counts
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2022-02-24 20:03:12 -05:00
|
|
|
.flat_map(|(index, count)| {
|
2021-12-23 20:02:54 -05:00
|
|
|
// get all the mappings from this destination line to a different src line
|
|
|
|
let mut results = source_map
|
2021-10-06 13:28:28 -04:00
|
|
|
.tokens()
|
|
|
|
.filter(move |token| token.get_dst_line() as usize == index)
|
|
|
|
.map(move |token| (token.get_src_line() as usize, *count))
|
2021-12-23 20:02:54 -05:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
// only keep the results that point at different src lines
|
|
|
|
results.sort_unstable_by_key(|(index, _)| *index);
|
|
|
|
results.dedup_by_key(|(index, _)| *index);
|
|
|
|
results.into_iter()
|
2021-10-06 13:28:28 -04:00
|
|
|
})
|
2022-01-11 15:17:25 -05:00
|
|
|
.collect::<Vec<(usize, i64)>>();
|
2021-10-06 13:28:28 -04:00
|
|
|
|
|
|
|
found_lines.sort_unstable_by_key(|(index, _)| *index);
|
2021-12-23 20:02:54 -05:00
|
|
|
// combine duplicated lines
|
|
|
|
for i in (1..found_lines.len()).rev() {
|
|
|
|
if found_lines[i].0 == found_lines[i - 1].0 {
|
|
|
|
found_lines[i - 1].1 += found_lines[i].1;
|
|
|
|
found_lines.remove(i);
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
found_lines
|
|
|
|
} else {
|
|
|
|
line_counts
|
2021-12-23 20:02:54 -05:00
|
|
|
.into_iter()
|
2021-10-06 13:28:28 -04:00
|
|
|
.enumerate()
|
2021-12-23 20:02:54 -05:00
|
|
|
.map(|(index, count)| (index, count))
|
2022-01-11 15:17:25 -05:00
|
|
|
.collect::<Vec<(usize, i64)>>()
|
2021-10-06 13:28:28 -04:00
|
|
|
};
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
coverage_report
|
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
enum CoverageReporterKind {
|
|
|
|
Pretty,
|
|
|
|
Lcov,
|
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
fn create_reporter(
|
|
|
|
kind: CoverageReporterKind,
|
|
|
|
) -> Box<dyn CoverageReporter + Send> {
|
|
|
|
match kind {
|
|
|
|
CoverageReporterKind::Lcov => Box::new(LcovCoverageReporter::new()),
|
|
|
|
CoverageReporterKind::Pretty => Box::new(PrettyCoverageReporter::new()),
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
2021-12-23 20:02:54 -05:00
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
trait CoverageReporter {
|
2022-02-15 10:33:21 -05:00
|
|
|
fn report(
|
|
|
|
&mut self,
|
|
|
|
coverage_report: &CoverageReport,
|
|
|
|
file_text: &str,
|
|
|
|
) -> Result<(), AnyError>;
|
2021-12-23 20:02:54 -05:00
|
|
|
|
|
|
|
fn done(&mut self);
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
struct LcovCoverageReporter {}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
impl LcovCoverageReporter {
|
|
|
|
pub fn new() -> LcovCoverageReporter {
|
|
|
|
LcovCoverageReporter {}
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
impl CoverageReporter for LcovCoverageReporter {
|
2022-02-15 10:33:21 -05:00
|
|
|
fn report(
|
|
|
|
&mut self,
|
|
|
|
coverage_report: &CoverageReport,
|
|
|
|
_file_text: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
// pipes output to stdout if no file is specified
|
|
|
|
let out_mode: Result<Box<dyn Write>, Error> = match coverage_report.output {
|
|
|
|
// only append to the file as the file should be created already
|
|
|
|
Some(ref path) => File::options()
|
|
|
|
.append(true)
|
|
|
|
.open(path)
|
|
|
|
.map(|f| Box::new(f) as Box<dyn Write>),
|
|
|
|
None => Ok(Box::new(io::stdout())),
|
|
|
|
};
|
|
|
|
let mut out_writer = out_mode?;
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let file_path = coverage_report
|
|
|
|
.url
|
|
|
|
.to_file_path()
|
|
|
|
.ok()
|
2022-02-24 20:03:12 -05:00
|
|
|
.and_then(|p| p.to_str().map(|p| p.to_string()))
|
2021-12-23 20:02:54 -05:00
|
|
|
.unwrap_or_else(|| coverage_report.url.to_string());
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(out_writer, "SF:{file_path}")?;
|
2021-12-23 20:02:54 -05:00
|
|
|
|
|
|
|
for function in &coverage_report.named_functions {
|
2022-02-15 10:33:21 -05:00
|
|
|
writeln!(
|
|
|
|
out_writer,
|
|
|
|
"FN:{},{}",
|
|
|
|
function.line_index + 1,
|
|
|
|
function.name
|
|
|
|
)?;
|
2021-10-06 13:28:28 -04:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
for function in &coverage_report.named_functions {
|
2022-02-15 10:33:21 -05:00
|
|
|
writeln!(
|
|
|
|
out_writer,
|
|
|
|
"FNDA:{},{}",
|
|
|
|
function.execution_count, function.name
|
|
|
|
)?;
|
2021-12-23 20:02:54 -05:00
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let functions_found = coverage_report.named_functions.len();
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(out_writer, "FNF:{functions_found}")?;
|
2021-12-23 20:02:54 -05:00
|
|
|
let functions_hit = coverage_report
|
|
|
|
.named_functions
|
2021-01-29 14:45:22 -05:00
|
|
|
.iter()
|
2021-12-23 20:02:54 -05:00
|
|
|
.filter(|f| f.execution_count > 0)
|
|
|
|
.count();
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(out_writer, "FNH:{functions_hit}")?;
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
for branch in &coverage_report.branches {
|
|
|
|
let taken = if let Some(taken) = &branch.taken {
|
|
|
|
taken.to_string()
|
|
|
|
} else {
|
|
|
|
"-".to_string()
|
|
|
|
};
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2022-02-15 10:33:21 -05:00
|
|
|
writeln!(
|
|
|
|
out_writer,
|
2021-12-23 20:02:54 -05:00
|
|
|
"BRDA:{},{},{},{}",
|
|
|
|
branch.line_index + 1,
|
|
|
|
branch.block_number,
|
|
|
|
branch.branch_number,
|
|
|
|
taken
|
2022-02-15 10:33:21 -05:00
|
|
|
)?;
|
2021-12-23 20:02:54 -05:00
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let branches_found = coverage_report.branches.len();
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(out_writer, "BRF:{branches_found}")?;
|
2021-12-23 20:02:54 -05:00
|
|
|
let branches_hit =
|
|
|
|
coverage_report.branches.iter().filter(|b| b.is_hit).count();
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(out_writer, "BRH:{branches_hit}")?;
|
2021-12-23 20:02:54 -05:00
|
|
|
for (index, count) in &coverage_report.found_lines {
|
2022-02-15 10:33:21 -05:00
|
|
|
writeln!(out_writer, "DA:{},{}", index + 1, count)?;
|
2021-12-23 20:02:54 -05:00
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let lines_hit = coverage_report
|
|
|
|
.found_lines
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, count)| *count != 0)
|
|
|
|
.count();
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(out_writer, "LH:{lines_hit}")?;
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let lines_found = coverage_report.found_lines.len();
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(out_writer, "LF:{lines_found}")?;
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2022-02-15 10:33:21 -05:00
|
|
|
writeln!(out_writer, "end_of_record")?;
|
|
|
|
Ok(())
|
2021-12-23 20:02:54 -05:00
|
|
|
}
|
2021-01-29 14:45:22 -05:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
fn done(&mut self) {}
|
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
struct PrettyCoverageReporter {}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
impl PrettyCoverageReporter {
|
|
|
|
pub fn new() -> PrettyCoverageReporter {
|
|
|
|
PrettyCoverageReporter {}
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
impl CoverageReporter for PrettyCoverageReporter {
|
2022-02-15 10:33:21 -05:00
|
|
|
fn report(
|
|
|
|
&mut self,
|
|
|
|
coverage_report: &CoverageReport,
|
|
|
|
file_text: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-12-23 20:02:54 -05:00
|
|
|
let lines = file_text.split('\n').collect::<Vec<_>>();
|
|
|
|
print!("cover {} ... ", coverage_report.url);
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let hit_lines = coverage_report
|
|
|
|
.found_lines
|
2021-02-24 09:27:51 -05:00
|
|
|
.iter()
|
2021-12-23 20:02:54 -05:00
|
|
|
.filter(|(_, count)| *count > 0)
|
2021-10-06 13:28:28 -04:00
|
|
|
.map(|(index, _)| *index);
|
2020-09-23 14:12:24 -04:00
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let missed_lines = coverage_report
|
|
|
|
.found_lines
|
2021-02-24 09:27:51 -05:00
|
|
|
.iter()
|
2021-10-06 13:28:28 -04:00
|
|
|
.filter(|(_, count)| *count == 0)
|
|
|
|
.map(|(index, _)| *index);
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let lines_found = coverage_report.found_lines.len();
|
2021-10-06 13:28:28 -04:00
|
|
|
let lines_hit = hit_lines.count();
|
|
|
|
let line_ratio = lines_hit as f32 / lines_found as f32;
|
|
|
|
|
|
|
|
let line_coverage =
|
2021-12-23 20:02:54 -05:00
|
|
|
format!("{:.3}% ({}/{})", line_ratio * 100.0, lines_hit, lines_found);
|
2021-01-29 14:45:22 -05:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
if line_ratio >= 0.9 {
|
|
|
|
println!("{}", colors::green(&line_coverage));
|
|
|
|
} else if line_ratio >= 0.75 {
|
|
|
|
println!("{}", colors::yellow(&line_coverage));
|
|
|
|
} else {
|
|
|
|
println!("{}", colors::red(&line_coverage));
|
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-10-06 13:28:28 -04:00
|
|
|
let mut last_line = None;
|
|
|
|
for line_index in missed_lines {
|
|
|
|
const WIDTH: usize = 4;
|
|
|
|
const SEPERATOR: &str = "|";
|
|
|
|
|
|
|
|
// Put a horizontal separator between disjoint runs of lines
|
|
|
|
if let Some(last_line) = last_line {
|
|
|
|
if last_line + 1 != line_index {
|
|
|
|
let dash = colors::gray("-".repeat(WIDTH + 1));
|
|
|
|
println!("{}{}{}", dash, colors::gray(SEPERATOR), dash);
|
2020-11-24 16:26:38 -05:00
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
2020-11-24 16:26:38 -05:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
println!(
|
|
|
|
"{:width$} {} {}",
|
2021-10-06 13:28:28 -04:00
|
|
|
line_index + 1,
|
|
|
|
colors::gray(SEPERATOR),
|
|
|
|
colors::red(&lines[line_index]),
|
|
|
|
width = WIDTH
|
2021-02-24 09:27:51 -05:00
|
|
|
);
|
2020-11-24 16:26:38 -05:00
|
|
|
|
2021-10-06 13:28:28 -04:00
|
|
|
last_line = Some(line_index);
|
2020-09-23 14:12:24 -04:00
|
|
|
}
|
2022-02-15 10:33:21 -05:00
|
|
|
Ok(())
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
2021-10-06 13:28:28 -04:00
|
|
|
|
|
|
|
fn done(&mut self) {}
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
2021-10-06 13:28:28 -04:00
|
|
|
fn collect_coverages(
|
2023-01-13 16:56:29 -05:00
|
|
|
files: FileFlags,
|
2021-02-24 09:27:51 -05:00
|
|
|
) -> Result<Vec<ScriptCoverage>, AnyError> {
|
2021-01-07 05:53:57 -05:00
|
|
|
let mut coverages: Vec<ScriptCoverage> = Vec::new();
|
2022-12-07 13:10:10 -05:00
|
|
|
let file_paths = FileCollector::new(|file_path| {
|
2021-02-24 09:27:51 -05:00
|
|
|
file_path.extension().map_or(false, |ext| ext == "json")
|
2022-12-07 13:10:10 -05:00
|
|
|
})
|
|
|
|
.ignore_git_folder()
|
|
|
|
.ignore_node_modules()
|
2023-01-13 16:56:29 -05:00
|
|
|
.add_ignore_paths(&files.ignore)
|
|
|
|
.collect_files(&files.include)?;
|
2020-12-21 08:04:25 -05:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
for file_path in file_paths {
|
|
|
|
let json = fs::read_to_string(file_path.as_path())?;
|
2021-01-07 05:53:57 -05:00
|
|
|
let new_coverage: ScriptCoverage = serde_json::from_str(&json)?;
|
2022-01-11 15:17:25 -05:00
|
|
|
coverages.push(new_coverage);
|
2020-12-21 08:04:25 -05:00
|
|
|
}
|
|
|
|
|
2021-01-07 05:53:57 -05:00
|
|
|
coverages.sort_by_key(|k| k.url.clone());
|
2021-01-04 11:01:21 -05:00
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
Ok(coverages)
|
|
|
|
}
|
|
|
|
|
2021-10-06 13:28:28 -04:00
|
|
|
fn filter_coverages(
|
2021-01-07 05:53:57 -05:00
|
|
|
coverages: Vec<ScriptCoverage>,
|
2021-02-24 09:27:51 -05:00
|
|
|
include: Vec<String>,
|
|
|
|
exclude: Vec<String>,
|
2021-01-07 05:53:57 -05:00
|
|
|
) -> Vec<ScriptCoverage> {
|
2021-02-24 09:27:51 -05:00
|
|
|
let include: Vec<Regex> =
|
|
|
|
include.iter().map(|e| Regex::new(e).unwrap()).collect();
|
|
|
|
|
|
|
|
let exclude: Vec<Regex> =
|
|
|
|
exclude.iter().map(|e| Regex::new(e).unwrap()).collect();
|
|
|
|
|
2020-09-13 09:01:30 -04:00
|
|
|
coverages
|
|
|
|
.into_iter()
|
|
|
|
.filter(|e| {
|
2021-02-24 09:27:51 -05:00
|
|
|
let is_internal = e.url.starts_with("deno:")
|
|
|
|
|| e.url.ends_with("__anonymous__")
|
2022-05-04 07:10:55 -04:00
|
|
|
|| e.url.ends_with("$deno$test.js")
|
|
|
|
|| e.url.ends_with(".snap");
|
2020-09-21 23:59:02 -04:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
let is_included = include.iter().any(|p| p.is_match(&e.url));
|
|
|
|
let is_excluded = exclude.iter().any(|p| p.is_match(&e.url));
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
(include.is_empty() || is_included) && !is_excluded && !is_internal
|
2020-09-13 09:01:30 -04:00
|
|
|
})
|
2021-01-07 05:53:57 -05:00
|
|
|
.collect::<Vec<ScriptCoverage>>()
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
2020-12-21 08:04:25 -05:00
|
|
|
|
2021-10-06 13:28:28 -04:00
|
|
|
pub async fn cover_files(
|
2021-02-24 09:27:51 -05:00
|
|
|
flags: Flags,
|
2021-12-20 16:29:02 -05:00
|
|
|
coverage_flags: CoverageFlags,
|
2020-12-21 08:04:25 -05:00
|
|
|
) -> Result<(), AnyError> {
|
2023-01-13 16:56:29 -05:00
|
|
|
if coverage_flags.files.include.is_empty() {
|
2022-12-09 09:40:48 -05:00
|
|
|
return Err(generic_error("No matching coverage profiles found"));
|
|
|
|
}
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
let ps = ProcState::build(flags).await?;
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2023-01-13 16:56:29 -05:00
|
|
|
let script_coverages = collect_coverages(coverage_flags.files)?;
|
2021-12-20 16:29:02 -05:00
|
|
|
let script_coverages = filter_coverages(
|
|
|
|
script_coverages,
|
|
|
|
coverage_flags.include,
|
|
|
|
coverage_flags.exclude,
|
|
|
|
);
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2022-01-11 15:17:25 -05:00
|
|
|
let proc_coverages: Vec<_> = script_coverages
|
|
|
|
.into_iter()
|
|
|
|
.map(|cov| ProcessCoverage { result: vec![cov] })
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let script_coverages = if let Some(c) = merge::merge_processes(proc_coverages)
|
|
|
|
{
|
|
|
|
c.result
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
2021-12-20 16:29:02 -05:00
|
|
|
let reporter_kind = if coverage_flags.lcov {
|
2021-02-24 09:27:51 -05:00
|
|
|
CoverageReporterKind::Lcov
|
|
|
|
} else {
|
|
|
|
CoverageReporterKind::Pretty
|
|
|
|
};
|
2020-12-21 08:04:25 -05:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
let mut reporter = create_reporter(reporter_kind);
|
|
|
|
|
2022-02-15 10:33:21 -05:00
|
|
|
let out_mode = match coverage_flags.output {
|
|
|
|
Some(ref path) => match File::create(path) {
|
|
|
|
Ok(_) => Some(PathBuf::from(path)),
|
|
|
|
Err(e) => {
|
|
|
|
return Err(anyhow!("Failed to create output file: {}", e));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
for script_coverage in script_coverages {
|
2021-10-06 13:28:28 -04:00
|
|
|
let module_specifier =
|
|
|
|
deno_core::resolve_url_or_path(&script_coverage.url)?;
|
2020-12-21 08:04:25 -05:00
|
|
|
|
2022-01-10 11:36:58 -05:00
|
|
|
let maybe_file = if module_specifier.scheme() == "file" {
|
|
|
|
ps.file_fetcher.get_source(&module_specifier)
|
|
|
|
} else {
|
|
|
|
ps.file_fetcher
|
|
|
|
.fetch_cached(&module_specifier, 10)
|
|
|
|
.with_context(|| {
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("Failed to fetch \"{module_specifier}\" from cache.")
|
2022-01-10 11:36:58 -05:00
|
|
|
})?
|
|
|
|
};
|
|
|
|
let file = maybe_file.ok_or_else(|| {
|
2022-02-07 11:05:49 -05:00
|
|
|
anyhow!("Failed to fetch \"{}\" from cache.
|
|
|
|
Before generating coverage report, run `deno test --coverage` to ensure consistent state.",
|
2022-01-10 11:36:58 -05:00
|
|
|
module_specifier
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// Check if file was transpiled
|
2022-07-19 11:58:18 -04:00
|
|
|
let original_source = &file.source;
|
|
|
|
let transpiled_code = match file.media_type {
|
2022-01-10 11:36:58 -05:00
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Unknown
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
2022-05-20 16:40:55 -04:00
|
|
|
| MediaType::Json => file.source.as_ref().to_string(),
|
2022-01-10 11:36:58 -05:00
|
|
|
MediaType::Dts | MediaType::Dmts | MediaType::Dcts => "".to_string(),
|
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Tsx => {
|
2022-11-29 12:43:54 -05:00
|
|
|
let source_hash = get_source_hash(&file.source, ps.emit_options_hash);
|
|
|
|
match ps.emit_cache.get_emit_code(&file.specifier, source_hash) {
|
2022-07-19 11:58:18 -04:00
|
|
|
Some(code) => code,
|
2022-07-12 18:58:39 -04:00
|
|
|
None => {
|
2022-01-10 11:36:58 -05:00
|
|
|
return Err(anyhow!(
|
|
|
|
"Missing transpiled source code for: \"{}\".
|
|
|
|
Before generating coverage report, run `deno test --coverage` to ensure consistent state.",
|
|
|
|
file.specifier,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MediaType::Wasm | MediaType::TsBuildInfo | MediaType::SourceMap => {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-23 20:02:54 -05:00
|
|
|
let coverage_report = generate_coverage_report(
|
2021-10-06 13:28:28 -04:00
|
|
|
&script_coverage,
|
2022-07-19 11:58:18 -04:00
|
|
|
&transpiled_code,
|
|
|
|
&source_map_from_code(&transpiled_code),
|
2022-02-15 10:33:21 -05:00
|
|
|
&out_mode,
|
2021-10-06 13:28:28 -04:00
|
|
|
);
|
2021-12-23 20:02:54 -05:00
|
|
|
|
2022-05-22 10:45:22 -04:00
|
|
|
if !coverage_report.found_lines.is_empty() {
|
|
|
|
reporter.report(&coverage_report, original_source)?;
|
|
|
|
}
|
2021-10-06 01:05:18 -04:00
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 13:28:28 -04:00
|
|
|
reporter.done();
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
Ok(())
|
|
|
|
}
|