2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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;
|
2023-11-05 17:58:59 -05:00
|
|
|
use crate::cdp;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
2023-10-03 19:05:06 -04:00
|
|
|
use crate::npm::CliNpmResolver;
|
2021-12-07 19:21:04 -05:00
|
|
|
use crate::tools::fmt::format_json;
|
2023-04-19 17:30:52 -04:00
|
|
|
use crate::tools::test::is_supported_test_path;
|
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;
|
2023-03-21 18:33:12 -04:00
|
|
|
use deno_core::ModuleCode;
|
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::Write;
|
2023-04-19 17:30:52 -04:00
|
|
|
use std::path::Path;
|
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 merge;
|
|
|
|
mod range_tree;
|
2023-12-10 23:30:38 -05:00
|
|
|
mod reporter;
|
2023-12-08 02:54:52 -05:00
|
|
|
mod util;
|
2023-11-05 17:58:59 -05:00
|
|
|
use merge::ProcessCoverage;
|
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,
|
2023-11-05 17:58:59 -05:00
|
|
|
parameters: cdp::StartPreciseCoverageArgs,
|
|
|
|
) -> Result<cdp::StartPreciseCoverageResponse, AnyError> {
|
2021-07-08 11:53:56 -04:00
|
|
|
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,
|
2023-11-05 17:58:59 -05:00
|
|
|
) -> Result<cdp::TakePreciseCoverageResponse, AnyError> {
|
2021-07-08 11:53:56 -04:00
|
|
|
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
|
2023-11-05 17:58:59 -05:00
|
|
|
.start_precise_coverage(cdp::StartPreciseCoverageArgs {
|
2021-07-08 11:53:56 -04:00
|
|
|
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 {
|
2023-09-11 09:53:42 -04:00
|
|
|
// Filter out internal JS files from being included in coverage reports
|
|
|
|
if script_coverage.url.starts_with("ext:")
|
|
|
|
|| script_coverage.url.starts_with("[ext:")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
let filename = format!("{}.json", Uuid::new_v4());
|
2021-06-28 21:39:19 -04:00
|
|
|
let filepath = self.dir.join(filename);
|
|
|
|
|
2023-10-24 09:37:02 -04:00
|
|
|
let mut out = BufWriter::new(File::create(&filepath)?);
|
2021-12-07 19:21:04 -05:00
|
|
|
let coverage = serde_json::to_string(&script_coverage)?;
|
2023-10-24 09:37:02 -04:00
|
|
|
let formatted_coverage =
|
|
|
|
format_json(&filepath, &coverage, &Default::default())
|
|
|
|
.ok()
|
|
|
|
.flatten()
|
|
|
|
.unwrap_or(coverage);
|
2021-12-07 19:21:04 -05:00
|
|
|
|
2023-06-26 09:10:27 -04:00
|
|
|
out.write_all(formatted_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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 02:54:52 -05:00
|
|
|
#[derive(Debug, Clone)]
|
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
|
|
|
}
|
|
|
|
|
2023-12-08 02:54:52 -05:00
|
|
|
#[derive(Debug, Clone)]
|
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
|
|
|
}
|
|
|
|
|
2023-12-08 02:54:52 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2023-12-10 23:30:38 -05:00
|
|
|
pub struct CoverageReport {
|
2021-12-23 20:02:54 -05:00
|
|
|
url: ModuleSpecifier,
|
|
|
|
named_functions: Vec<FunctionCoverageItem>,
|
|
|
|
branches: Vec<BranchCoverageItem>,
|
2023-12-08 02:54:52 -05:00
|
|
|
/// (line_index, number_of_hits)
|
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(
|
2023-11-05 17:58:59 -05:00
|
|
|
script_coverage: &cdp::ScriptCoverage,
|
2023-03-21 18:33:12 -04:00
|
|
|
script_source: String,
|
2021-12-23 20:02:54 -05:00
|
|
|
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());
|
2023-03-21 18:33:12 -04:00
|
|
|
let text_lines = TextLines::new(&script_source);
|
2021-12-23 20:02:54 -05:00
|
|
|
|
2023-03-21 18:33:12 -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()
|
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-10-06 13:28:28 -04:00
|
|
|
fn collect_coverages(
|
2023-01-13 16:56:29 -05:00
|
|
|
files: FileFlags,
|
2023-11-05 17:58:59 -05:00
|
|
|
) -> Result<Vec<cdp::ScriptCoverage>, AnyError> {
|
|
|
|
let mut coverages: Vec<cdp::ScriptCoverage> = Vec::new();
|
2022-12-07 13:10:10 -05:00
|
|
|
let file_paths = FileCollector::new(|file_path| {
|
2023-03-15 17:46:36 -04:00
|
|
|
file_path
|
|
|
|
.extension()
|
|
|
|
.map(|ext| ext == "json")
|
|
|
|
.unwrap_or(false)
|
2022-12-07 13:10:10 -05:00
|
|
|
})
|
|
|
|
.ignore_git_folder()
|
|
|
|
.ignore_node_modules()
|
2023-08-06 21:56:56 -04:00
|
|
|
.ignore_vendor_folder()
|
2023-01-13 16:56:29 -05:00
|
|
|
.add_ignore_paths(&files.ignore)
|
2023-09-08 10:04:45 -04:00
|
|
|
.collect_files(if files.include.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(&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())?;
|
2023-11-05 17:58:59 -05:00
|
|
|
let new_coverage: cdp::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(
|
2023-11-05 17:58:59 -05:00
|
|
|
coverages: Vec<cdp::ScriptCoverage>,
|
2021-02-24 09:27:51 -05:00
|
|
|
include: Vec<String>,
|
|
|
|
exclude: Vec<String>,
|
2023-10-03 19:05:06 -04:00
|
|
|
npm_resolver: &dyn CliNpmResolver,
|
2023-11-05 17:58:59 -05:00
|
|
|
) -> Vec<cdp::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| {
|
2023-03-08 06:44:54 -05:00
|
|
|
let is_internal = e.url.starts_with("ext:")
|
2021-02-24 09:27:51 -05:00
|
|
|
|| e.url.ends_with("__anonymous__")
|
2022-05-04 07:10:55 -04:00
|
|
|
|| e.url.ends_with("$deno$test.js")
|
2023-04-19 17:30:52 -04:00
|
|
|
|| e.url.ends_with(".snap")
|
2023-10-03 19:05:06 -04:00
|
|
|
|| is_supported_test_path(Path::new(e.url.as_str()))
|
|
|
|
|| Url::parse(&e.url)
|
|
|
|
.ok()
|
|
|
|
.map(|url| npm_resolver.in_npm_package(&url))
|
|
|
|
.unwrap_or(false);
|
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
|
|
|
})
|
2023-11-05 17:58:59 -05:00
|
|
|
.collect::<Vec<cdp::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"));
|
|
|
|
}
|
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
2023-10-03 19:05:06 -04:00
|
|
|
let npm_resolver = factory.npm_resolver().await?;
|
2023-05-01 14:35:23 -04:00
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let emitter = factory.emitter()?;
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2023-12-08 02:54:52 -05:00
|
|
|
assert!(!coverage_flags.files.include.is_empty());
|
|
|
|
|
|
|
|
// Use the first include path as the default output path.
|
|
|
|
let coverage_root = coverage_flags.files.include[0].clone();
|
2023-01-13 16:56:29 -05:00
|
|
|
let script_coverages = collect_coverages(coverage_flags.files)?;
|
2023-12-18 05:43:27 -05:00
|
|
|
if script_coverages.is_empty() {
|
|
|
|
return Err(generic_error("No coverage files found"));
|
|
|
|
}
|
2021-12-20 16:29:02 -05:00
|
|
|
let script_coverages = filter_coverages(
|
|
|
|
script_coverages,
|
|
|
|
coverage_flags.include,
|
|
|
|
coverage_flags.exclude,
|
2023-10-03 19:05:06 -04:00
|
|
|
npm_resolver.as_ref(),
|
2021-12-20 16:29:02 -05:00
|
|
|
);
|
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![]
|
|
|
|
};
|
|
|
|
|
2023-12-10 23:30:38 -05:00
|
|
|
let mut reporter = reporter::create(coverage_flags.r#type);
|
2021-02-24 09:27:51 -05:00
|
|
|
|
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 {
|
2023-03-13 21:12:09 -04:00
|
|
|
let module_specifier = deno_core::resolve_url_or_path(
|
|
|
|
&script_coverage.url,
|
2023-05-01 14:35:23 -04:00
|
|
|
cli_options.initial_cwd(),
|
2023-03-13 21:12:09 -04:00
|
|
|
)?;
|
2020-12-21 08:04:25 -05:00
|
|
|
|
2022-01-10 11:36:58 -05:00
|
|
|
let maybe_file = if module_specifier.scheme() == "file" {
|
2023-05-01 14:35:23 -04:00
|
|
|
file_fetcher.get_source(&module_specifier)
|
2022-01-10 11:36:58 -05:00
|
|
|
} else {
|
2023-05-01 14:35:23 -04:00
|
|
|
file_fetcher
|
2022-01-10 11:36:58 -05:00
|
|
|
.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
|
2023-03-21 18:33:12 -04:00
|
|
|
let original_source = file.source.clone();
|
|
|
|
let transpiled_code: ModuleCode = match file.media_type {
|
2022-01-10 11:36:58 -05:00
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Unknown
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
2023-04-04 08:46:31 -04:00
|
|
|
| MediaType::Json => file.source.clone().into(),
|
2023-03-21 18:33:12 -04:00
|
|
|
MediaType::Dts | MediaType::Dmts | MediaType::Dcts => Default::default(),
|
2022-01-10 11:36:58 -05:00
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Tsx => {
|
2023-06-26 09:10:27 -04:00
|
|
|
match emitter.maybe_cached_emit(&file.specifier, &file.source) {
|
2023-03-21 18:33:12 -04:00
|
|
|
Some(code) => code.into(),
|
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!()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
let source_map = source_map_from_code(&transpiled_code);
|
2021-12-23 20:02:54 -05:00
|
|
|
let coverage_report = generate_coverage_report(
|
2021-10-06 13:28:28 -04:00
|
|
|
&script_coverage,
|
2023-04-04 08:46:31 -04:00
|
|
|
transpiled_code.as_str().to_owned(),
|
2023-03-21 18:33:12 -04:00
|
|
|
&source_map,
|
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() {
|
2023-03-21 18:33:12 -04:00
|
|
|
reporter.report(&coverage_report, &original_source)?;
|
2022-05-22 10:45:22 -04:00
|
|
|
}
|
2021-10-06 01:05:18 -04:00
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
|
2023-12-08 02:54:52 -05:00
|
|
|
reporter.done(&coverage_root);
|
2021-10-06 13:28:28 -04:00
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
Ok(())
|
|
|
|
}
|