From ae8874b4b2015453e53965dae2a2dae9cacbce70 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Wed, 24 Feb 2021 22:27:51 +0800 Subject: feat: add "deno coverage" subcommand (#8664) This commit adds a new subcommand called "coverage" which can generate code coverage reports to stdout in multiple formats from code coverage profiles collected to disk. Currently this supports outputting a pretty printed diff and the lcov format for interoperability with third-party services and tools. Code coverage is still collected via other subcommands that run and collect code coverage such as "deno test --coverage=" but that command no longer prints a pretty printed report at the end of a test run with coverage collection enabled. The restrictions on which files that can be reported on has also been relaxed and are fully controllable with the include and exclude regular expression flags on the coverage subcommand. Co-authored-by: Luca Casonato --- cli/main.rs | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) (limited to 'cli/main.rs') diff --git a/cli/main.rs b/cli/main.rs index b9516e704..e957b9342 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -978,6 +978,34 @@ async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> { Ok(()) } +async fn coverage_command( + flags: Flags, + files: Vec, + ignore: Vec, + include: Vec, + exclude: Vec, + lcov: bool, +) -> Result<(), AnyError> { + if !flags.unstable { + exit_unstable("compile"); + } + + if files.is_empty() { + println!("No matching coverage profiles found"); + std::process::exit(1); + } + + tools::coverage::cover_files( + flags.clone(), + files, + ignore, + include, + exclude, + lcov, + ) + .await +} + async fn test_command( flags: Flags, include: Option>, @@ -1047,7 +1075,6 @@ async fn test_command( let mut maybe_coverage_collector = if let Some(ref coverage_dir) = program_state.coverage_dir { let session = worker.create_inspector_session(); - let coverage_dir = PathBuf::from(coverage_dir); let mut coverage_collector = tools::coverage::CoverageCollector::new(coverage_dir, session); @@ -1067,20 +1094,6 @@ async fn test_command( if let Some(coverage_collector) = maybe_coverage_collector.as_mut() { coverage_collector.stop_collecting().await?; - - // TODO(caspervonb) extract reporting into it's own subcommand. - // For now, we'll only report for the command that passed --coverage as a flag. - if flags.coverage_dir.is_some() { - let mut exclude = test_modules.clone(); - exclude.push(main_module.clone()); - tools::coverage::report_coverages( - program_state.clone(), - &coverage_collector.dir, - quiet, - exclude, - ) - .await?; - } } Ok(()) @@ -1172,6 +1185,14 @@ fn get_subcommand( target, } => compile_command(flags, source_file, output, args, target, lite) .boxed_local(), + DenoSubcommand::Coverage { + files, + ignore, + include, + exclude, + lcov, + } => coverage_command(flags, files, ignore, include, exclude, lcov) + .boxed_local(), DenoSubcommand::Fmt { check, files, -- cgit v1.2.3