2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-01-20 09:51:36 -05:00
|
|
|
use crate::ast;
|
|
|
|
use crate::ast::TokenOrComment;
|
2020-09-13 09:01:30 -04:00
|
|
|
use crate::colors;
|
2021-02-24 09:27:51 -05:00
|
|
|
use crate::flags::Flags;
|
|
|
|
use crate::fs_util::collect_files;
|
2021-01-20 09:51:36 -05:00
|
|
|
use crate::media_type::MediaType;
|
2021-01-07 05:53:57 -05:00
|
|
|
use crate::module_graph::TypeLib;
|
|
|
|
use crate::program_state::ProgramState;
|
2021-01-28 09:11:38 -05:00
|
|
|
use crate::source_maps::SourceMapGetter;
|
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;
|
|
|
|
use deno_core::serde_json::json;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2021-05-26 15:07:12 -04:00
|
|
|
use deno_core::LocalInspectorSession;
|
2021-01-07 05:53:57 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2021-02-24 09:27:51 -05:00
|
|
|
use regex::Regex;
|
2020-09-13 09:01:30 -04:00
|
|
|
use serde::Deserialize;
|
2020-12-21 08:04:25 -05:00
|
|
|
use serde::Serialize;
|
2021-01-28 09:11:38 -05:00
|
|
|
use sourcemap::SourceMap;
|
2020-12-21 08:04:25 -05:00
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
2021-01-20 09:51:36 -05:00
|
|
|
use swc_common::Span;
|
2020-12-21 08:04:25 -05:00
|
|
|
use uuid::Uuid;
|
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
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
pub async fn start_collecting(&mut self) -> Result<(), AnyError> {
|
2020-10-07 04:24:15 -04:00
|
|
|
self.session.post_message("Debugger.enable", None).await?;
|
|
|
|
self.session.post_message("Profiler.enable", None).await?;
|
2020-09-13 09:01:30 -04:00
|
|
|
self
|
2020-09-29 17:05:06 -04:00
|
|
|
.session
|
2020-09-19 08:01:56 -04:00
|
|
|
.post_message(
|
2020-10-07 04:24:15 -04:00
|
|
|
"Profiler.startPreciseCoverage",
|
2020-09-19 08:01:56 -04:00
|
|
|
Some(json!({"callCount": true, "detailed": true})),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-09-13 09:01:30 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
pub async fn stop_collecting(&mut self) -> Result<(), AnyError> {
|
2020-09-19 08:01:56 -04:00
|
|
|
let result = self
|
2020-09-29 17:05:06 -04:00
|
|
|
.session
|
2020-10-07 04:24:15 -04:00
|
|
|
.post_message("Profiler.takePreciseCoverage", None)
|
2020-09-13 09:01:30 -04:00
|
|
|
.await?;
|
2020-09-21 23:59:02 -04:00
|
|
|
|
2020-09-19 08:01:56 -04:00
|
|
|
let take_coverage_result: TakePreciseCoverageResult =
|
|
|
|
serde_json::from_value(result)?;
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
fs::create_dir_all(&self.dir)?;
|
|
|
|
|
|
|
|
let script_coverages = take_coverage_result.result;
|
|
|
|
for script_coverage in script_coverages {
|
|
|
|
let filename = format!("{}.json", Uuid::new_v4());
|
2021-01-07 05:53:57 -05:00
|
|
|
let json = serde_json::to_string(&script_coverage)?;
|
2020-12-21 08:04:25 -05:00
|
|
|
fs::write(self.dir.join(filename), &json)?;
|
2020-09-21 23:59:02 -04:00
|
|
|
}
|
|
|
|
|
2020-10-07 04:24:15 -04:00
|
|
|
self.session.post_message("Profiler.disable", None).await?;
|
|
|
|
self.session.post_message("Debugger.disable", None).await?;
|
2020-09-13 09:01:30 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
// TODO(caspervonb) all of these structs can and should be made private, possibly moved to
|
|
|
|
// inspector::protocol.
|
2021-01-04 11:01:21 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2020-09-13 09:01:30 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CoverageRange {
|
|
|
|
pub start_offset: usize,
|
|
|
|
pub end_offset: usize,
|
|
|
|
pub count: usize,
|
|
|
|
}
|
|
|
|
|
2021-01-04 11:01:21 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2020-09-13 09:01:30 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct FunctionCoverage {
|
|
|
|
pub function_name: String,
|
|
|
|
pub ranges: Vec<CoverageRange>,
|
|
|
|
pub is_block_coverage: bool,
|
|
|
|
}
|
|
|
|
|
2021-01-04 11:01:21 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2020-09-13 09:01:30 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ScriptCoverage {
|
|
|
|
pub script_id: String,
|
|
|
|
pub url: String,
|
|
|
|
pub functions: Vec<FunctionCoverage>,
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2020-09-13 09:01:30 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct TakePreciseCoverageResult {
|
|
|
|
result: Vec<ScriptCoverage>,
|
|
|
|
}
|
|
|
|
|
2020-09-21 23:59:02 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct GetScriptSourceResult {
|
|
|
|
pub script_source: String,
|
|
|
|
pub bytecode: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
pub enum CoverageReporterKind {
|
|
|
|
Pretty,
|
|
|
|
Lcov,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_reporter(
|
|
|
|
kind: CoverageReporterKind,
|
|
|
|
) -> Box<dyn CoverageReporter + Send> {
|
|
|
|
match kind {
|
|
|
|
CoverageReporterKind::Lcov => Box::new(LcovCoverageReporter::new()),
|
|
|
|
CoverageReporterKind::Pretty => Box::new(PrettyCoverageReporter::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait CoverageReporter {
|
|
|
|
fn visit_coverage(
|
|
|
|
&mut self,
|
|
|
|
script_coverage: &ScriptCoverage,
|
|
|
|
script_source: &str,
|
|
|
|
maybe_source_map: Option<Vec<u8>>,
|
|
|
|
maybe_original_source: Option<String>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn done(&mut self);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LcovCoverageReporter {}
|
|
|
|
|
|
|
|
impl LcovCoverageReporter {
|
|
|
|
pub fn new() -> LcovCoverageReporter {
|
|
|
|
LcovCoverageReporter {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoverageReporter for LcovCoverageReporter {
|
|
|
|
fn visit_coverage(
|
|
|
|
&mut self,
|
|
|
|
script_coverage: &ScriptCoverage,
|
|
|
|
script_source: &str,
|
|
|
|
maybe_source_map: Option<Vec<u8>>,
|
|
|
|
_maybe_original_source: Option<String>,
|
|
|
|
) {
|
|
|
|
// TODO(caspervonb) cleanup and reduce duplication between reporters, pre-compute line coverage
|
|
|
|
// elsewhere.
|
2021-03-25 14:17:37 -04:00
|
|
|
let maybe_source_map = maybe_source_map
|
|
|
|
.map(|source_map| SourceMap::from_slice(&source_map).unwrap());
|
2021-02-24 09:27:51 -05:00
|
|
|
|
|
|
|
let url = Url::parse(&script_coverage.url).unwrap();
|
|
|
|
let file_path = url.to_file_path().unwrap();
|
|
|
|
println!("SF:{}", file_path.to_str().unwrap());
|
|
|
|
|
|
|
|
let mut functions_found = 0;
|
|
|
|
for function in &script_coverage.functions {
|
|
|
|
if function.function_name.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let source_line = script_source[0..function.ranges[0].start_offset]
|
|
|
|
.split('\n')
|
|
|
|
.count();
|
|
|
|
|
|
|
|
let line_index = if let Some(source_map) = maybe_source_map.as_ref() {
|
|
|
|
source_map
|
|
|
|
.tokens()
|
|
|
|
.find(|token| token.get_dst_line() as usize == source_line)
|
|
|
|
.map(|token| token.get_src_line() as usize)
|
|
|
|
.unwrap_or(0)
|
|
|
|
} else {
|
|
|
|
source_line
|
|
|
|
};
|
|
|
|
|
|
|
|
let function_name = &function.function_name;
|
|
|
|
|
|
|
|
println!("FN:{},{}", line_index + 1, function_name);
|
|
|
|
|
|
|
|
functions_found += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut functions_hit = 0;
|
|
|
|
for function in &script_coverage.functions {
|
|
|
|
if function.function_name.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let execution_count = function.ranges[0].count;
|
|
|
|
let function_name = &function.function_name;
|
|
|
|
|
|
|
|
println!("FNDA:{},{}", execution_count, function_name);
|
|
|
|
|
|
|
|
if execution_count != 0 {
|
|
|
|
functions_hit += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("FNF:{}", functions_found);
|
|
|
|
println!("FNH:{}", functions_hit);
|
|
|
|
|
|
|
|
let mut branches_found = 0;
|
|
|
|
let mut branches_hit = 0;
|
|
|
|
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() {
|
|
|
|
let source_line =
|
|
|
|
script_source[0..range.start_offset].split('\n').count();
|
|
|
|
|
|
|
|
let line_index = if let Some(source_map) = maybe_source_map.as_ref() {
|
|
|
|
source_map
|
|
|
|
.tokens()
|
|
|
|
.find(|token| token.get_dst_line() as usize == source_line)
|
|
|
|
.map(|token| token.get_src_line() as usize)
|
|
|
|
.unwrap_or(0)
|
|
|
|
} else {
|
|
|
|
source_line
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
range.count.to_string()
|
|
|
|
} else {
|
|
|
|
"-".to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"BRDA:{},{},{},{}",
|
|
|
|
line_index + 1,
|
|
|
|
block_number,
|
|
|
|
branch_number,
|
|
|
|
taken
|
|
|
|
);
|
|
|
|
|
|
|
|
branches_found += 1;
|
|
|
|
if range.count > 0 {
|
|
|
|
branches_hit += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("BRF:{}", branches_found);
|
|
|
|
println!("BRH:{}", branches_hit);
|
|
|
|
|
|
|
|
let lines = script_source.split('\n').collect::<Vec<_>>();
|
|
|
|
let line_offsets = {
|
|
|
|
let mut offsets: Vec<(usize, usize)> = Vec::new();
|
|
|
|
let mut index = 0;
|
|
|
|
|
|
|
|
for line in &lines {
|
|
|
|
offsets.push((index, index + line.len() + 1));
|
|
|
|
index += line.len() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
offsets
|
|
|
|
};
|
|
|
|
|
|
|
|
let line_counts = line_offsets
|
|
|
|
.iter()
|
|
|
|
.map(|(line_start_offset, line_end_offset)| {
|
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
if range.start_offset <= *line_start_offset
|
|
|
|
&& range.end_offset >= *line_end_offset
|
|
|
|
{
|
|
|
|
count += range.count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 05:51:01 -05:00
|
|
|
// We reset the count if any block with a zero count overlaps with the line range.
|
2021-02-24 09:27:51 -05:00
|
|
|
for function in &script_coverage.functions {
|
|
|
|
for range in &function.ranges {
|
|
|
|
if range.count > 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-08 05:51:01 -05:00
|
|
|
let overlaps = std::cmp::max(line_end_offset, &range.end_offset)
|
|
|
|
- std::cmp::min(line_start_offset, &range.start_offset)
|
|
|
|
< (line_end_offset - line_start_offset)
|
|
|
|
+ (range.end_offset - range.start_offset);
|
|
|
|
|
|
|
|
if overlaps {
|
2021-02-24 09:27:51 -05:00
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count
|
|
|
|
})
|
|
|
|
.collect::<Vec<usize>>();
|
|
|
|
|
|
|
|
let found_lines = if let Some(source_map) = maybe_source_map.as_ref() {
|
|
|
|
let mut found_lines = line_counts
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, count)| {
|
|
|
|
source_map
|
|
|
|
.tokens()
|
|
|
|
.filter(move |token| token.get_dst_line() as usize == index)
|
|
|
|
.map(move |token| (token.get_src_line() as usize, *count))
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<(usize, usize)>>();
|
|
|
|
|
|
|
|
found_lines.sort_unstable_by_key(|(index, _)| *index);
|
|
|
|
found_lines.dedup_by_key(|(index, _)| *index);
|
|
|
|
found_lines
|
|
|
|
} else {
|
|
|
|
line_counts
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, count)| (index, *count))
|
|
|
|
.collect::<Vec<(usize, usize)>>()
|
|
|
|
};
|
|
|
|
|
|
|
|
for (index, count) in &found_lines {
|
|
|
|
println!("DA:{},{}", index + 1, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
let lines_hit = found_lines.iter().filter(|(_, count)| *count != 0).count();
|
|
|
|
|
|
|
|
println!("LH:{}", lines_hit);
|
|
|
|
|
|
|
|
let lines_found = found_lines.len();
|
|
|
|
println!("LF:{}", lines_found);
|
|
|
|
|
|
|
|
println!("end_of_record");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn done(&mut self) {}
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
pub struct PrettyCoverageReporter {}
|
|
|
|
|
2020-09-13 09:01:30 -04:00
|
|
|
impl PrettyCoverageReporter {
|
2021-02-24 09:27:51 -05:00
|
|
|
pub fn new() -> PrettyCoverageReporter {
|
|
|
|
PrettyCoverageReporter {}
|
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-02-24 09:27:51 -05:00
|
|
|
impl CoverageReporter for PrettyCoverageReporter {
|
|
|
|
fn visit_coverage(
|
2020-12-21 08:04:25 -05:00
|
|
|
&mut self,
|
|
|
|
script_coverage: &ScriptCoverage,
|
|
|
|
script_source: &str,
|
2021-01-28 09:11:38 -05:00
|
|
|
maybe_source_map: Option<Vec<u8>>,
|
|
|
|
maybe_original_source: Option<String>,
|
2020-12-21 08:04:25 -05:00
|
|
|
) {
|
2021-03-25 14:17:37 -04:00
|
|
|
let maybe_source_map = maybe_source_map
|
|
|
|
.map(|source_map| SourceMap::from_slice(&source_map).unwrap());
|
2021-01-28 09:11:38 -05:00
|
|
|
|
2021-01-20 09:51:36 -05:00
|
|
|
let mut ignored_spans: Vec<Span> = Vec::new();
|
|
|
|
for item in ast::lex("", script_source, &MediaType::JavaScript) {
|
|
|
|
if let TokenOrComment::Token(_) = item.inner {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ignored_spans.push(item.span);
|
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-01-20 09:51:36 -05:00
|
|
|
let lines = script_source.split('\n').collect::<Vec<_>>();
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-01-29 14:45:22 -05:00
|
|
|
let line_offsets = {
|
|
|
|
let mut offsets: Vec<(usize, usize)> = Vec::new();
|
|
|
|
let mut index = 0;
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-01-29 14:45:22 -05:00
|
|
|
for line in &lines {
|
|
|
|
offsets.push((index, index + line.len() + 1));
|
|
|
|
index += line.len() + 1;
|
2021-01-20 09:51:36 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:45:22 -05:00
|
|
|
offsets
|
|
|
|
};
|
|
|
|
|
2021-02-24 09:27:51 -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.
|
2021-01-29 14:45:22 -05:00
|
|
|
let line_counts = line_offsets
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, (line_start_offset, line_end_offset))| {
|
|
|
|
let ignore = ignored_spans.iter().any(|span| {
|
|
|
|
(span.lo.0 as usize) <= *line_start_offset
|
|
|
|
&& (span.hi.0 as usize) >= *line_end_offset
|
|
|
|
});
|
|
|
|
|
|
|
|
if ignore {
|
|
|
|
return (index, 1);
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
2021-01-07 06:45:42 -05:00
|
|
|
|
2021-01-29 14:45:22 -05:00
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
if range.start_offset <= *line_start_offset
|
|
|
|
&& range.end_offset >= *line_end_offset
|
|
|
|
{
|
|
|
|
count += range.count;
|
|
|
|
}
|
2021-01-07 06:45:42 -05:00
|
|
|
}
|
2021-01-29 14:45:22 -05:00
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-03-08 05:51:01 -05:00
|
|
|
// We reset the count if any block with a zero count overlaps with the line range.
|
2021-01-29 14:45:22 -05:00
|
|
|
for function in &script_coverage.functions {
|
|
|
|
for range in &function.ranges {
|
|
|
|
if range.count > 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-08 05:51:01 -05:00
|
|
|
let overlaps = std::cmp::max(line_end_offset, &range.end_offset)
|
|
|
|
- std::cmp::min(line_start_offset, &range.start_offset)
|
|
|
|
< (line_end_offset - line_start_offset)
|
|
|
|
+ (range.end_offset - range.start_offset);
|
|
|
|
|
|
|
|
if overlaps {
|
2021-01-29 14:45:22 -05:00
|
|
|
count = 0;
|
|
|
|
}
|
2021-01-07 06:45:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:45:22 -05:00
|
|
|
(index, count)
|
|
|
|
})
|
|
|
|
.collect::<Vec<(usize, usize)>>();
|
2021-01-07 06:45:42 -05:00
|
|
|
|
2021-01-29 14:45:22 -05:00
|
|
|
let lines = if let Some(original_source) = maybe_original_source.as_ref() {
|
|
|
|
original_source.split('\n').collect::<Vec<_>>()
|
|
|
|
} else {
|
|
|
|
lines
|
|
|
|
};
|
|
|
|
|
|
|
|
let line_counts = if let Some(source_map) = maybe_source_map.as_ref() {
|
|
|
|
let mut line_counts = line_counts
|
|
|
|
.iter()
|
|
|
|
.map(|(index, count)| {
|
|
|
|
source_map
|
|
|
|
.tokens()
|
|
|
|
.filter(move |token| token.get_dst_line() as usize == *index)
|
|
|
|
.map(move |token| (token.get_src_line() as usize, *count))
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<(usize, usize)>>();
|
|
|
|
|
|
|
|
line_counts.sort_unstable_by_key(|(index, _)| *index);
|
|
|
|
line_counts.dedup_by_key(|(index, _)| *index);
|
|
|
|
|
|
|
|
line_counts
|
|
|
|
} else {
|
|
|
|
line_counts
|
|
|
|
};
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
print!("cover {} ... ", script_coverage.url);
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
let hit_lines = line_counts
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, count)| *count != 0)
|
|
|
|
.map(|(index, _)| *index);
|
2020-09-23 14:12:24 -04:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
let missed_lines = line_counts
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, count)| *count == 0)
|
|
|
|
.map(|(index, _)| *index);
|
2021-01-29 14:45:22 -05:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
let lines_found = line_counts.len();
|
|
|
|
let lines_hit = hit_lines.count();
|
|
|
|
let line_ratio = lines_hit as f32 / lines_found as f32;
|
2021-01-29 14:45:22 -05:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
let line_coverage =
|
|
|
|
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-02-24 09:27:51 -05:00
|
|
|
let mut last_line = None;
|
|
|
|
for line_index in missed_lines {
|
|
|
|
const WIDTH: usize = 4;
|
|
|
|
const SEPERATOR: &str = "|";
|
2020-11-24 16:26:38 -05:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
// Put a horizontal separator between disjoint runs of lines
|
|
|
|
if let Some(last_line) = last_line {
|
|
|
|
if last_line + 1 != line_index {
|
2021-03-01 06:49:58 -05:00
|
|
|
let dash = colors::gray("-".repeat(WIDTH + 1));
|
2021-02-24 09:27:51 -05:00
|
|
|
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$} {} {}",
|
|
|
|
line_index + 1,
|
|
|
|
colors::gray(SEPERATOR),
|
|
|
|
colors::red(&lines[line_index]),
|
|
|
|
width = WIDTH
|
|
|
|
);
|
2020-11-24 16:26:38 -05:00
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
last_line = Some(line_index);
|
2020-09-23 14:12:24 -04:00
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
|
|
|
fn done(&mut self) {}
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
fn collect_coverages(
|
|
|
|
files: Vec<PathBuf>,
|
|
|
|
ignore: Vec<PathBuf>,
|
|
|
|
) -> Result<Vec<ScriptCoverage>, AnyError> {
|
2021-01-07 05:53:57 -05:00
|
|
|
let mut coverages: Vec<ScriptCoverage> = Vec::new();
|
2021-02-24 09:27:51 -05:00
|
|
|
let file_paths = collect_files(&files, &ignore, |file_path| {
|
|
|
|
file_path.extension().map_or(false, |ext| ext == "json")
|
|
|
|
})?;
|
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)?;
|
2021-01-04 11:01:21 -05:00
|
|
|
|
2021-01-07 05:53:57 -05:00
|
|
|
let existing_coverage =
|
|
|
|
coverages.iter_mut().find(|x| x.url == new_coverage.url);
|
2021-01-04 11:01:21 -05:00
|
|
|
|
|
|
|
if let Some(existing_coverage) = existing_coverage {
|
2021-01-07 05:53:57 -05:00
|
|
|
for new_function in new_coverage.functions {
|
2021-01-04 11:01:21 -05:00
|
|
|
let existing_function = existing_coverage
|
|
|
|
.functions
|
|
|
|
.iter_mut()
|
|
|
|
.find(|x| x.function_name == new_function.function_name);
|
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(existing_range) = existing_range {
|
|
|
|
existing_range.count += new_range.count;
|
|
|
|
} else {
|
|
|
|
existing_function.ranges.push(new_range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-07 05:53:57 -05:00
|
|
|
existing_coverage.functions.push(new_function);
|
2020-12-21 08:04:25 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 11:01:21 -05:00
|
|
|
} else {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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__")
|
|
|
|
|| e.url.ends_with("$deno$test.ts");
|
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-02-24 09:27:51 -05:00
|
|
|
pub async fn cover_files(
|
|
|
|
flags: Flags,
|
|
|
|
files: Vec<PathBuf>,
|
|
|
|
ignore: Vec<PathBuf>,
|
|
|
|
include: Vec<String>,
|
|
|
|
exclude: Vec<String>,
|
|
|
|
lcov: bool,
|
2020-12-21 08:04:25 -05:00
|
|
|
) -> Result<(), AnyError> {
|
2021-02-24 09:27:51 -05:00
|
|
|
let program_state = ProgramState::build(flags).await?;
|
|
|
|
|
|
|
|
let script_coverages = collect_coverages(files, ignore)?;
|
|
|
|
let script_coverages = filter_coverages(script_coverages, include, exclude);
|
|
|
|
|
|
|
|
let reporter_kind = if lcov {
|
|
|
|
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);
|
|
|
|
|
|
|
|
for script_coverage in script_coverages {
|
2021-01-07 05:53:57 -05:00
|
|
|
let module_specifier =
|
2021-02-17 13:47:18 -05:00
|
|
|
deno_core::resolve_url_or_path(&script_coverage.url)?;
|
2021-01-07 05:53:57 -05:00
|
|
|
program_state
|
|
|
|
.prepare_module_load(
|
|
|
|
module_specifier.clone(),
|
|
|
|
TypeLib::UnstableDenoWindow,
|
|
|
|
Permissions::allow_all(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2021-01-07 05:53:57 -05:00
|
|
|
false,
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let module_source = program_state.load(module_specifier.clone(), None)?;
|
|
|
|
let script_source = &module_source.code;
|
|
|
|
|
2021-01-28 09:11:38 -05:00
|
|
|
let maybe_source_map = program_state.get_source_map(&script_coverage.url);
|
|
|
|
let maybe_cached_source = program_state
|
|
|
|
.file_fetcher
|
|
|
|
.get_source(&module_specifier)
|
|
|
|
.map(|f| f.source);
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
reporter.visit_coverage(
|
2021-01-28 09:11:38 -05:00
|
|
|
&script_coverage,
|
|
|
|
&script_source,
|
|
|
|
maybe_source_map,
|
|
|
|
maybe_cached_source,
|
|
|
|
);
|
2020-12-21 08:04:25 -05:00
|
|
|
}
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
reporter.done();
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
Ok(())
|
|
|
|
}
|