2020-09-13 09:01:30 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use crate::colors;
|
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;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::inspector::InspectorSession;
|
2020-09-13 09:01:30 -04:00
|
|
|
use serde::Deserialize;
|
2020-12-21 08:04:25 -05:00
|
|
|
use serde::Serialize;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
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,
|
2020-09-29 17:05:06 -04:00
|
|
|
session: Box<InspectorSession>,
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CoverageCollector {
|
2020-12-21 08:04:25 -05:00
|
|
|
pub fn new(dir: PathBuf, session: Box<InspectorSession>) -> Self {
|
|
|
|
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?;
|
2020-09-21 23:59:02 -04:00
|
|
|
|
2020-10-07 04:24:15 -04:00
|
|
|
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 get_script_source_value = self
|
2020-09-29 17:05:06 -04:00
|
|
|
.session
|
2020-09-21 23:59:02 -04:00
|
|
|
.post_message(
|
2020-10-07 04:24:15 -04:00
|
|
|
"Debugger.getScriptSource",
|
2020-09-21 23:59:02 -04:00
|
|
|
Some(json!({
|
|
|
|
"scriptId": script_coverage.script_id,
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let get_script_source_result: GetScriptSourceResult =
|
2020-12-21 08:04:25 -05:00
|
|
|
serde_json::from_value(get_script_source_value)?;
|
2020-09-21 23:59:02 -04:00
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
let script_source = get_script_source_result.script_source.clone();
|
|
|
|
|
|
|
|
let coverage = Coverage {
|
2020-09-21 23:59:02 -04:00
|
|
|
script_coverage,
|
2020-12-21 08:04:25 -05:00
|
|
|
script_source,
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(caspervonb) Would be much better to look up the source during the reporting stage
|
|
|
|
// instead of storing it here.
|
|
|
|
// Long term, that's what we should be doing.
|
|
|
|
let filename = format!("{}.json", Uuid::new_v4());
|
|
|
|
let json = serde_json::to_string(&coverage)?;
|
|
|
|
fs::write(self.dir.join(filename), &json)?;
|
2020-09-21 23:59:02 -04:00
|
|
|
}
|
|
|
|
|
2020-09-13 09:01:30 -04:00
|
|
|
self
|
2020-09-29 17:05:06 -04:00
|
|
|
.session
|
2020-10-07 04:24:15 -04:00
|
|
|
.post_message("Profiler.stopPreciseCoverage", None)
|
2020-09-21 23:59:02 -04:00
|
|
|
.await?;
|
2020-12-21 08:04:25 -05: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-01-04 11:01:21 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2020-09-21 23:59:02 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Coverage {
|
|
|
|
pub script_coverage: ScriptCoverage,
|
|
|
|
pub script_source: String,
|
|
|
|
}
|
|
|
|
|
2020-09-13 09:01:30 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[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>,
|
|
|
|
}
|
|
|
|
|
2020-09-13 09:01:30 -04:00
|
|
|
pub struct PrettyCoverageReporter {
|
2020-09-23 14:12:24 -04:00
|
|
|
quiet: bool,
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(caspervonb) add support for lcov output (see geninfo(1) for format spec).
|
|
|
|
impl PrettyCoverageReporter {
|
2020-09-23 14:12:24 -04:00
|
|
|
pub fn new(quiet: bool) -> PrettyCoverageReporter {
|
|
|
|
PrettyCoverageReporter { quiet }
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
pub fn visit_coverage(
|
|
|
|
&mut self,
|
|
|
|
script_coverage: &ScriptCoverage,
|
|
|
|
script_source: &str,
|
|
|
|
) {
|
|
|
|
let lines = script_source.lines().collect::<Vec<_>>();
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2020-09-23 14:12:24 -04:00
|
|
|
let mut covered_lines: Vec<usize> = Vec::new();
|
|
|
|
let mut uncovered_lines: Vec<usize> = Vec::new();
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2020-09-23 14:12:24 -04:00
|
|
|
let mut line_start_offset = 0;
|
|
|
|
for (index, line) in lines.iter().enumerate() {
|
2020-09-13 09:01:30 -04:00
|
|
|
let line_end_offset = line_start_offset + line.len();
|
|
|
|
|
|
|
|
let mut count = 0;
|
2020-12-21 08:04:25 -05:00
|
|
|
for function in &script_coverage.functions {
|
2020-09-13 09:01:30 -04:00
|
|
|
for range in &function.ranges {
|
|
|
|
if range.start_offset <= line_start_offset
|
|
|
|
&& range.end_offset >= line_end_offset
|
|
|
|
{
|
|
|
|
if range.count == 0 {
|
|
|
|
count = 0;
|
|
|
|
break;
|
|
|
|
}
|
2020-09-23 14:12:24 -04:00
|
|
|
|
|
|
|
count += range.count;
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 14:12:24 -04:00
|
|
|
line_start_offset = line_end_offset;
|
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
if count > 0 {
|
2020-09-23 14:12:24 -04:00
|
|
|
covered_lines.push(index);
|
|
|
|
} else {
|
|
|
|
uncovered_lines.push(index);
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 14:12:24 -04:00
|
|
|
if !self.quiet {
|
2020-12-21 08:04:25 -05:00
|
|
|
print!("cover {} ... ", script_coverage.url);
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2020-09-23 14:12:24 -04:00
|
|
|
let line_coverage_ratio = covered_lines.len() as f32 / lines.len() as f32;
|
|
|
|
let line_coverage = format!(
|
|
|
|
"{:.3}% ({}/{})",
|
|
|
|
line_coverage_ratio * 100.0,
|
|
|
|
covered_lines.len(),
|
|
|
|
lines.len()
|
|
|
|
);
|
|
|
|
|
|
|
|
if line_coverage_ratio >= 0.9 {
|
|
|
|
println!("{}", colors::green(&line_coverage));
|
|
|
|
} else if line_coverage_ratio >= 0.75 {
|
|
|
|
println!("{}", colors::yellow(&line_coverage));
|
|
|
|
} else {
|
|
|
|
println!("{}", colors::red(&line_coverage));
|
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2020-11-24 16:26:38 -05:00
|
|
|
let mut last_line = None;
|
2020-09-23 14:12:24 -04:00
|
|
|
for line_index in uncovered_lines {
|
2020-11-24 16:26:38 -05:00
|
|
|
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-09-23 14:12:24 -04:00
|
|
|
println!(
|
2020-11-24 16:26:38 -05:00
|
|
|
"{:width$} {} {}",
|
2020-09-23 14:12:24 -04:00
|
|
|
line_index + 1,
|
2020-11-24 16:26:38 -05:00
|
|
|
colors::gray(SEPERATOR),
|
2020-09-23 14:12:24 -04:00
|
|
|
colors::red(&lines[line_index]),
|
2020-11-24 16:26:38 -05:00
|
|
|
width = WIDTH
|
2020-09-23 14:12:24 -04:00
|
|
|
);
|
2020-11-24 16:26:38 -05:00
|
|
|
|
|
|
|
last_line = Some(line_index);
|
2020-09-23 14:12:24 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
fn collect_coverages(dir: &PathBuf) -> Result<Vec<Coverage>, AnyError> {
|
|
|
|
let mut coverages: Vec<Coverage> = Vec::new();
|
|
|
|
|
|
|
|
let entries = fs::read_dir(dir)?;
|
|
|
|
for entry in entries {
|
|
|
|
let json = fs::read_to_string(entry.unwrap().path())?;
|
2021-01-04 11:01:21 -05:00
|
|
|
let new_coverage: Coverage = serde_json::from_str(&json)?;
|
|
|
|
|
|
|
|
let existing_coverage = coverages
|
|
|
|
.iter_mut()
|
|
|
|
.find(|x| x.script_coverage.url == new_coverage.script_coverage.url);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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 {
|
|
|
|
existing_coverage
|
|
|
|
.script_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-04 11:01:21 -05:00
|
|
|
coverages.sort_by_key(|k| k.script_coverage.url.clone());
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
Ok(coverages)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filter_coverages(
|
2020-09-21 23:59:02 -04:00
|
|
|
coverages: Vec<Coverage>,
|
2020-12-21 08:04:25 -05:00
|
|
|
exclude: Vec<Url>,
|
2020-09-21 23:59:02 -04:00
|
|
|
) -> Vec<Coverage> {
|
2020-09-13 09:01:30 -04:00
|
|
|
coverages
|
|
|
|
.into_iter()
|
|
|
|
.filter(|e| {
|
2020-09-21 23:59:02 -04:00
|
|
|
if let Ok(url) = Url::parse(&e.script_coverage.url) {
|
|
|
|
if url.path().ends_with("__anonymous__") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
for module_url in &exclude {
|
|
|
|
if &url == module_url {
|
2020-09-13 09:01:30 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(path) = url.to_file_path() {
|
2020-12-21 08:04:25 -05:00
|
|
|
for module_url in &exclude {
|
|
|
|
if let Ok(module_path) = module_url.to_file_path() {
|
|
|
|
if path.starts_with(module_path.parent().unwrap()) {
|
2020-09-13 09:01:30 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
})
|
2020-09-21 23:59:02 -04:00
|
|
|
.collect::<Vec<Coverage>>()
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
2020-12-21 08:04:25 -05:00
|
|
|
|
|
|
|
pub fn report_coverages(
|
|
|
|
dir: &PathBuf,
|
|
|
|
quiet: bool,
|
|
|
|
exclude: Vec<Url>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let coverages = collect_coverages(dir)?;
|
|
|
|
let coverages = filter_coverages(coverages, exclude);
|
|
|
|
|
|
|
|
let mut coverage_reporter = PrettyCoverageReporter::new(quiet);
|
|
|
|
for coverage in coverages {
|
|
|
|
let script_coverage = coverage.script_coverage;
|
|
|
|
let script_source = coverage.script_source;
|
|
|
|
coverage_reporter.visit_coverage(&script_coverage, &script_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|