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
|
|
|
|
|
|
|
use crate::colors;
|
2021-02-24 09:27:51 -05:00
|
|
|
use crate::flags::Flags;
|
|
|
|
use crate::fs_util::collect_files;
|
2021-01-07 05:53:57 -05:00
|
|
|
use crate::module_graph::TypeLib;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
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;
|
2021-10-06 01:05:18 -04:00
|
|
|
use deno_core::resolve_url_or_path;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2021-05-26 15:07:12 -04:00
|
|
|
use deno_core::LocalInspectorSession;
|
2021-10-06 01:05:18 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
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;
|
2021-10-06 01:05:18 -04:00
|
|
|
use std::cmp;
|
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;
|
|
|
|
use std::io::Write;
|
2020-12-21 08:04:25 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use uuid::Uuid;
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
// TODO(caspervonb) These structs are specific to the inspector protocol and should be refactored
|
|
|
|
// into a reusable module.
|
2021-07-08 11:53:56 -04:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-10-06 01:05:18 -04:00
|
|
|
struct CoverageRange {
|
2021-07-08 11:53:56 -04:00
|
|
|
pub start_offset: usize,
|
|
|
|
pub end_offset: usize,
|
|
|
|
pub count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-10-06 01:05:18 -04:00
|
|
|
struct FunctionCoverage {
|
2021-07-08 11:53:56 -04:00
|
|
|
pub function_name: String,
|
|
|
|
pub ranges: Vec<CoverageRange>,
|
|
|
|
pub is_block_coverage: bool,
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
struct LineCoverage {
|
|
|
|
pub start_offset: usize,
|
|
|
|
pub end_offset: usize,
|
|
|
|
pub ranges: Vec<CoverageRange>,
|
|
|
|
}
|
|
|
|
|
2021-07-08 11:53:56 -04:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-10-06 01:05:18 -04:00
|
|
|
struct ScriptCoverage {
|
2021-07-08 11:53:56 -04:00
|
|
|
pub script_id: String,
|
|
|
|
pub url: String,
|
|
|
|
pub functions: Vec<FunctionCoverage>,
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
struct CoverageResult {
|
|
|
|
pub lines: Vec<LineCoverage>,
|
|
|
|
pub functions: Vec<FunctionCoverage>,
|
|
|
|
}
|
|
|
|
|
2021-07-08 11:53:56 -04:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-10-06 01:05:18 -04:00
|
|
|
struct StartPreciseCoverageParameters {
|
2021-07-08 11:53:56 -04:00
|
|
|
pub call_count: bool,
|
|
|
|
pub detailed: bool,
|
|
|
|
pub allow_triggered_updates: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-10-06 01:05:18 -04:00
|
|
|
struct StartPreciseCoverageReturnObject {
|
2021-07-08 11:53:56 -04:00
|
|
|
pub timestamp: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-10-06 01:05:18 -04:00
|
|
|
struct TakePreciseCoverageReturnObject {
|
2021-07-08 11:53:56 -04:00
|
|
|
pub result: Vec<ScriptCoverage>,
|
|
|
|
pub timestamp: f64,
|
|
|
|
}
|
|
|
|
|
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> {
|
2020-10-07 04:24:15 -04: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> {
|
2020-10-07 04:24:15 -04: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> {
|
|
|
|
self.session.post_message("Debugger.disable", None).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn disable_profiler(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.session.post_message("Profiler.disable", None).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn start_precise_coverage(
|
|
|
|
&mut self,
|
|
|
|
parameters: StartPreciseCoverageParameters,
|
|
|
|
) -> Result<StartPreciseCoverageReturnObject, AnyError> {
|
|
|
|
let parameters_value = serde_json::to_value(parameters)?;
|
|
|
|
let return_value = self
|
2020-09-29 17:05:06 -04:00
|
|
|
.session
|
2021-07-08 11:53:56 -04:00
|
|
|
.post_message("Profiler.startPreciseCoverage", Some(parameters_value))
|
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
|
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
|
|
|
|
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)?);
|
|
|
|
serde_json::to_writer_pretty(&mut out, &script_coverage)?;
|
|
|
|
out.write_all(b"\n")?;
|
|
|
|
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-10-06 01:05:18 -04:00
|
|
|
enum CoverageReporterKind {
|
2021-02-24 09:27:51 -05:00
|
|
|
Pretty,
|
|
|
|
Lcov,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_reporter(
|
|
|
|
kind: CoverageReporterKind,
|
|
|
|
) -> Box<dyn CoverageReporter + Send> {
|
|
|
|
match kind {
|
|
|
|
CoverageReporterKind::Pretty => Box::new(PrettyCoverageReporter::new()),
|
2021-10-06 01:05:18 -04:00
|
|
|
CoverageReporterKind::Lcov => Box::new(LcovCoverageReporter::new()),
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
trait CoverageReporter {
|
|
|
|
fn report_result(
|
2021-02-24 09:27:51 -05:00
|
|
|
&mut self,
|
2021-10-06 01:05:18 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
result: &CoverageResult,
|
|
|
|
source: &str,
|
2021-02-24 09:27:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LcovCoverageReporter {}
|
|
|
|
|
|
|
|
impl LcovCoverageReporter {
|
|
|
|
pub fn new() -> LcovCoverageReporter {
|
|
|
|
LcovCoverageReporter {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoverageReporter for LcovCoverageReporter {
|
2021-10-06 01:05:18 -04:00
|
|
|
fn report_result(
|
2021-02-24 09:27:51 -05:00
|
|
|
&mut self,
|
2021-10-06 01:05:18 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
result: &CoverageResult,
|
|
|
|
source: &str,
|
2021-02-24 09:27:51 -05:00
|
|
|
) {
|
2021-10-06 01:05:18 -04:00
|
|
|
println!("SF:{}", specifier.to_file_path().unwrap().to_str().unwrap());
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let named_functions = result
|
|
|
|
.functions
|
|
|
|
.iter()
|
|
|
|
.filter(|block| !block.function_name.is_empty())
|
|
|
|
.collect::<Vec<&FunctionCoverage>>();
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
for block in &named_functions {
|
|
|
|
let index = source[0..block.ranges[0].start_offset].split('\n').count();
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
println!("FN:{},{}", index + 1, block.function_name);
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let hit_functions = named_functions
|
|
|
|
.iter()
|
|
|
|
.filter(|block| block.ranges[0].count > 0)
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<&FunctionCoverage>>();
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
for block in &hit_functions {
|
|
|
|
println!("FNDA:{},{}", block.ranges[0].count, block.function_name);
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
println!("FNF:{}", named_functions.len());
|
|
|
|
println!("FNH:{}", hit_functions.len());
|
2021-02-24 09:27:51 -05:00
|
|
|
|
|
|
|
let mut branches_found = 0;
|
|
|
|
let mut branches_hit = 0;
|
2021-10-06 01:05:18 -04:00
|
|
|
for (block_number, block) in result.functions.iter().enumerate() {
|
|
|
|
let block_hits = block.ranges[0].count;
|
|
|
|
for (branch_number, range) in block.ranges[1..].iter().enumerate() {
|
|
|
|
let line_index = source[0..range.start_offset].split('\n').count();
|
2021-02-24 09:27:51 -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 {
|
|
|
|
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);
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let enumerated_lines = result
|
|
|
|
.lines
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.collect::<Vec<(usize, &LineCoverage)>>();
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
for (index, line) in &enumerated_lines {
|
|
|
|
if line.ranges.is_empty() {
|
|
|
|
continue;
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let mut count = 0;
|
|
|
|
for range in &line.ranges {
|
|
|
|
count += range.count;
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
if range.count == 0 {
|
|
|
|
count = 0;
|
|
|
|
break;
|
2021-02-24 09:27:51 -05:00
|
|
|
}
|
2021-10-06 01:05:18 -04:00
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
println!("DA:{},{}", index + 1, count);
|
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let lines_found = enumerated_lines
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, line)| !line.ranges.is_empty())
|
|
|
|
.count();
|
2021-03-08 05:51:01 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
println!("LF:{}", lines_found);
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let lines_hit = enumerated_lines
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, line)| {
|
|
|
|
!line.ranges.is_empty()
|
|
|
|
&& !line.ranges.iter().any(|range| range.count == 0)
|
2021-02-24 09:27:51 -05:00
|
|
|
})
|
2021-10-06 01:05:18 -04:00
|
|
|
.count();
|
2021-02-24 09:27:51 -05:00
|
|
|
|
|
|
|
println!("LH:{}", lines_hit);
|
|
|
|
|
|
|
|
println!("end_of_record");
|
|
|
|
}
|
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-10-06 01:05:18 -04:00
|
|
|
const PRETTY_LINE_WIDTH: usize = 4;
|
|
|
|
const PRETTY_LINE_SEPERATOR: &str = "|";
|
2021-02-24 09:27:51 -05:00
|
|
|
impl CoverageReporter for PrettyCoverageReporter {
|
2021-10-06 01:05:18 -04:00
|
|
|
fn report_result(
|
2020-12-21 08:04:25 -05:00
|
|
|
&mut self,
|
2021-10-06 01:05:18 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
result: &CoverageResult,
|
|
|
|
source: &str,
|
2020-12-21 08:04:25 -05:00
|
|
|
) {
|
2021-10-06 01:05:18 -04:00
|
|
|
print!("cover {} ... ", specifier);
|
2021-01-20 09:51:36 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let enumerated_lines = result
|
|
|
|
.lines
|
2021-01-29 14:45:22 -05:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2021-10-06 01:05:18 -04:00
|
|
|
.collect::<Vec<(usize, &LineCoverage)>>();
|
2021-01-29 14:45:22 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let found_lines = enumerated_lines
|
2021-02-24 09:27:51 -05:00
|
|
|
.iter()
|
2021-10-06 01:05:18 -04:00
|
|
|
.filter(|(_, coverage)| !coverage.ranges.is_empty())
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<(usize, &LineCoverage)>>();
|
2020-09-23 14:12:24 -04:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let missed_lines = found_lines
|
2021-02-24 09:27:51 -05:00
|
|
|
.iter()
|
2021-10-06 01:05:18 -04:00
|
|
|
.filter(|(_, coverage)| {
|
|
|
|
coverage.ranges.iter().any(|range| range.count == 0)
|
|
|
|
})
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<(usize, &LineCoverage)>>();
|
|
|
|
|
|
|
|
let line_ratio = (found_lines.len() - missed_lines.len()) as f32
|
|
|
|
/ found_lines.len() as f32;
|
|
|
|
let line_coverage = format!(
|
|
|
|
"{:.3}% ({}/{})",
|
|
|
|
line_ratio * 100.0,
|
|
|
|
found_lines.len() - missed_lines.len(),
|
|
|
|
found_lines.len()
|
|
|
|
);
|
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 01:05:18 -04:00
|
|
|
let mut maybe_last_index = None;
|
|
|
|
for (index, line) in missed_lines {
|
|
|
|
if let Some(last_index) = maybe_last_index {
|
|
|
|
if last_index + 1 != index {
|
|
|
|
let dash = colors::gray("-".repeat(PRETTY_LINE_WIDTH + 1));
|
|
|
|
println!("{}{}{}", dash, colors::gray(PRETTY_LINE_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-10-06 01:05:18 -04:00
|
|
|
let slice = &source[line.start_offset..line.end_offset];
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
println!(
|
|
|
|
"{:width$} {} {}",
|
2021-10-06 01:05:18 -04:00
|
|
|
index + 1,
|
|
|
|
colors::gray(PRETTY_LINE_SEPERATOR),
|
|
|
|
colors::red(&slice),
|
|
|
|
width = PRETTY_LINE_WIDTH,
|
2021-02-24 09:27:51 -05:00
|
|
|
);
|
2020-11-24 16:26:38 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
maybe_last_index = Some(index);
|
2020-09-23 14:12:24 -04:00
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
fn collect_script_coverages(
|
2021-02-24 09:27:51 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
fn filter_script_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__")
|
2021-07-26 08:05:44 -04:00
|
|
|
|| e.url.ends_with("$deno$test.js");
|
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 01:05:18 -04:00
|
|
|
fn offset_to_line_col(source: &str, offset: usize) -> Option<(u32, u32)> {
|
|
|
|
let mut line = 0;
|
|
|
|
let mut col = 0;
|
|
|
|
|
|
|
|
if let Some(slice) = source.get(0..offset) {
|
|
|
|
for ch in slice.bytes() {
|
|
|
|
if ch == b'\n' {
|
|
|
|
line += 1;
|
|
|
|
col = 0;
|
|
|
|
} else {
|
|
|
|
col += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some((line, col));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line_col_to_offset(source: &str, line: u32, col: u32) -> Option<usize> {
|
|
|
|
let mut current_col = 0;
|
|
|
|
let mut current_line = 0;
|
|
|
|
|
|
|
|
for (i, ch) in source.bytes().enumerate() {
|
|
|
|
if current_line == line && current_col == col {
|
|
|
|
return Some(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch == b'\n' {
|
|
|
|
current_line += 1;
|
|
|
|
current_col = 0;
|
|
|
|
} else {
|
|
|
|
current_col += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn cover_script(
|
|
|
|
program_state: ProcState,
|
|
|
|
script: ScriptCoverage,
|
|
|
|
) -> Result<CoverageResult, AnyError> {
|
|
|
|
let module_specifier = resolve_url_or_path(&script.url)?;
|
|
|
|
let file = program_state
|
|
|
|
.file_fetcher
|
|
|
|
.fetch(&module_specifier, &mut Permissions::allow_all())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let source = file.source.as_str();
|
|
|
|
|
|
|
|
let line_offsets = {
|
|
|
|
let mut line_offsets: Vec<(usize, usize)> = Vec::new();
|
|
|
|
let mut offset = 0;
|
|
|
|
|
|
|
|
for line in source.split('\n') {
|
|
|
|
line_offsets.push((offset, offset + line.len()));
|
|
|
|
offset += line.len() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_offsets
|
|
|
|
};
|
|
|
|
|
|
|
|
program_state
|
|
|
|
.prepare_module_load(
|
|
|
|
module_specifier.clone(),
|
|
|
|
TypeLib::UnstableDenoWindow,
|
|
|
|
Permissions::allow_all(),
|
|
|
|
Permissions::allow_all(),
|
|
|
|
false,
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let compiled_source =
|
|
|
|
program_state.load(module_specifier.clone(), None)?.code;
|
|
|
|
|
|
|
|
// TODO(caspervonb): source mapping is still a bit of a mess and we should try look into avoiding
|
|
|
|
// doing any loads at this stage of execution but it'll do for now.
|
|
|
|
let maybe_raw_source_map = program_state.get_source_map(&script.url);
|
|
|
|
if let Some(raw_source_map) = maybe_raw_source_map {
|
|
|
|
let source_map = SourceMap::from_slice(&raw_source_map)?;
|
|
|
|
|
|
|
|
// To avoid false positives we base our line ranges on the ranges of the compiled lines
|
|
|
|
let compiled_line_offsets = {
|
|
|
|
let mut line_offsets: Vec<(usize, usize)> = Vec::new();
|
|
|
|
let mut offset = 0;
|
|
|
|
|
|
|
|
for line in compiled_source.split('\n') {
|
|
|
|
line_offsets.push((offset, offset + line.len()));
|
|
|
|
offset += line.len() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_offsets
|
|
|
|
};
|
|
|
|
|
|
|
|
// First we get the adjusted ranges of these lines
|
|
|
|
let compiled_line_ranges = compiled_line_offsets
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(start_offset, end_offset)| {
|
|
|
|
// We completely ignore empty lines, they just cause trouble and can't map to anything
|
|
|
|
// meaningful.
|
|
|
|
let line = &compiled_source[*start_offset..*end_offset];
|
|
|
|
if line == "\n" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ranges = script
|
|
|
|
.functions
|
|
|
|
.iter()
|
|
|
|
.map(|function| {
|
|
|
|
function.ranges.iter().filter_map(|function_range| {
|
|
|
|
if &function_range.start_offset > end_offset {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if &function_range.end_offset < start_offset {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(CoverageRange {
|
|
|
|
start_offset: cmp::max(
|
|
|
|
*start_offset,
|
|
|
|
function_range.start_offset,
|
|
|
|
),
|
|
|
|
end_offset: cmp::min(*end_offset, function_range.end_offset),
|
|
|
|
count: function_range.count,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<CoverageRange>>();
|
|
|
|
|
|
|
|
Some(ranges)
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<CoverageRange>>();
|
|
|
|
|
|
|
|
// Then we map those adjusted ranges from their closest tokens to their source locations.
|
|
|
|
let mapped_line_ranges = compiled_line_ranges
|
|
|
|
.iter()
|
|
|
|
.map(|line_range| {
|
|
|
|
let (start_line, start_col) =
|
|
|
|
offset_to_line_col(&compiled_source, line_range.start_offset)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let start_token =
|
|
|
|
source_map.lookup_token(start_line, start_col).unwrap();
|
|
|
|
|
|
|
|
let (end_line, end_col) =
|
|
|
|
offset_to_line_col(&compiled_source, line_range.end_offset).unwrap();
|
|
|
|
|
|
|
|
let end_token = source_map.lookup_token(end_line, end_col).unwrap();
|
|
|
|
|
|
|
|
let mapped_start_offset = line_col_to_offset(
|
|
|
|
source,
|
|
|
|
start_token.get_src_line(),
|
|
|
|
start_token.get_src_col(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mapped_end_offset = line_col_to_offset(
|
|
|
|
source,
|
|
|
|
end_token.get_src_line(),
|
|
|
|
end_token.get_src_col(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
CoverageRange {
|
|
|
|
start_offset: mapped_start_offset,
|
|
|
|
end_offset: mapped_end_offset,
|
|
|
|
count: line_range.count,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<CoverageRange>>();
|
|
|
|
|
|
|
|
// Then we go through the source lines and grab any ranges that apply to any given line
|
|
|
|
// adjusting them as we go.
|
|
|
|
let lines = line_offsets
|
|
|
|
.iter()
|
|
|
|
.map(|(start_offset, end_offset)| {
|
|
|
|
let ranges = mapped_line_ranges
|
|
|
|
.iter()
|
|
|
|
.filter_map(|line_range| {
|
|
|
|
if &line_range.start_offset > end_offset {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if &line_range.end_offset < start_offset {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(CoverageRange {
|
|
|
|
start_offset: cmp::max(*start_offset, line_range.start_offset),
|
|
|
|
end_offset: cmp::min(*end_offset, line_range.end_offset),
|
|
|
|
count: line_range.count,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
LineCoverage {
|
|
|
|
start_offset: *start_offset,
|
|
|
|
end_offset: *end_offset,
|
|
|
|
ranges,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let functions = script
|
|
|
|
.functions
|
|
|
|
.iter()
|
|
|
|
.map(|function| {
|
|
|
|
let ranges = function
|
|
|
|
.ranges
|
|
|
|
.iter()
|
|
|
|
.map(|function_range| {
|
|
|
|
let (start_line, start_col) =
|
|
|
|
offset_to_line_col(&compiled_source, function_range.start_offset)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let start_token =
|
|
|
|
source_map.lookup_token(start_line, start_col).unwrap();
|
|
|
|
|
|
|
|
let mapped_start_offset = line_col_to_offset(
|
|
|
|
source,
|
|
|
|
start_token.get_src_line(),
|
|
|
|
start_token.get_src_col(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let (end_line, end_col) =
|
|
|
|
offset_to_line_col(&compiled_source, function_range.end_offset)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let end_token = source_map.lookup_token(end_line, end_col).unwrap();
|
|
|
|
|
|
|
|
let mapped_end_offset = line_col_to_offset(
|
|
|
|
source,
|
|
|
|
end_token.get_src_line(),
|
|
|
|
end_token.get_src_col(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
CoverageRange {
|
|
|
|
start_offset: mapped_start_offset,
|
|
|
|
end_offset: mapped_end_offset,
|
|
|
|
count: function_range.count,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
FunctionCoverage {
|
|
|
|
ranges,
|
|
|
|
is_block_coverage: function.is_block_coverage,
|
|
|
|
function_name: function.function_name.clone(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<FunctionCoverage>>();
|
|
|
|
|
|
|
|
return Ok(CoverageResult { lines, functions });
|
|
|
|
}
|
|
|
|
|
|
|
|
let functions = script.functions.clone();
|
|
|
|
|
|
|
|
let lines = line_offsets
|
|
|
|
.iter()
|
|
|
|
.map(|(start_offset, end_offset)| {
|
|
|
|
let line = &source[*start_offset..*end_offset];
|
|
|
|
if line == "\n" {
|
|
|
|
return LineCoverage {
|
|
|
|
start_offset: *start_offset,
|
|
|
|
end_offset: *end_offset,
|
|
|
|
ranges: Vec::new(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let ranges = script
|
|
|
|
.functions
|
|
|
|
.iter()
|
|
|
|
.map(|function| {
|
|
|
|
function.ranges.iter().filter_map(|function_range| {
|
|
|
|
if &function_range.start_offset > end_offset {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if &function_range.end_offset < start_offset {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(CoverageRange {
|
|
|
|
start_offset: cmp::max(
|
|
|
|
*start_offset,
|
|
|
|
function_range.start_offset,
|
|
|
|
),
|
|
|
|
end_offset: cmp::min(*end_offset, function_range.end_offset),
|
|
|
|
count: function_range.count,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
LineCoverage {
|
|
|
|
start_offset: *start_offset,
|
|
|
|
end_offset: *end_offset,
|
|
|
|
ranges,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(CoverageResult { lines, functions })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn cover_scripts(
|
2021-02-24 09:27:51 -05:00
|
|
|
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-09-24 11:10:42 -04:00
|
|
|
let ps = ProcState::build(flags).await?;
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let script_coverages = collect_script_coverages(files, ignore)?;
|
|
|
|
let script_coverages =
|
|
|
|
filter_script_coverages(script_coverages, include, exclude);
|
2021-02-24 09:27:51 -05:00
|
|
|
|
|
|
|
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-10-06 01:05:18 -04:00
|
|
|
let result = cover_script(ps.clone(), script_coverage.clone()).await?;
|
2021-01-07 05:53:57 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
let module_specifier = resolve_url_or_path(&script_coverage.url)?;
|
|
|
|
let file = ps
|
2021-01-28 09:11:38 -05:00
|
|
|
.file_fetcher
|
2021-10-06 01:05:18 -04:00
|
|
|
.fetch(&module_specifier, &mut Permissions::allow_all())
|
|
|
|
.await?;
|
2020-12-21 08:04:25 -05:00
|
|
|
|
2021-10-06 01:05:18 -04:00
|
|
|
reporter.report_result(&module_specifier, &result, &file.source);
|
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
Ok(())
|
|
|
|
}
|