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
|
|
|
|
2024-03-27 14:25:39 -04:00
|
|
|
use crate::args::CliOptions;
|
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-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;
|
2024-07-05 17:53:09 -04:00
|
|
|
use deno_config::glob::FileCollector;
|
2024-01-15 19:15:39 -05:00
|
|
|
use deno_config::glob::FilePatterns;
|
|
|
|
use deno_config::glob::PathOrPattern;
|
|
|
|
use deno_config::glob::PathOrPatternSet;
|
2022-01-10 11:36:58 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
2022-12-09 09:40:48 -05:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2022-04-15 10:08:09 -04:00
|
|
|
use deno_core::sourcemap::SourceMap;
|
2021-10-06 13:28:28 -04:00
|
|
|
use deno_core::url::Url;
|
2021-05-26 15:07:12 -04:00
|
|
|
use deno_core::LocalInspectorSession;
|
2021-02-24 09:27:51 -05:00
|
|
|
use regex::Regex;
|
2020-12-21 08:04:25 -05:00
|
|
|
use std::fs;
|
2021-06-28 21:39:19 -04:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufWriter;
|
2023-01-14 23:18:58 -05:00
|
|
|
use std::io::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;
|
2024-07-23 19:00:48 -04:00
|
|
|
use std::sync::Arc;
|
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
|
|
|
}
|
|
|
|
|
2024-02-13 11:22:30 -05:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl crate::worker::CoverageCollector for CoverageCollector {
|
|
|
|
async fn start_collecting(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.enable_debugger().await?;
|
|
|
|
self.enable_profiler().await?;
|
|
|
|
self
|
|
|
|
.start_precise_coverage(cdp::StartPreciseCoverageArgs {
|
|
|
|
call_count: true,
|
|
|
|
detailed: true,
|
|
|
|
allow_triggered_updates: false,
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn stop_collecting(&mut self) -> Result<(), AnyError> {
|
|
|
|
fs::create_dir_all(&self.dir)?;
|
|
|
|
|
|
|
|
let script_coverages = self.take_precise_coverage().await?.result;
|
|
|
|
for script_coverage in script_coverages {
|
2024-05-28 10:01:32 -04:00
|
|
|
// Filter out internal and http/https JS files and eval'd scripts
|
|
|
|
// from being included in coverage reports
|
2024-02-13 11:22:30 -05:00
|
|
|
if script_coverage.url.starts_with("ext:")
|
|
|
|
|| script_coverage.url.starts_with("[ext:")
|
2024-05-28 07:25:46 -04:00
|
|
|
|| script_coverage.url.starts_with("http:")
|
|
|
|
|| script_coverage.url.starts_with("https:")
|
2024-04-16 09:45:41 -04:00
|
|
|
|| script_coverage.url.starts_with("node:")
|
2024-05-28 10:01:32 -04:00
|
|
|
|| script_coverage.url.is_empty()
|
2024-02-13 11:22:30 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let filename = format!("{}.json", Uuid::new_v4());
|
|
|
|
let filepath = self.dir.join(filename);
|
|
|
|
|
|
|
|
let mut out = BufWriter::new(File::create(&filepath)?);
|
|
|
|
let coverage = serde_json::to_string(&script_coverage)?;
|
|
|
|
let formatted_coverage =
|
|
|
|
format_json(&filepath, &coverage, &Default::default())
|
|
|
|
.ok()
|
|
|
|
.flatten()
|
|
|
|
.unwrap_or(coverage);
|
|
|
|
|
|
|
|
out.write_all(formatted_coverage.as_bytes())?;
|
|
|
|
out.flush()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.disable_debugger().await?;
|
|
|
|
self.disable_profiler().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-01-04 08:49:17 -05:00
|
|
|
let line_index = range_to_src_line_index(
|
|
|
|
&function.ranges[0],
|
|
|
|
&text_lines,
|
|
|
|
&maybe_source_map,
|
2022-07-11 19:02:11 -04: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() {
|
2024-01-04 08:49:17 -05:00
|
|
|
let line_index =
|
|
|
|
range_to_src_line_index(range, &text_lines, &maybe_source_map);
|
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
|
|
|
|
2024-01-04 08:49:17 -05:00
|
|
|
fn range_to_src_line_index(
|
|
|
|
range: &cdp::CoverageRange,
|
|
|
|
text_lines: &TextLines,
|
|
|
|
maybe_source_map: &Option<SourceMap>,
|
|
|
|
) -> usize {
|
|
|
|
let source_lc = text_lines.line_and_column_index(
|
|
|
|
text_lines.byte_index_from_char_index(range.start_char_offset),
|
|
|
|
);
|
|
|
|
if let Some(source_map) = maybe_source_map.as_ref() {
|
|
|
|
source_map
|
|
|
|
.lookup_token(source_lc.line_index as u32, source_lc.column_index as u32)
|
|
|
|
.map(|token| token.get_src_line() as usize)
|
|
|
|
.unwrap_or(0)
|
|
|
|
} else {
|
|
|
|
source_lc.line_index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 13:28:28 -04:00
|
|
|
fn collect_coverages(
|
2024-03-27 14:25:39 -04:00
|
|
|
cli_options: &CliOptions,
|
2023-01-13 16:56:29 -05:00
|
|
|
files: FileFlags,
|
2024-01-08 12:18:42 -05:00
|
|
|
initial_cwd: &Path,
|
2023-11-05 17:58:59 -05:00
|
|
|
) -> Result<Vec<cdp::ScriptCoverage>, AnyError> {
|
|
|
|
let mut coverages: Vec<cdp::ScriptCoverage> = Vec::new();
|
2024-01-08 12:18:42 -05:00
|
|
|
let file_patterns = FilePatterns {
|
2024-01-18 15:57:30 -05:00
|
|
|
base: initial_cwd.to_path_buf(),
|
2024-01-08 12:18:42 -05:00
|
|
|
include: Some({
|
2024-01-15 19:15:39 -05:00
|
|
|
if files.include.is_empty() {
|
|
|
|
PathOrPatternSet::new(vec![PathOrPattern::Path(
|
|
|
|
initial_cwd.to_path_buf(),
|
|
|
|
)])
|
2024-01-08 12:18:42 -05:00
|
|
|
} else {
|
2024-03-07 20:16:32 -05:00
|
|
|
PathOrPatternSet::from_include_relative_path_or_patterns(
|
2024-01-15 19:15:39 -05:00
|
|
|
initial_cwd,
|
|
|
|
&files.include,
|
|
|
|
)?
|
|
|
|
}
|
2024-01-08 12:18:42 -05:00
|
|
|
}),
|
2024-05-27 23:37:30 -04:00
|
|
|
exclude: PathOrPatternSet::new(vec![]),
|
2024-01-08 12:18:42 -05:00
|
|
|
};
|
2024-03-07 20:16:32 -05:00
|
|
|
let file_paths = FileCollector::new(|e| {
|
|
|
|
e.path.extension().map(|ext| ext == "json").unwrap_or(false)
|
2022-12-07 13:10:10 -05:00
|
|
|
})
|
|
|
|
.ignore_git_folder()
|
|
|
|
.ignore_node_modules()
|
2024-03-27 14:25:39 -04:00
|
|
|
.set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
|
2024-07-05 17:53:09 -04:00
|
|
|
.collect_file_patterns(&deno_config::fs::RealDenoConfigFs, file_patterns)?;
|
2020-12-21 08:04:25 -05:00
|
|
|
|
2024-05-27 23:37:30 -04:00
|
|
|
let coverage_patterns = FilePatterns {
|
|
|
|
base: initial_cwd.to_path_buf(),
|
|
|
|
include: None,
|
|
|
|
exclude: PathOrPatternSet::from_exclude_relative_path_or_patterns(
|
|
|
|
initial_cwd,
|
|
|
|
&files.ignore,
|
|
|
|
)
|
|
|
|
.context("Invalid ignore pattern.")?,
|
|
|
|
};
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
for file_path in file_paths {
|
2024-01-08 12:18:42 -05:00
|
|
|
let new_coverage = fs::read_to_string(file_path.as_path())
|
|
|
|
.map_err(AnyError::from)
|
2024-05-27 23:37:30 -04:00
|
|
|
.and_then(|json| {
|
|
|
|
serde_json::from_str::<cdp::ScriptCoverage>(&json)
|
|
|
|
.map_err(AnyError::from)
|
|
|
|
})
|
2024-01-08 12:18:42 -05:00
|
|
|
.with_context(|| format!("Failed reading '{}'", file_path.display()))?;
|
2024-05-27 23:37:30 -04:00
|
|
|
let url = Url::parse(&new_coverage.url)?;
|
|
|
|
if coverage_patterns.matches_specifier(&url) {
|
|
|
|
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();
|
|
|
|
|
2024-09-20 02:04:22 -04:00
|
|
|
// Matches virtual file paths for doc testing
|
|
|
|
// e.g. file:///path/to/mod.ts$23-29.ts
|
|
|
|
let doc_test_re =
|
|
|
|
Regex::new(r"\$\d+-\d+\.(js|mjs|cjs|jsx|ts|mts|cts|tsx)$").unwrap();
|
|
|
|
|
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()))
|
2024-09-20 02:04:22 -04:00
|
|
|
|| doc_test_re.is_match(e.url.as_str())
|
2023-10-03 19:05:06 -04:00
|
|
|
|| 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(
|
2024-07-23 19:00:48 -04:00
|
|
|
flags: Arc<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"));
|
|
|
|
}
|
|
|
|
|
2024-07-23 19:00:48 -04:00
|
|
|
let factory = CliFactory::from_flags(flags);
|
|
|
|
let cli_options = factory.cli_options()?;
|
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 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.
|
2024-01-15 19:15:39 -05:00
|
|
|
let coverage_root = cli_options
|
|
|
|
.initial_cwd()
|
|
|
|
.join(&coverage_flags.files.include[0]);
|
2024-03-27 14:25:39 -04:00
|
|
|
let script_coverages = collect_coverages(
|
|
|
|
cli_options,
|
|
|
|
coverage_flags.files,
|
|
|
|
cli_options.initial_cwd(),
|
|
|
|
)?;
|
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
|
|
|
);
|
2024-03-15 23:58:57 -04:00
|
|
|
if script_coverages.is_empty() {
|
|
|
|
return Err(generic_error("No covered files included in the report"));
|
|
|
|
}
|
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
|
2024-04-18 21:43:28 -04:00
|
|
|
.fetch_cached(&module_specifier, 10)
|
2022-01-10 11:36:58 -05:00
|
|
|
.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
|
|
|
|
)
|
2024-01-31 22:15:22 -05:00
|
|
|
})?.into_text_decoded()?;
|
2022-01-10 11:36:58 -05:00
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
let original_source = file.source.clone();
|
2024-01-31 22:15:22 -05:00
|
|
|
// Check if file was transpiled
|
|
|
|
let transpiled_code = match file.media_type {
|
2022-01-10 11:36:58 -05:00
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Unknown
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
2024-01-31 22:15:22 -05:00
|
|
|
| MediaType::Json => None,
|
2024-06-05 11:04:16 -04:00
|
|
|
MediaType::Dts | MediaType::Dmts | MediaType::Dcts => Some(Vec::new()),
|
2022-01-10 11:36:58 -05:00
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Tsx => {
|
2024-01-31 22:15:22 -05:00
|
|
|
Some(match emitter.maybe_cached_emit(&file.specifier, &file.source) {
|
|
|
|
Some(code) => code,
|
2022-07-12 18:58:39 -04:00
|
|
|
None => {
|
2022-01-10 11:36:58 -05:00
|
|
|
return Err(anyhow!(
|
|
|
|
"Missing transpiled source code for: \"{}\".
|
|
|
|
Before generating coverage report, run `deno test --coverage` to ensure consistent state.",
|
|
|
|
file.specifier,
|
|
|
|
))
|
|
|
|
}
|
2024-01-31 22:15:22 -05:00
|
|
|
})
|
2022-01-10 11:36:58 -05:00
|
|
|
}
|
|
|
|
MediaType::Wasm | MediaType::TsBuildInfo | MediaType::SourceMap => {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
};
|
2024-06-05 11:04:16 -04:00
|
|
|
let runtime_code: String = match transpiled_code {
|
|
|
|
Some(code) => String::from_utf8(code)
|
|
|
|
.with_context(|| format!("Failed decoding {}", file.specifier))?,
|
|
|
|
None => original_source.to_string(),
|
|
|
|
};
|
2022-01-10 11:36:58 -05:00
|
|
|
|
2024-06-05 11:04:16 -04:00
|
|
|
let source_map = source_map_from_code(runtime_code.as_bytes());
|
2021-12-23 20:02:54 -05:00
|
|
|
let coverage_report = generate_coverage_report(
|
2021-10-06 13:28:28 -04:00
|
|
|
&script_coverage,
|
2024-01-31 22:15:22 -05:00
|
|
|
runtime_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(())
|
|
|
|
}
|